Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -34,146 +34,66 @@ def init_conn():
|
|
34 |
|
35 |
gcservice_account = init_conn()
|
36 |
|
37 |
-
|
38 |
|
39 |
-
percentages_format = {'
|
40 |
-
'
|
41 |
-
'Team FPPM Boost': '{:.2%}'}
|
42 |
|
43 |
@st.cache_resource(ttl = 600)
|
44 |
def init_baselines():
|
45 |
-
sh = gcservice_account.open_by_url(
|
46 |
|
47 |
-
worksheet = sh.worksheet('
|
48 |
raw_display = pd.DataFrame(worksheet.get_values())
|
49 |
raw_display.columns = raw_display.iloc[0]
|
50 |
raw_display = raw_display[1:]
|
51 |
raw_display = raw_display.reset_index(drop=True)
|
52 |
-
matchups = raw_display[raw_display['
|
|
|
|
|
53 |
matchups_dict = dict(zip(matchups['Team'], matchups['Opp']))
|
54 |
|
55 |
-
worksheet = sh.worksheet('
|
56 |
raw_display = pd.DataFrame(worksheet.get_values())
|
57 |
raw_display.columns = raw_display.iloc[0]
|
58 |
raw_display = raw_display[1:]
|
59 |
raw_display = raw_display.reset_index(drop=True)
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
|
66 |
-
|
67 |
-
raw_display = pd.DataFrame(worksheet.get_values())
|
68 |
-
raw_display.columns = raw_display.iloc[0]
|
69 |
-
raw_display = raw_display[1:]
|
70 |
-
raw_display = raw_display.reset_index(drop=True)
|
71 |
-
cols_to_check = ['Pts% Boost', 'Reb% Boost', 'Ast% Boost', '3p% Boost', 'Stl Boost%', 'Blk Boost%', 'TOV Boost%', 'FPPM Boost']
|
72 |
-
raw_display.loc[:, cols_to_check] = raw_display.loc[:, cols_to_check].replace({'%': ''}, regex=True).astype(float) / 100
|
73 |
-
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
74 |
-
raw_display['position'] = 'Shooting Guard'
|
75 |
-
sg_dem = raw_display[raw_display['Acro'] != ""]
|
76 |
-
|
77 |
-
worksheet = sh.worksheet('SF_DEM_Calc')
|
78 |
-
raw_display = pd.DataFrame(worksheet.get_values())
|
79 |
-
raw_display.columns = raw_display.iloc[0]
|
80 |
-
raw_display = raw_display[1:]
|
81 |
-
raw_display = raw_display.reset_index(drop=True)
|
82 |
-
cols_to_check = ['Pts% Boost', 'Reb% Boost', 'Ast% Boost', '3p% Boost', 'Stl Boost%', 'Blk Boost%', 'TOV Boost%', 'FPPM Boost']
|
83 |
-
raw_display.loc[:, cols_to_check] = raw_display.loc[:, cols_to_check].replace({'%': ''}, regex=True).astype(float) / 100
|
84 |
-
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
85 |
-
raw_display['position'] = 'Small Forward'
|
86 |
-
sf_dem = raw_display[raw_display['Acro'] != ""]
|
87 |
-
|
88 |
-
worksheet = sh.worksheet('PF_DEM_Calc')
|
89 |
-
raw_display = pd.DataFrame(worksheet.get_values())
|
90 |
-
raw_display.columns = raw_display.iloc[0]
|
91 |
-
raw_display = raw_display[1:]
|
92 |
-
raw_display = raw_display.reset_index(drop=True)
|
93 |
-
cols_to_check = ['Pts% Boost', 'Reb% Boost', 'Ast% Boost', '3p% Boost', 'Stl Boost%', 'Blk Boost%', 'TOV Boost%', 'FPPM Boost']
|
94 |
-
raw_display.loc[:, cols_to_check] = raw_display.loc[:, cols_to_check].replace({'%': ''}, regex=True).astype(float) / 100
|
95 |
-
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
96 |
-
raw_display['position'] = 'Power Forward'
|
97 |
-
pf_dem = raw_display[raw_display['Acro'] != ""]
|
98 |
-
|
99 |
-
worksheet = sh.worksheet('C_DEM_Calc')
|
100 |
-
raw_display = pd.DataFrame(worksheet.get_values())
|
101 |
-
raw_display.columns = raw_display.iloc[0]
|
102 |
-
raw_display = raw_display[1:]
|
103 |
-
raw_display = raw_display.reset_index(drop=True)
|
104 |
-
cols_to_check = ['Pts% Boost', 'Reb% Boost', 'Ast% Boost', '3p% Boost', 'Stl Boost%', 'Blk Boost%', 'TOV Boost%', 'FPPM Boost']
|
105 |
-
raw_display.loc[:, cols_to_check] = raw_display.loc[:, cols_to_check].replace({'%': ''}, regex=True).astype(float) / 100
|
106 |
-
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
107 |
-
raw_display['position'] = 'Center'
|
108 |
-
c_dem = raw_display[raw_display['Acro'] != ""]
|
109 |
-
|
110 |
-
overall_dem = pd.concat([pg_dem, sg_dem, sf_dem, pf_dem, c_dem])
|
111 |
-
overall_dem = overall_dem[['Acro', 'G', 'Pts% Boost', 'Reb% Boost', 'Ast% Boost', '3p% Boost',
|
112 |
-
'Stl Boost%', 'Blk Boost%', 'TOV Boost%', 'FPPM', 'FPPM Boost', 'position']]
|
113 |
-
overall_dem['Team'] = overall_dem['Acro'] + '-' + overall_dem['position']
|
114 |
-
overall_dem['Team FPPM Boost'] = overall_dem.groupby('Acro', sort=False)['FPPM Boost'].transform('mean')
|
115 |
-
overall_dem = overall_dem.reset_index()
|
116 |
-
|
117 |
-
|
118 |
-
export_dem = overall_dem[['Team', 'Acro', 'G', 'Pts% Boost', 'Reb% Boost', 'Ast% Boost', '3p% Boost',
|
119 |
-
'Stl Boost%', 'Blk Boost%', 'TOV Boost%', 'FPPM', 'FPPM Boost', 'Team FPPM Boost', 'position']]
|
120 |
-
|
121 |
-
return export_dem, matchups, matchups_dict
|
122 |
|
123 |
def convert_df_to_csv(df):
|
124 |
return df.to_csv().encode('utf-8')
|
125 |
|
126 |
-
|
127 |
|
128 |
col1, col2 = st.columns([1, 9])
|
129 |
with col1:
|
130 |
if st.button("Reset Data", key='reset1'):
|
131 |
st.cache_data.clear()
|
132 |
-
|
133 |
-
split_var1 = st.radio("View
|
134 |
-
|
135 |
-
view_var1 = matchups.Opp.values.tolist()
|
136 |
-
split_var2 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var2')
|
137 |
-
if split_var2 == 'Specific Teams':
|
138 |
-
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = view_var1, key='team_var1')
|
139 |
-
elif split_var2 == 'All':
|
140 |
-
team_var1 = view_var1
|
141 |
-
split_var3 = st.radio("Would you like to view all positions or specific ones?", ('All', 'Specific Positions'), key='split_var3')
|
142 |
-
if split_var3 == 'Specific Positions':
|
143 |
-
pos_var1 = st.multiselect('Which teams would you like to include in the tables?', options = overall_dem['position'].unique(), key='pos_var1')
|
144 |
-
elif split_var3 == 'All':
|
145 |
-
pos_var1 = overall_dem.position.values.tolist()
|
146 |
-
if split_var1 == 'All':
|
147 |
-
split_var2 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var2')
|
148 |
-
if split_var2 == 'Specific Teams':
|
149 |
-
team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = overall_dem['Acro'].unique(), key='team_var1')
|
150 |
-
elif split_var2 == 'All':
|
151 |
-
team_var1 = overall_dem.Acro.values.tolist()
|
152 |
-
split_var3 = st.radio("Would you like to view all positions or specific ones?", ('All', 'Specific Positions'), key='split_var3')
|
153 |
-
if split_var3 == 'Specific Positions':
|
154 |
-
pos_var1 = st.multiselect('Which teams would you like to include in the tables?', options = overall_dem['position'].unique(), key='pos_var1')
|
155 |
-
elif split_var3 == 'All':
|
156 |
-
pos_var1 = overall_dem.position.values.tolist()
|
157 |
with col2:
|
158 |
if split_var1 == 'Slate Matchups':
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
elif split_var1 == '
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
data=convert_df_to_csv(overall_dem),
|
177 |
-
file_name='DEM_export.csv',
|
178 |
-
mime='text/csv',
|
179 |
-
)
|
|
|
34 |
|
35 |
gcservice_account = init_conn()
|
36 |
|
37 |
+
NHL_data = 'https://docs.google.com/spreadsheets/d/1NmKa-b-2D3w7rRxwMPSchh31GKfJ1XcDI2GU8rXWnHI/edit#gid=811139250'
|
38 |
|
39 |
+
percentages_format = {'Shots': '{:.2%}', 'HDCF': '{:.2%}', 'Goals': '{:.2%}', 'Assists': '{:.2%}', 'Blocks': '{:.2%}',
|
40 |
+
'L14_Shots': '{:.2%}', 'L14_HDCF': '{:.2%}', 'L14_Goals': '{:.2%}', 'L14_Assists': '{:.2%}', 'L14_Blocks': '{:.2%}'}
|
|
|
41 |
|
42 |
@st.cache_resource(ttl = 600)
|
43 |
def init_baselines():
|
44 |
+
sh = gcservice_account.open_by_url(NHL_data)
|
45 |
|
46 |
+
worksheet = sh.worksheet('Matchups')
|
47 |
raw_display = pd.DataFrame(worksheet.get_values())
|
48 |
raw_display.columns = raw_display.iloc[0]
|
49 |
raw_display = raw_display[1:]
|
50 |
raw_display = raw_display.reset_index(drop=True)
|
51 |
+
matchups = raw_display[raw_display['Opp'] != ""]
|
52 |
+
data_cols = matchups.columns.drop(['Team', 'Opp'])
|
53 |
+
matchups[data_cols] = matchups[data_cols].apply(pd.to_numeric, errors='coerce')
|
54 |
matchups_dict = dict(zip(matchups['Team'], matchups['Opp']))
|
55 |
|
56 |
+
worksheet = sh.worksheet('Marketshares')
|
57 |
raw_display = pd.DataFrame(worksheet.get_values())
|
58 |
raw_display.columns = raw_display.iloc[0]
|
59 |
raw_display = raw_display[1:]
|
60 |
raw_display = raw_display.reset_index(drop=True)
|
61 |
+
raw_display = raw_display[raw_display['Line'] != ""]
|
62 |
+
overall_ms = raw_display[['Line', 'SK1', 'SK2', 'SK3', 'Cost', 'Team Total', 'Shots', 'HDCF', 'Goals', 'Assists', 'Blocks',
|
63 |
+
'L14_Shots', 'L14_HDCF', 'L14_Goals', 'L14_Assists', 'L14_Blocks']]
|
64 |
+
data_cols = overall_ms.columns.drop(['Line', 'SK1', 'SK2', 'SK3'])
|
65 |
+
overall_ms[data_cols] = overall_ms[data_cols].apply(pd.to_numeric, errors='coerce')
|
66 |
|
67 |
+
return matchups, matchups_dict, overall_ms
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
def convert_df_to_csv(df):
|
70 |
return df.to_csv().encode('utf-8')
|
71 |
|
72 |
+
matchups, matchups_dict, overall_ms = init_baselines()
|
73 |
|
74 |
col1, col2 = st.columns([1, 9])
|
75 |
with col1:
|
76 |
if st.button("Reset Data", key='reset1'):
|
77 |
st.cache_data.clear()
|
78 |
+
matchups, matchups_dict, overall_ms = init_baselines()
|
79 |
+
split_var1 = st.radio("View matchups or line marketshares?", ('Slate Matchups', 'Line Marketshares'), key='split_var1')
|
80 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
with col2:
|
82 |
if split_var1 == 'Slate Matchups':
|
83 |
+
display_table = matchups
|
84 |
+
st.dataframe(display_table.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
85 |
+
st.download_button(
|
86 |
+
label="Export Matchups",
|
87 |
+
data=convert_df_to_csv(display_table),
|
88 |
+
file_name='Matchups_export.csv',
|
89 |
+
mime='text/csv',
|
90 |
+
)
|
91 |
+
elif split_var1 == 'Line Marketshares':
|
92 |
+
display_table = overall_ms
|
93 |
+
st.dataframe(display_table.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(percentages_format, precision=2), use_container_width = True)
|
94 |
+
st.download_button(
|
95 |
+
label="Export Marketshares",
|
96 |
+
data=convert_df_to_csv(display_table),
|
97 |
+
file_name='Marketshares_export.csv',
|
98 |
+
mime='text/csv',
|
99 |
+
)
|
|
|
|
|
|
|
|