Multichem commited on
Commit
de86cd9
·
1 Parent(s): 17b5657

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +195 -0
app.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pulp
2
+ import numpy as np
3
+ import pandas as pd
4
+ import random
5
+ import sys
6
+ import openpyxl
7
+ import re
8
+ import time
9
+ import streamlit as st
10
+ import matplotlib
11
+ from matplotlib.colors import LinearSegmentedColormap
12
+ from st_aggrid import GridOptionsBuilder, AgGrid, GridUpdateMode, DataReturnMode
13
+ import json
14
+ import requests
15
+ import gspread
16
+ import plotly.figure_factory as ff
17
+
18
+ scope = ['https://www.googleapis.com/auth/spreadsheets',
19
+ "https://www.googleapis.com/auth/drive"]
20
+
21
+ credentials = {
22
+ "type": "service_account",
23
+ "project_id": "sheets-api-connect-378620",
24
+ "private_key_id": "1005124050c80d085e2c5b344345715978dd9cc9",
25
+ "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtKa01beXwc88R\nnPZVQTNPVQuBnbwoOfc66gW3547ja/UEyIGAF112dt/VqHprRafkKGmlg55jqJNt\na4zceLKV+wTm7vBu7lDISTJfGzCf2TrxQYNqwMKE2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JXM1eOOFFuZeQCaInqdPZy1UcV5Szla4UmUZWkk1m24blHzXAoGBAMcA\nyH4qdbxX9AYrC1dvsSRvgcnzytMvX05LU0uF6tzGtG0zVlub4ahvpEHCfNuy44UT\nxRWW/oFFaWjjyFxO5sWggpUqNuHEnRopg3QXx22SRRTGbN45li/+QAocTkgsiRh1\nqEcYZsO4mPCsQqAy6E2p6RcK+Xa+omxvSnVhq0x1AoGAKr8GdkCl4CF6rieLMAQ7\nLNBuuoYGaHoh8l5E2uOQpzwxVy/nMBcAv+2+KqHEzHryUv1owOi6pMLv7A9mTFoS\n18B0QRLuz5fSOsVnmldfC9fpUc6H8cH1SINZpzajqQA74bPwELJjnzrCnH79TnHG\nJuElxA33rFEjbgbzdyrE768=\n-----END PRIVATE KEY-----\n",
26
+ "client_email": "gspread-connection@sheets-api-connect-378620.iam.gserviceaccount.com",
27
+ "client_id": "106625872877651920064",
28
+ "auth_uri": "https://accounts.google.com/o/oauth2/auth",
29
+ "token_uri": "https://oauth2.googleapis.com/token",
30
+ "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
31
+ "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gspread-connection%40sheets-api-connect-378620.iam.gserviceaccount.com"
32
+ }
33
+
34
+ gc = gspread.service_account_from_dict(credentials)
35
+
36
+ st.set_page_config(layout="wide")
37
+
38
+ roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}',
39
+ '40+%': '{:.2%}','3x%': '{:.2%}','4x%': '{:.2%}','5x%': '{:.2%}','Own': '{:.2%}','LevX': '{:.2%}'}
40
+ stat_format = {'Odds%': '{:.2%}', 'Boosts': '{:.2%}'}
41
+
42
+ overall_table = 'LOL_Overall_Proj'
43
+ wins_table = 'LOL_Win_Proj'
44
+ losses_table = 'LOL_Loss_Proj'
45
+ stacks_table = 'https://docs.google.com/spreadsheets/d/10MVGsAHJPUAdK9SJ28ZqjgBgV2xBJSXEka-s2pIxHHE/edit?pli=1#gid=0'
46
+ bo1_player_stats = 'https://docs.google.com/spreadsheets/d/10MVGsAHJPUAdK9SJ28ZqjgBgV2xBJSXEka-s2pIxHHE/edit?pli=1#gid=0'
47
+ bo3_player_stats = 'https://docs.google.com/spreadsheets/d/10MVGsAHJPUAdK9SJ28ZqjgBgV2xBJSXEka-s2pIxHHE/edit?pli=1#gid=0'
48
+ bo5_player_stats = 'https://docs.google.com/spreadsheets/d/10MVGsAHJPUAdK9SJ28ZqjgBgV2xBJSXEka-s2pIxHHE/edit?pli=1#gid=0'
49
+
50
+ @st.cache_data
51
+ def load_roo_model(URL):
52
+ sh = gc.open(URL)
53
+ worksheet = sh.get_worksheet(0)
54
+ raw_display = pd.DataFrame(worksheet.get_all_records())
55
+ raw_display["Salary"] = raw_display["Salary"].replace("$", "", regex=True).astype(float)
56
+ raw_display['Top_finish'] = raw_display['Top_finish'].str.replace('%', '').astype(float)/100
57
+ raw_display['Top_5_finish'] = raw_display['Top_5_finish'].str.replace('%', '').astype(float)/100
58
+ raw_display['Top_10_finish'] = raw_display['Top_10_finish'].str.replace('%', '').astype(float)/100
59
+ raw_display['40+%'] = raw_display['40+%'].str.replace('%', '').astype(float)/100
60
+ raw_display['3x%'] = raw_display['3x%'].str.replace('%', '').astype(float)/100
61
+ raw_display['4x%'] = raw_display['4x%'].str.replace('%', '').astype(float)/100
62
+ raw_display['5x%'] = raw_display['5x%'].str.replace('%', '').astype(float)/100
63
+ raw_display['Own'] = raw_display['Own'].str.replace('%', '').astype(float)/100
64
+ raw_display['LevX'] = raw_display['LevX'].str.replace('%', '').astype(float)/100
65
+
66
+ return raw_display
67
+
68
+ @st.cache_data
69
+ def load_bo1_proj_model(URL):
70
+ sh = gc.open_by_url(URL)
71
+ worksheet = sh.get_worksheet(13)
72
+ raw_display = pd.DataFrame(worksheet.get_all_records())
73
+ raw_display.rename(columns={"Name": "Player"}, inplace = True)
74
+ raw_display['Odds%'] = raw_display['Odds%'].str.replace('%', '').astype(float)/100
75
+ raw_display['Boosts'] = raw_display['Boosts'].str.replace('%', '').astype(float)/100
76
+ raw_display = raw_display.loc[raw_display['Kills'] != '#DIV/0!']
77
+ raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
78
+ raw_display = raw_display.sort_values(by='Kills', ascending=False)
79
+
80
+ return raw_display
81
+
82
+ @st.cache_data
83
+ def load_bo3_proj_model(URL):
84
+ sh = gc.open_by_url(URL)
85
+ worksheet = sh.get_worksheet(14)
86
+ raw_display = pd.DataFrame(worksheet.get_all_records())
87
+ raw_display.rename(columns={"Name": "Player"}, inplace = True)
88
+ raw_display['Odds%'] = raw_display['Odds%'].str.replace('%', '').astype(float)/100
89
+ raw_display['Boosts'] = raw_display['Boosts'].str.replace('%', '').astype(float)/100
90
+ raw_display = raw_display.loc[raw_display['Kills'] != '#DIV/0!']
91
+ raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
92
+ raw_display = raw_display.sort_values(by='Kills', ascending=False)
93
+
94
+ return raw_display
95
+
96
+ @st.cache_data
97
+ def load_bo5_proj_model(URL):
98
+ sh = gc.open_by_url(URL)
99
+ worksheet = sh.get_worksheet(15)
100
+ raw_display = pd.DataFrame(worksheet.get_all_records())
101
+ raw_display.rename(columns={"Name": "Player"}, inplace = True)
102
+ raw_display['Odds%'] = raw_display['Odds%'].str.replace('%', '').astype(float)/100
103
+ raw_display['Boosts'] = raw_display['Boosts'].str.replace('%', '').astype(float)/100
104
+ raw_display = raw_display.loc[raw_display['Kills'] != '#DIV/0!']
105
+ raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
106
+ raw_display = raw_display.sort_values(by='Kills', ascending=False)
107
+
108
+ return raw_display
109
+
110
+ @st.cache_data
111
+ def load_stacks_table(URL):
112
+ sh = gc.open_by_url(URL)
113
+ worksheet = sh.get_worksheet(9)
114
+ raw_display = pd.DataFrame(worksheet.get_all_records())
115
+ raw_display = raw_display.sort_values(by='Stack+', ascending=False)
116
+
117
+ return raw_display
118
+
119
+ tab1, tab2, tab3 = st.tabs(["LOL Stacks Table", "LOL Range of Outcomes", "LOL Player Base Stats"])
120
+
121
+ def convert_df_to_csv(df):
122
+ return df.to_csv().encode('utf-8')
123
+
124
+ with tab1:
125
+ if st.button("Reset Data", key='reset1'):
126
+ # Clear values from *all* all in-memory and on-disk data caches:
127
+ # i.e. clear values from both square and cube
128
+ st.cache_data.clear()
129
+ hold_display = load_stacks_table(stacks_table)
130
+ display = hold_display.set_index('Team')
131
+ st.dataframe(display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
132
+ st.download_button(
133
+ label="Export Stacks",
134
+ data=convert_df_to_csv(display),
135
+ file_name='LOL_Stacks_export.csv',
136
+ mime='text/csv',
137
+ )
138
+
139
+ with tab2:
140
+ if st.button("Reset Data", key='reset2'):
141
+ # Clear values from *all* all in-memory and on-disk data caches:
142
+ # i.e. clear values from both square and cube
143
+ st.cache_data.clear()
144
+ model_choice = st.radio("What table would you like to display?", ('Overall', 'Wins', 'Losses'), key='roo_table')
145
+ pos_var1 = st.selectbox('View specific position?', options = ['All', 'TOP', 'JNG', 'MID', 'ADC', 'SUP'], key = 'roo_posvar')
146
+ team_var1 = st.multiselect('View specific team?', options = hold_display['Team'].unique(), key = 'roo_teamvar')
147
+ if model_choice == 'Overall':
148
+ hold_display = load_roo_model(overall_table)
149
+ elif model_choice == 'Wins':
150
+ hold_display = load_roo_model(wins_table)
151
+ elif model_choice == 'Losses':
152
+ hold_display = load_roo_model(losses_table)
153
+ display = hold_display.set_index('Player')
154
+ if team_var1:
155
+ display = display[display['Team'].isin(team_var1)]
156
+ if pos_var1 == 'All':
157
+ display = display
158
+ elif pos_var1 != 'All':
159
+ display = display[display['Position'].str.contains(pos_var1)]
160
+ st.dataframe(display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(roo_format, precision=2), use_container_width = True)
161
+ st.download_button(
162
+ label="Export Range of Outcomes",
163
+ data=convert_df_to_csv(display),
164
+ file_name='LOL_ROO_export.csv',
165
+ mime='text/csv',
166
+ )
167
+
168
+ with tab3:
169
+ if st.button("Reset Data", key='reset3'):
170
+ # Clear values from *all* all in-memory and on-disk data caches:
171
+ # i.e. clear values from both square and cube
172
+ st.cache_data.clear()
173
+ gametype_choice = st.radio("What format are the games being played?", ('Best of 1', 'Best of 3', 'Best of 5'), key='player_stats')
174
+ pos_var2 = st.selectbox('View specific position?', options = ['All', 'TOP', 'JNG', 'MID', 'ADC', 'SUP'], key = 'proj_posvar')
175
+ team_var2 = st.multiselect('View specific team?', options = hold_display['Team'].unique(), key = 'proj_teamvar')
176
+ if gametype_choice == 'Best of 1':
177
+ hold_display = load_bo1_proj_model(bo1_player_stats)
178
+ elif gametype_choice == 'Best of 3':
179
+ hold_display = load_bo3_proj_model(bo3_player_stats)
180
+ elif gametype_choice == 'Best of 5':
181
+ hold_display = load_bo5_proj_model(bo5_player_stats)
182
+ display = hold_display.set_index('Player')
183
+ if team_var2:
184
+ display = display[display['Team'].isin(team_var2)]
185
+ if pos_var2 == 'All':
186
+ display = display
187
+ elif pos_var2 != 'All':
188
+ display = display[display['Position'].str.contains(pos_var2)]
189
+ st.dataframe(display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(stat_format, precision=2), use_container_width = True)
190
+ st.download_button(
191
+ label="Export Baselines",
192
+ data=convert_df_to_csv(display),
193
+ file_name='LOL_Baselines_export.csv',
194
+ mime='text/csv',
195
+ )