Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -40,174 +40,101 @@ NBA_Data = 'https://docs.google.com/spreadsheets/d/1Yq0vGriWK-bS79e-bD6_u9pqrYE6
|
|
40 |
def init_baselines():
|
41 |
sh = gcservice_account.open_by_url(NBA_Data)
|
42 |
|
43 |
-
worksheet = sh.worksheet('
|
44 |
raw_display = pd.DataFrame(worksheet.get_values())
|
45 |
raw_display.columns = raw_display.iloc[0]
|
46 |
raw_display = raw_display[1:]
|
47 |
raw_display = raw_display.reset_index(drop=True)
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
data_cols = trend_table.columns.drop(['PLAYER_NAME', 'Team', 'Position', 'FD_Position'])
|
58 |
-
trend_table[data_cols] = trend_table[data_cols].apply(pd.to_numeric, errors='coerce')
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
-
|
|
|
|
|
|
|
73 |
|
74 |
def convert_df_to_csv(df):
|
75 |
return df.to_csv().encode('utf-8')
|
76 |
|
77 |
-
|
78 |
|
79 |
col1, col2 = st.columns([1, 9])
|
80 |
with col1:
|
81 |
if st.button("Reset Data", key='reset1'):
|
82 |
st.cache_data.clear()
|
83 |
-
|
84 |
-
split_var1 = st.radio("What table would you like to view?", ('
|
85 |
-
site_var1 = st.radio("What site would you like to view?", ('Draftkings', 'Fanduel'), key='site_var1')
|
86 |
-
if site_var1 == 'Draftkings':
|
87 |
-
trend_table = trend_table[['PLAYER_NAME', 'Team', 'Position', 'L10 MIN', 'L10 Fantasy', 'L10 Ceiling',
|
88 |
-
'L5 MIN', 'L5 Fantasy', 'L5 Ceiling', 'L3 MIN', 'L3 Fantasy',
|
89 |
-
'L3 Ceiling', 'Trend Min', 'Trend Median', 'DK_Proj', 'Adj Median', 'Adj Ceiling',
|
90 |
-
'DK_Salary', 'DK_Avg_Val', 'DK_Ceiling_Value']]
|
91 |
-
minutes_table = dk_minutes_table
|
92 |
-
medians_table = dk_medians_table
|
93 |
-
proj_medians_table = dk_proj_medians_table
|
94 |
-
elif site_var1 == 'Fanduel':
|
95 |
-
trend_table = trend_table[['PLAYER_NAME', 'Team', 'FD_Position', 'L10 MIN', 'L10 FD_Fantasy',
|
96 |
-
'L10 FD_Ceiling', 'L5 MIN', 'L5 FD_Fantasy', 'L5 FD_Ceiling', 'L3 MIN', 'L3 FD_Fantasy',
|
97 |
-
'L3 FD_Ceiling', 'Trend Min', 'Trend FD_Median', 'FD_Proj', 'Adj FD_Median', 'Adj FD_Ceiling',
|
98 |
-
'FD_Salary', 'FD_Avg_Val', 'FD_Ceiling_Value']]
|
99 |
-
minutes_table = fd_minutes_table
|
100 |
-
medians_table = fd_medians_table
|
101 |
-
proj_medians_table = fd_proj_medians_table
|
102 |
-
trend_table = trend_table.set_axis(['PLAYER_NAME', 'Team', 'Position', 'L10 MIN', 'L10 Fantasy', 'L10 Ceiling',
|
103 |
-
'L5 MIN', 'L5 Fantasy', 'L5 Ceiling', 'L3 MIN', 'L3 Fantasy',
|
104 |
-
'L3 Ceiling', 'Trend Min', 'Trend Median', 'Proj', 'Adj Median', 'Adj Ceiling',
|
105 |
-
'Salary', 'Avg_Val', 'Ceiling_Value'], axis=1)
|
106 |
-
minutes_table = minutes_table.set_axis(['PLAYER_NAME', 'Team', 'L10 MIN', 'L5 MIN', 'L3 MIN', 'Trend Min'], axis=1)
|
107 |
-
medians_table = medians_table.set_axis(['PLAYER_NAME', 'Team', 'L10 Fantasy','L5 Fantasy', 'L3 Fantasy', 'Trend Median'], axis=1)
|
108 |
-
proj_medians_table = proj_medians_table.set_axis(['PLAYER_NAME', 'Team', 'Position', 'Salary', 'Proj',
|
109 |
-
'Adj Median', 'Avg_Val', 'Adj Ceiling', 'Ceiling_Value'], axis=1)
|
110 |
-
if split_var1 == 'Overall':
|
111 |
-
view_var1 = trend_table.Team.values.tolist()
|
112 |
-
split_var2 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var2')
|
113 |
-
|
114 |
-
if split_var2 == 'Specific Teams':
|
115 |
-
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = view_var1, key='team_var1')
|
116 |
-
elif split_var2 == 'All':
|
117 |
-
team_var1 = view_var1
|
118 |
-
|
119 |
-
split_var3 = st.radio("Would you like to view all positions or specific ones?", ('All', 'Specific Positions'), key='split_var3')
|
120 |
-
if split_var3 == 'Specific Positions':
|
121 |
-
pos_var1 = st.multiselect('Which positions would you like to include in the tables?', options = ['PG', 'SG', 'SF', 'PF', 'C'], key='pos_var1')
|
122 |
-
elif split_var3 == 'All':
|
123 |
-
pos_var1 = ['PG', 'SG', 'SF', 'PF', 'C']
|
124 |
-
|
125 |
-
proj_var1 = st.slider("Is there a certain projection range you want to view?", 0, 100, (10, 100), key='proj_var1')
|
126 |
-
|
127 |
-
elif split_var1 == 'Minutes Trends':
|
128 |
-
view_var1 = trend_table.Team.values.tolist()
|
129 |
-
split_var2 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var2')
|
130 |
-
|
131 |
-
if split_var2 == 'Specific Teams':
|
132 |
-
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = view_var1, key='team_var1')
|
133 |
-
elif split_var2 == 'All':
|
134 |
-
team_var1 = view_var1
|
135 |
-
|
136 |
-
elif split_var1 == 'Fantasy Trends':
|
137 |
-
view_var1 = trend_table.Team.values.tolist()
|
138 |
-
split_var2 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var2')
|
139 |
-
|
140 |
-
if split_var2 == 'Specific Teams':
|
141 |
-
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = view_var1, key='team_var1')
|
142 |
-
elif split_var2 == 'All':
|
143 |
-
team_var1 = view_var1
|
144 |
-
|
145 |
-
elif split_var1 == 'Slate specific':
|
146 |
-
view_var1 = trend_table.Team.values.tolist()
|
147 |
-
split_var2 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var2')
|
148 |
-
|
149 |
-
if split_var2 == 'Specific Teams':
|
150 |
-
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = view_var1, key='team_var1')
|
151 |
-
elif split_var2 == 'All':
|
152 |
-
team_var1 = view_var1
|
153 |
-
|
154 |
-
split_var3 = st.radio("Would you like to view all positions or specific ones?", ('All', 'Specific Positions'), key='split_var3')
|
155 |
-
if split_var3 == 'Specific Positions':
|
156 |
-
pos_var1 = st.multiselect('Which positions would you like to include in the tables?', options = ['PG', 'SG', 'SF', 'PF', 'C'], key='pos_var1')
|
157 |
-
elif split_var3 == 'All':
|
158 |
-
pos_var1 = ['PG', 'SG', 'SF', 'PF', 'C']
|
159 |
-
|
160 |
-
proj_var1 = st.slider("Is there a certain projection range you want to view?", 0, 100, (10, 100), key='proj_var1')
|
161 |
|
162 |
with col2:
|
163 |
-
if split_var1 == '
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
label="Export Trending Numbers",
|
184 |
-
data=convert_df_to_csv(table_display),
|
185 |
-
file_name='Trending_export.csv',
|
186 |
-
mime='text/csv',
|
187 |
-
)
|
188 |
-
|
189 |
-
elif split_var1 == 'Fantasy Trends':
|
190 |
-
table_display = medians_table[medians_table['Team'].isin(team_var1)]
|
191 |
-
table_display = table_display.set_index('PLAYER_NAME')
|
192 |
-
st.dataframe(table_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
193 |
-
st.download_button(
|
194 |
-
label="Export Trending Numbers",
|
195 |
-
data=convert_df_to_csv(table_display),
|
196 |
-
file_name='Trending_export.csv',
|
197 |
-
mime='text/csv',
|
198 |
-
)
|
199 |
-
|
200 |
-
elif split_var1 == 'Slate specific':
|
201 |
-
table_display = proj_medians_table[proj_medians_table['Proj'] >= proj_var1[0]]
|
202 |
-
table_display = table_display[table_display['Proj'] <= proj_var1[1]]
|
203 |
-
table_display = table_display[table_display['Team'].isin(team_var1)]
|
204 |
-
table_display = table_display[table_display['Position'].str.contains('|'.join(pos_var1))]
|
205 |
-
table_display = table_display.sort_values(by='Adj Ceiling', ascending=False)
|
206 |
-
table_display = table_display.set_index('PLAYER_NAME')
|
207 |
-
st.dataframe(table_display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
208 |
-
st.download_button(
|
209 |
-
label="Export Trending Numbers",
|
210 |
-
data=convert_df_to_csv(table_display),
|
211 |
-
file_name='NBA_Trending_export.csv',
|
212 |
-
mime='text/csv',
|
213 |
-
)
|
|
|
40 |
def init_baselines():
|
41 |
sh = gcservice_account.open_by_url(NBA_Data)
|
42 |
|
43 |
+
worksheet = sh.worksheet('Gamelog')
|
44 |
raw_display = pd.DataFrame(worksheet.get_values())
|
45 |
raw_display.columns = raw_display.iloc[0]
|
46 |
raw_display = raw_display[1:]
|
47 |
raw_display = raw_display.reset_index(drop=True)
|
48 |
+
gamelog_table = raw_display[raw_display['PLAYER_NAME'] != ""]
|
49 |
+
gamelog_table.replace('', np.nan, inplace=True)
|
50 |
+
gamelog_table = gamelog_table[['PLAYER_NAME', 'TEAM_NAME', 'SEASON_ID', 'GAME_DATE', 'MATCHUP', 'MIN', 'touches', 'PTS', 'FGM', 'FGA', 'FG_PCT', 'FG3M', 'FG3A',
|
51 |
+
'FG3_PCT', 'FTM', 'FTA', 'FT_PCT', 'reboundChancesOffensive', 'OREB', 'reboundChancesDefensive', 'DREB', 'reboundChancesTotal', 'REB',
|
52 |
+
'passes', 'secondaryAssists', 'freeThrowAssists', 'assists', 'STL', 'BLK', 'TOV', 'PF', 'DD', 'TD', 'Fantasy', 'FD_Fantasy']]
|
53 |
+
gamelog_table['rebound%'] = gamelog_table['REB'] / gamelog_table['reboundChancesTotal']
|
54 |
+
gamelog_table['assists_per_pass'] = gamelog_table['assists'] / gamelog_table['passes']
|
55 |
+
gamelog_table['Fantasy_per_touch'] = gamelog_table['Fantasy'] / gamelog_table['touches']
|
56 |
+
gamelog_table['FD_Fantasy_per_touch'] = gamelog_table['FD_Fantasy'] / gamelog_table['touches']
|
|
|
|
|
57 |
|
58 |
+
season_long_table = gamelog_table[['PLAYER_NAME', 'TEAM_NAME']]
|
59 |
+
season_long_table['MIN'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['MIN'].transform('mean').astype(float)
|
60 |
+
season_long_table['touches'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['touches'].transform('mean').astype(float)
|
61 |
+
season_long_table['FGM'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FGM'].transform('mean').astype(float)
|
62 |
+
season_long_table['FGA'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FGA'].transform('mean').astype(float)
|
63 |
+
season_long_table['FG_PCT'] = (gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FGM'].transform('sum').astype(int) /
|
64 |
+
gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FGA'].transform('sum').astype(int))
|
65 |
+
season_long_table['FG3M'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FG3M'].transform('mean').astype(float)
|
66 |
+
season_long_table['FG3A'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FG3A'].transform('mean').astype(float)
|
67 |
+
season_long_table['FG3_PCT'] = (gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FG3M'].transform('sum').astype(int) /
|
68 |
+
gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FG3A'].transform('sum').astype(int))
|
69 |
+
season_long_table['FTM'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FTM'].transform('mean').astype(float)
|
70 |
+
season_long_table['FTA'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FTA'].transform('mean').astype(float)
|
71 |
+
season_long_table['FT_PCT'] = (gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FTM'].transform('sum').astype(int) /
|
72 |
+
gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FTA'].transform('sum').astype(int))
|
73 |
+
season_long_table['reboundChancesOffensive'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['reboundChancesOffensive'].transform('mean').astype(float)
|
74 |
+
season_long_table['OREB'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['OREB'].transform('mean').astype(float)
|
75 |
+
season_long_table['reboundChancesDefensive'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['reboundChancesDefensive'].transform('mean').astype(float)
|
76 |
+
season_long_table['DREB'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['DREB'].transform('mean').astype(float)
|
77 |
+
season_long_table['reboundChancesTotal'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['reboundChancesTotal'].transform('mean').astype(float)
|
78 |
+
season_long_table['REB'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['REB'].transform('mean').astype(float)
|
79 |
+
season_long_table['passes'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['passes'].transform('mean').astype(float)
|
80 |
+
season_long_table['secondaryAssists'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['secondaryAssists'].transform('mean').astype(float)
|
81 |
+
season_long_table['freeThrowAssists'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['freeThrowAssists'].transform('mean').astype(float)
|
82 |
+
season_long_table['assists'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['assists'].transform('mean').astype(float)
|
83 |
+
season_long_table['STL'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['STL'].transform('mean').astype(float)
|
84 |
+
season_long_table['BLK'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['BLK'].transform('mean').astype(float)
|
85 |
+
season_long_table['TOV'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['TOV'].transform('mean').astype(float)
|
86 |
+
season_long_table['PF'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['PF'].transform('mean').astype(float)
|
87 |
+
season_long_table['DD'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['DD'].transform('mean').astype(float)
|
88 |
+
season_long_table['TD'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['TD'].transform('mean').astype(float)
|
89 |
+
season_long_table['Fantasy'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['Fantasy'].transform('mean').astype(float)
|
90 |
+
season_long_table['FD_Fantasy'] = gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FD_Fantasy'].transform('mean').astype(float)
|
91 |
+
season_long_table['rebound%'] = (gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['REB'].transform('sum').astype(int) /
|
92 |
+
gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['reboundChancesTotal'].transform('sum').astype(int))
|
93 |
+
season_long_table['assists_per_pass'] = (gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['assists'].transform('sum').astype(int) /
|
94 |
+
gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['passes'].transform('sum').astype(int))
|
95 |
+
season_long_table['Fantasy_per_touch'] = (gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['Fantasy'].transform('sum').astype(int) /
|
96 |
+
gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['touches'].transform('sum').astype(int))
|
97 |
+
season_long_table['FD_Fantasy_per_touch'] = (gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['FD_Fantasy'].transform('sum').astype(int) /
|
98 |
+
gamelog_table.groupby(['PLAYER_NAME', 'SEASON_ID'], sort=False)['touches'].transform('sum').astype(int))
|
99 |
+
season_long_table = season_long_table.drop_duplicates(subset='PLAYER_NAME')
|
100 |
+
|
101 |
+
return gamelog_table, season_long_table
|
102 |
|
103 |
+
@st.cache_data(show_spinner=False)
|
104 |
+
def split_frame(input_df, rows):
|
105 |
+
df = [input_df.loc[i : i + rows - 1, :] for i in range(0, len(input_df), rows)]
|
106 |
+
return df
|
107 |
|
108 |
def convert_df_to_csv(df):
|
109 |
return df.to_csv().encode('utf-8')
|
110 |
|
111 |
+
gamelog_table, season_long_table = init_baselines()
|
112 |
|
113 |
col1, col2 = st.columns([1, 9])
|
114 |
with col1:
|
115 |
if st.button("Reset Data", key='reset1'):
|
116 |
st.cache_data.clear()
|
117 |
+
gamelog_table, season_long_table = init_baselines()
|
118 |
+
split_var1 = st.radio("What table would you like to view?", ('Season Long', 'Gamelogs'), key='split_var1')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
with col2:
|
121 |
+
if split_var1 == 'Season Logs':
|
122 |
+
st.dataframe(season_long_table.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
123 |
+
elif split_var1 == 'Gamelogs':
|
124 |
+
pagination = st.container()
|
125 |
+
|
126 |
+
bottom_menu = st.columns((4, 1, 1))
|
127 |
+
with bottom_menu[2]:
|
128 |
+
batch_size = st.selectbox("Page Size", options=[25, 50, 100])
|
129 |
+
with bottom_menu[1]:
|
130 |
+
total_pages = (
|
131 |
+
int(len(gamelog_table) / batch_size) if int(len(gamelog_table) / batch_size) > 0 else 1
|
132 |
+
)
|
133 |
+
current_page = st.number_input(
|
134 |
+
"Page", min_value=1, max_value=total_pages, step=1
|
135 |
+
)
|
136 |
+
with bottom_menu[0]:
|
137 |
+
st.markdown(f"Page **{current_page}** of **{total_pages}** ")
|
138 |
+
|
139 |
+
pages = split_frame(gamelog_table, batch_size)
|
140 |
+
pagination.dataframe(data=pages[current_page - 1], use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|