Multichem commited on
Commit
5ebc6ac
·
1 Parent(s): f24962f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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": "dfsnew",
23
+ "private_key_id": "2432f6c3771f70a410c5c878d1359869fc9dddc8",
24
+ "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDBNBDU2aJuEr6n\ne0o7pDY8gjg1+g1e3oHlpyY/CHMByZuEwfXewsZYP/TApfr8zxXDNG9X31CloWXH\n6ef8H0h6TjhRppE/2YCUZlbgtvpwlDg+1aKTKY5Lc/L937I6V512mgMDhDmTwX+p\noV0vhPuJnyFy+Fuo+xu8D9A46lhTTIK4EZhHc04SUBxUI3pDdfvuMbjciD/Pskn2\nMwBSEG/FQoe4GYrSmm7jzYdSHItVBakr26xl117m8BrIuceU7IEWrnJGDza8TtTZ\n+4Wp7PY9v6DgVt2+rnnDaF/g7k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Z0wV2+xvuvBz7h+ni8\nQQphFxoEhcBRq7ys1h6ebt+86mQW1ne4aRjWbKxBAoGARA+rBNIC9Z1vyRzMAXfj\nnQ9/wShd/NGpVRNrm7sdUastfoyK8Ip3HkJac3xE1ARpQTvxAz742mdeDxPWI8wZ\nHDsjIrRqGLKMN7tSIoM720y6PY/Tsg89SdY4y0h6M75rrEi4Lv5b7s4EmqAZdfKT\nbEyuT7sCPCLeOX/RLy/lCpA=\n-----END PRIVATE KEY-----\n",
25
+ "client_email": "[email protected]",
26
+ "client_id": "105107448378741046480",
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/dfsapps%40dfsnew.iam.gserviceaccount.com",
31
+ "universe_domain": "googleapis.com"
32
+ }
33
+
34
+ header= {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) '
35
+ 'AppleWebKit/537.11 (KHTML, like Gecko) '
36
+ 'Chrome/23.0.1271.64 Safari/537.11',
37
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
38
+ 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
39
+ 'Accept-Encoding': 'none',
40
+ 'Accept-Language': 'en-US,en;q=0.8',
41
+ 'Connection': 'keep-alive'}
42
+
43
+ gc_con = gspread.service_account_from_dict(credentials, scope)
44
+
45
+ return gc_con
46
+
47
+ gcservice_account = init_conn()
48
+
49
+ NBAGetGameData = 'https://docs.google.com/spreadsheets/d/1tRQrF_I5rS7Q0g9vE8NrENDZ2P3_DvtbBZzKEakwOI0/edit#gid=1373653837'
50
+ NBABettingModel = 'https://docs.google.com/spreadsheets/d/1WBnvOHQi_zVTGF63efejK5ho02AY00HiYrMHnMJXY1E/edit#gid=1157978351'
51
+
52
+ @st.cache_resource(ttl = 300)
53
+ def init_baselines():
54
+ sh = gcservice_account.open_by_url(NBAGetGameData)
55
+
56
+ worksheet = sh.worksheet('MinPublic')
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.replace('', np.nan, inplace=True)
62
+ raw_display = raw_display[['NBAID', 'PID', 'Player', 'TC', 'MP (Today)', 'Next Game', 'H/R', 'Injury Notes', 'Player Impact per 48', 'Player Impact',
63
+ 'Team PM', 'Last Updated']]
64
+ raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
65
+ public_minutes = raw_display[raw_display['NBAID'] != ""]
66
+
67
+ sh = gcservice_account.open_by_url(NBABettingModel)
68
+
69
+ worksheet = sh.worksheet('PlayerImpactByTeam')
70
+ raw_display = pd.DataFrame(worksheet.get_values())
71
+ raw_display.columns = raw_display.iloc[0]
72
+ raw_display = raw_display[1:]
73
+ raw_display = raw_display.reset_index(drop=True)
74
+ raw_display.replace('', 0, inplace=True)
75
+ raw_display = raw_display[['PID', 'Player', 'Team', 'Avg Minutes last 30 days for team', 'Minutes Projection', 'Rotation Impact (versus last 30 days)',
76
+ 'Injury Notes', 'Minute Change', 'Baseline Team PM', 'Net Rotation PM +/- for Team', 'Projected PM for Game', 'Offset', 'Rank']]
77
+ raw_display['Minute Change'].replace('+', '', inplace=True)
78
+ raw_display = raw_display.apply(pd.to_numeric, errors='coerce').fillna(raw_display)
79
+ player_impact = raw_display[raw_display['PID'] != ""]
80
+
81
+ return public_minutes, player_impact
82
+
83
+ def convert_df_to_csv(df):
84
+ return df.to_csv().encode('utf-8')
85
+
86
+ public_minutes, player_impact = init_baselines()
87
+
88
+ tab1, tab2 = st.tabs(["Minutes Baselines", "Player Impacts"])
89
+
90
+ with tab1:
91
+ if st.button("Reset Data", key='reset1'):
92
+ st.cache_data.clear()
93
+ public_minutes, player_impact = init_baselines()
94
+ split_var1 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var1')
95
+ if split_var1 == 'Specific Teams':
96
+ team_var1 = st.multiselect('Which teams would you like to include in the tables?', options = public_minutes['TC'].unique(), key='team_var1')
97
+ elif split_var1 == 'All':
98
+ team_var1 = public_minutes.TC.values.tolist()
99
+ public_minutes = public_minutes[public_minutes['TC'].isin(team_var1)]
100
+ player_min_disp = public_minutes.set_index('Player')
101
+ player_min_disp = player_min_disp.sort_values(by=['TC', 'MP (Today)'], ascending=[True, False])
102
+ st.dataframe(player_min_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
103
+ st.download_button(
104
+ label="Export Minutes Baselines",
105
+ data=convert_df_to_csv(public_minutes),
106
+ file_name='AmericanNumbers_Min_Baseline_export.csv',
107
+ mime='text/csv',
108
+ )
109
+
110
+ with tab2:
111
+ if st.button("Reset Data", key='reset2'):
112
+ st.cache_data.clear()
113
+ public_minutes, player_impact = init_baselines()
114
+ split_var2 = st.radio("Would you like to view all teams or specific ones?", ('All', 'Specific Teams'), key='split_var2')
115
+ if split_var2 == 'Specific Teams':
116
+ team_var2 = st.multiselect('Which teams would you like to include in the tables?', options = player_impact['Team'].unique(), key='team_var2')
117
+ elif split_var2 == 'All':
118
+ team_var2 = player_impact.Team.values.tolist()
119
+ player_impact = player_impact[player_impact['Team'].isin(team_var2)]
120
+ player_impact_disp = player_impact.set_index('Player')
121
+ player_impact_disp = player_impact_disp.sort_values(by=['Team', 'Rotation Impact (versus last 30 days)'], ascending=[True, False])
122
+ st.dataframe(player_impact_disp.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
123
+ st.download_button(
124
+ label="Export Player Impacts",
125
+ data=convert_df_to_csv(player_impact),
126
+ file_name='AmericanNumbers_Impact_export.csv',
127
+ mime='text/csv',
128
+ )