Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
st.set_page_config(layout="wide")
|
3 |
+
|
4 |
+
for name in dir():
|
5 |
+
if not name.startswith('_'):
|
6 |
+
del globals()[name]
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import pandas as pd
|
10 |
+
import streamlit as st
|
11 |
+
import gspread
|
12 |
+
import plotly.express as px
|
13 |
+
import random
|
14 |
+
import gc
|
15 |
+
|
16 |
+
@st.cache_resource
|
17 |
+
def init_conn():
|
18 |
+
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
19 |
+
|
20 |
+
credentials = {
|
21 |
+
"type": "service_account",
|
22 |
+
"project_id": "model-sheets-connect",
|
23 |
+
"private_key_id": "0e0bc2fdef04e771172fe5807392b9d6639d945e",
|
24 |
+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDiu1v/e6KBKOcK\nc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RtKD8lKDupJEYqSrseRbK/ax43DDITS77/DWwHl33D3FYC\nMblUm8ygwxQpR4VUfwDpYXBlklWcJovzamXpSnsfcYVkkQH47NuOXPXPkXQsw+w+\nDYcJzeu7F/vZqk9I7oBkWHUrrik9zPNoUzrfPvSRGtkAoTDSwibhoc5dAoGBAMHE\nK0T/ANeZQLNuzQps6S7G4eqjwz5W8qeeYxsdZkvWThOgDd/ewt3ijMnJm5X05hOn\ni4XF1euTuvUl7wbqYx76Wv3/1ZojiNNgy7ie4rYlyB/6vlBS97F4ZxJdxMlabbCW\n6b3EMWa4EVVXKoA1sCY7IVDE+yoQ1JYsZmq45YzPAoGBANWWHuVueFGZRDZlkNlK\nh5OmySmA0NdNug3G1upaTthyaTZ+CxGliwBqMHAwpkIRPwxUJpUwBTSEGztGTAxs\nWsUOVWlD2/1JaKSmHE8JbNg6sxLilcG6WEDzxjC5dLL1OrGOXj9WhC9KX3sq6qb6\nF/j9eUXfXjAlb042MphoF3ZC\n-----END PRIVATE KEY-----\n",
|
25 |
+
"client_email": "[email protected]",
|
26 |
+
"client_id": "100369174533302798535",
|
27 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
28 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
29 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
30 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40model-sheets-connect.iam.gserviceaccount.com"
|
31 |
+
}
|
32 |
+
|
33 |
+
gc_con = gspread.service_account_from_dict(credentials, scope)
|
34 |
+
|
35 |
+
return gc_con
|
36 |
+
|
37 |
+
gcservice_account = init_conn()
|
38 |
+
|
39 |
+
DEM_data = 'https://docs.google.com/spreadsheets/d/1Yq0vGriWK-bS79e-bD6_u9pqrYE6Yrlbb_wEkmH-ot0/edit#gid=1808117109'
|
40 |
+
|
41 |
+
@st.cache_resource(ttl = 600)
|
42 |
+
def init_baselines():
|
43 |
+
sh = gcservice_account.open_by_url(DEM_data)
|
44 |
+
|
45 |
+
worksheet = sh.worksheet('PG_DEM_Calc')
|
46 |
+
raw_display = pd.DataFrame(worksheet.get_values())
|
47 |
+
raw_display.columns = raw_display.iloc[0]
|
48 |
+
raw_display = raw_display[1:]
|
49 |
+
raw_display = raw_display.reset_index(drop=True)
|
50 |
+
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
51 |
+
raw_display['position'] = 'Point Guard'
|
52 |
+
pg_dem = raw_display[raw_display['Acro'] != ""]
|
53 |
+
|
54 |
+
worksheet = sh.worksheet('SG_DEM_Calc')
|
55 |
+
raw_display = pd.DataFrame(worksheet.get_values())
|
56 |
+
raw_display.columns = raw_display.iloc[0]
|
57 |
+
raw_display = raw_display[1:]
|
58 |
+
raw_display = raw_display.reset_index(drop=True)
|
59 |
+
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
60 |
+
raw_display['position'] = 'Shooting Guard'
|
61 |
+
sg_dem = raw_display[raw_display['Acro'] != ""]
|
62 |
+
|
63 |
+
worksheet = sh.worksheet('SF_DEM_Calc')
|
64 |
+
raw_display = pd.DataFrame(worksheet.get_values())
|
65 |
+
raw_display.columns = raw_display.iloc[0]
|
66 |
+
raw_display = raw_display[1:]
|
67 |
+
raw_display = raw_display.reset_index(drop=True)
|
68 |
+
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
69 |
+
raw_display['position'] = 'Small Forward'
|
70 |
+
sf_dem = raw_display[raw_display['Acro'] != ""]
|
71 |
+
|
72 |
+
worksheet = sh.worksheet('PF_DEM_Calc')
|
73 |
+
raw_display = pd.DataFrame(worksheet.get_values())
|
74 |
+
raw_display.columns = raw_display.iloc[0]
|
75 |
+
raw_display = raw_display[1:]
|
76 |
+
raw_display = raw_display.reset_index(drop=True)
|
77 |
+
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
78 |
+
raw_display['position'] = 'Power Forward'
|
79 |
+
pf_dem = raw_display[raw_display['Acro'] != ""]
|
80 |
+
|
81 |
+
worksheet = sh.worksheet('C_DEM_Calc')
|
82 |
+
raw_display = pd.DataFrame(worksheet.get_values())
|
83 |
+
raw_display.columns = raw_display.iloc[0]
|
84 |
+
raw_display = raw_display[1:]
|
85 |
+
raw_display = raw_display.reset_index(drop=True)
|
86 |
+
raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
|
87 |
+
raw_display['position'] = 'Center'
|
88 |
+
c_dem = raw_display[raw_display['Acro'] != ""]
|
89 |
+
|
90 |
+
overall_dem = pd.concat([pg_dem, sg_dem, sf_dem, pf_dem, c_dem])
|
91 |
+
overall_dem = overall_dem[['Acro', 'G', 'Pts% Boost', 'Reb% Boost', 'Ast% Boost', '3p% Boost',
|
92 |
+
'Stl Boost%', 'Blk Boost%', 'TOV Boost%', 'FPPM', 'FPPM Boost', 'position']]
|
93 |
+
|
94 |
+
return overall_dem
|
95 |
+
|
96 |
+
def convert_df_to_csv(df):
|
97 |
+
return df.to_csv().encode('utf-8')
|
98 |
+
|
99 |
+
overall_dem = init_baselines()
|
100 |
+
|
101 |
+
if st.button("Reset Data", key='reset1'):
|
102 |
+
st.cache_data.clear()
|
103 |
+
overall_dem = init_baselines()
|
104 |
+
# split_var1 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var1')
|
105 |
+
# if split_var1 == 'Specific Teams':
|
106 |
+
# team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = public_minutes['TC'].unique(), key='team_var1')
|
107 |
+
# elif split_var1 == 'All':
|
108 |
+
# team_var1 = public_minutes.TC.values.tolist()
|
109 |
+
# public_minutes = public_minutes[public_minutes['TC'].isin(team_var1)]
|
110 |
+
# player_min_disp = public_minutes.set_index('Player')
|
111 |
+
# player_min_disp = player_min_disp.sort_values(by=['TC', 'MP (Today)'], ascending=[True, False])
|
112 |
+
st.dataframe(overall_dem.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
|
113 |
+
st.download_button(
|
114 |
+
label="Export Minutes Baselines",
|
115 |
+
data=convert_df_to_csv(overall_dem),
|
116 |
+
file_name='DEM_export.csv',
|
117 |
+
mime='text/csv',
|
118 |
+
)
|