acetwo3ee commited on
Commit
beaf481
·
verified ·
1 Parent(s): b31ac12

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +26 -8
src/streamlit_app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import requests
3
  import pandas as pd
4
  import io
 
5
 
6
  # Function to fetch WNBA player game log data
7
  @st.cache_data
@@ -47,13 +48,25 @@ def fetch_wnba_data():
47
  "sec-fetch-site": "same-origin"
48
  }
49
 
50
- response = requests.get(url, params=params, headers=headers)
51
- if response.status_code == 200:
 
 
 
52
  data = response.json()
53
  game_logs = data.get("resultSets", [])[0].get("rowSet", [])
 
 
 
54
  return game_logs
55
- else:
56
- st.error(f"Failed to fetch data. Status code: {response.status_code}")
 
 
 
 
 
 
57
  return []
58
 
59
  # Function to convert game logs to a DataFrame
@@ -66,7 +79,7 @@ def create_dataframe(game_logs):
66
  "Double_Double", "Triple_Double", "MIN_Rank", "FGM_Rank", "FGA_Rank", "FG_PCT_Rank",
67
  "FG3M_Rank", "FG3A_Rank", "FG3_PCT_Rank", "FTM_Rank", "FTA_Rank", "FT_PCT_Rank",
68
  "OREB_Rank", "DREB_Rank", "REB_Rank", "AST_Rank", "STL_Rank", "BLK_Rank",
69
- "TO_Rank", "PF_Rank", "PTS_Rank", "PLUS_MINUS_Rank", "Fantasy_Points_Rank",
70
  "Double_Double_Rank", "Triple_Double_Rank", "Minutes_Formatted"
71
  ]
72
  return pd.DataFrame(game_logs, columns=columns)
@@ -80,13 +93,18 @@ def convert_to_csv(df):
80
  # Streamlit app
81
  st.title("WNBA 2025 Player Game Logs")
82
 
83
- # Fetch data
84
  with st.spinner("Fetching WNBA player game log data..."):
 
85
  game_logs = fetch_wnba_data()
 
 
 
86
 
87
  if game_logs:
88
  # Create DataFrame
89
- df = create_dataframe(game_logs)
 
90
 
91
  # Display total number of records
92
  st.write(f"Total records fetched: {len(df)}")
@@ -127,7 +145,7 @@ if game_logs:
127
  player_data = player_data.sort_values("Game_Date")
128
  st.line_chart(player_data.set_index("Game_Date")["PTS"])
129
  else:
130
- st.warning("No data available to display.")
131
 
132
  # Footer
133
  st.markdown("Data source: [WNBA Stats API](https://stats.wnba.com)")
 
2
  import requests
3
  import pandas as pd
4
  import io
5
+ import time
6
 
7
  # Function to fetch WNBA player game log data
8
  @st.cache_data
 
48
  "sec-fetch-site": "same-origin"
49
  }
50
 
51
+ try:
52
+ st.write("Sending API request...")
53
+ response = requests.get(url, params=params, headers=headers, timeout=10)
54
+ response.raise_for_status() # Raises an error for non-200 status codes
55
+ st.write("API request successful. Processing data...")
56
  data = response.json()
57
  game_logs = data.get("resultSets", [])[0].get("rowSet", [])
58
+ if not game_logs:
59
+ st.warning("No game logs found in the response.")
60
+ return []
61
  return game_logs
62
+ except requests.Timeout:
63
+ st.error("Request timed out after 10 seconds. Try again later or check your network.")
64
+ return []
65
+ except requests.HTTPError as e:
66
+ st.error(f"HTTP error occurred: {e}. Check API headers or rate limits.")
67
+ return []
68
+ except requests.RequestException as e:
69
+ st.error(f"Failed to fetch data: {e}")
70
  return []
71
 
72
  # Function to convert game logs to a DataFrame
 
79
  "Double_Double", "Triple_Double", "MIN_Rank", "FGM_Rank", "FGA_Rank", "FG_PCT_Rank",
80
  "FG3M_Rank", "FG3A_Rank", "FG3_PCT_Rank", "FTM_Rank", "FTA_Rank", "FT_PCT_Rank",
81
  "OREB_Rank", "DREB_Rank", "REB_Rank", "AST_Rank", "STL_Rank", "BLK_Rank",
82
+ "TO_Rank", "PF_Rank", "PTS_Rank", "PLUS_MINUS_Rank", "Fantasy_Score_Rank",
83
  "Double_Double_Rank", "Triple_Double_Rank", "Minutes_Formatted"
84
  ]
85
  return pd.DataFrame(game_logs, columns=columns)
 
93
  # Streamlit app
94
  st.title("WNBA 2025 Player Game Logs")
95
 
96
+ # Fetch data with progress feedback
97
  with st.spinner("Fetching WNBA player game log data..."):
98
+ start_time = time.time()
99
  game_logs = fetch_wnba_data()
100
+ fetch_time = time.time() - start_time
101
+ if game_logs:
102
+ st.success(f"Data fetched successfully in {fetch_time:.2f} seconds!")
103
 
104
  if game_logs:
105
  # Create DataFrame
106
+ with st.spinner("Converting data to DataFrame..."):
107
+ df = create_dataframe(game_logs)
108
 
109
  # Display total number of records
110
  st.write(f"Total records fetched: {len(df)}")
 
145
  player_data = player_data.sort_values("Game_Date")
146
  st.line_chart(player_data.set_index("Game_Date")["PTS"])
147
  else:
148
+ st.warning("No data available to display. Check error messages above.")
149
 
150
  # Footer
151
  st.markdown("Data source: [WNBA Stats API](https://stats.wnba.com)")