James McCool commited on
Commit
69200bc
·
1 Parent(s): 80caf35

Add Streamlit app for NHL player statistics with MongoDB integration

Browse files
Files changed (3) hide show
  1. app.py +155 -0
  2. app.yaml +10 -0
  3. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 pulp
9
+ import numpy as np
10
+ import pandas as pd
11
+ import streamlit as st
12
+ import pymongo
13
+ from itertools import combinations
14
+
15
+ @st.cache_resource
16
+ def init_conn():
17
+ uri = st.secrets['mongo_uri']
18
+ client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
19
+ db = client["NHL_Database"]
20
+
21
+ return db
22
+
23
+ db = init_conn()
24
+
25
+ player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}',
26
+ '4x%': '{:.2%}'}
27
+
28
+ @st.cache_resource(ttl=200)
29
+ def player_stat_table():
30
+ collection = db["Player_Level_ROO"]
31
+ cursor = collection.find()
32
+ player_frame = pd.DataFrame(cursor)
33
+ player_frame = player_frame[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own',
34
+ 'Small Field Own%', 'Large Field Own%', 'Cash Own%', 'CPT_Own', 'Site', 'Type', 'Slate', 'player_id', 'timestamp']]
35
+
36
+ collection = db["Player_Lines_ROO"]
37
+ cursor = collection.find()
38
+ line_frame = pd.DataFrame(cursor)
39
+ line_frame = line_frame[['Player', 'SK1', 'SK2', 'SK3', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '50+%', '2x%', '3x%', '4x%', 'Own', 'Site', 'Type', 'Slate']]
40
+
41
+ collection = db["Player_Powerplay_ROO"]
42
+ cursor = collection.find()
43
+ pp_frame = pd.DataFrame(cursor)
44
+ pp_frame = pp_frame[['Player', 'SK1', 'SK2', 'SK3', 'SK4', 'SK5', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '75+%', '2x%', '3x%', '4x%', 'Own', 'Site', 'Type', 'Slate']]
45
+
46
+ timestamp = player_frame['timestamp'].values[0]
47
+
48
+ return player_frame, line_frame, pp_frame, timestamp
49
+
50
+ @st.cache_data
51
+ def convert_df_to_csv(df):
52
+ return df.to_csv().encode('utf-8')
53
+
54
+ player_frame, line_frame, pp_frame, timestamp = player_stat_table()
55
+ t_stamp = f"Last Update: " + str(timestamp) + f" CST"
56
+
57
+ tab1, tab2, tab3 = st.tabs(["Player Range of Outcomes", "Line Combo Range of Outcomes", "Power Play Range of Outcomes"])
58
+
59
+ with tab1:
60
+ col1, col2 = st.columns([1, 7])
61
+ with col1:
62
+ st.info(t_stamp)
63
+ if st.button("Load/Reset Data", key='reset1'):
64
+ st.cache_data.clear()
65
+ player_frame, line_frame, pp_frame, timestamp = player_stat_table()
66
+ t_stamp = f"Last Update: " + str(timestamp) + f" CST"
67
+ site_var1 = st.radio("What table would you like to display?", ('Draftkings', 'Fanduel'), key='site_var1')
68
+ main_var1 = st.radio("Main slate or secondary slate?", ('Main Slate', 'Secondary Slate'), key='main_var1')
69
+ split_var1 = st.radio("Would you like to view the whole slate or just specific games?", ('Full Slate Run', 'Specific Games'), key='split_var1')
70
+ if split_var1 == 'Specific Games':
71
+ team_var1 = st.multiselect('Which teams would you like to include in the ROO?', options = player_frame['Team'].unique(), key='team_var1')
72
+ elif split_var1 == 'Full Slate Run':
73
+ team_var1 = player_frame.Team.values.tolist()
74
+ pos_split1 = st.radio("Are you viewing all positions, specific groups, or specific positions?", ('All Positions', 'Specific Positions'), key='pos_split1')
75
+ if pos_split1 == 'Specific Positions':
76
+ pos_var1 = st.multiselect('What Positions would you like to view?', options = ['C', 'W', 'D', 'G'])
77
+ elif pos_split1 == 'All Positions':
78
+ pos_var1 = 'All'
79
+ sal_var1 = st.slider("Is there a certain price range you want to view?", 2000, 10000, (2000, 20000), key='sal_var1')
80
+
81
+ with col2:
82
+ final_Proj = player_frame[player_frame['Site'] == str(site_var1)]
83
+ final_Proj = final_Proj[final_Proj['Type'] == 'Basic']
84
+ final_Proj = final_Proj[final_Proj['Slate'] == main_var1]
85
+ final_Proj = final_Proj[player_frame['Team'].isin(team_var1)]
86
+ final_Proj = final_Proj[final_Proj['Salary'] >= sal_var1[0]]
87
+ final_Proj = final_Proj[final_Proj['Salary'] <= sal_var1[1]]
88
+ if pos_var1 != 'All':
89
+ final_Proj = final_Proj[final_Proj['Position'].str.contains('|'.join(pos_var1))]
90
+ final_Proj = final_Proj.sort_values(by='Median', ascending=False)
91
+ if pos_var1 == 'All':
92
+ final_Proj = final_Proj.sort_values(by='Median', ascending=False)
93
+ st.dataframe(final_Proj.iloc[:, :-3].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), use_container_width = True)
94
+ st.download_button(
95
+ label="Export Tables",
96
+ data=convert_df_to_csv(final_Proj),
97
+ file_name='NHL_player_export.csv',
98
+ mime='text/csv',
99
+ )
100
+
101
+ with tab2:
102
+ col1, col2 = st.columns([1, 7])
103
+ with col1:
104
+ st.info(t_stamp)
105
+ if st.button("Load/Reset Data", key='reset2'):
106
+ st.cache_data.clear()
107
+ player_frame, line_frame, pp_frame, timestamp = player_stat_table()
108
+ t_stamp = f"Last Update: " + str(timestamp) + f" CST"
109
+ site_var2 = st.radio("What table would you like to display?", ('Draftkings', 'Fanduel'), key='site_var2')
110
+ main_var2 = st.radio("Main slate or secondary slate?", ('Main Slate', 'Secondary Slate'), key='main_var2')
111
+ sal_var2 = st.slider("Is there a certain price range you want to view?", 5000, 40000, (5000, 40000), key='sal_var2')
112
+
113
+ with col2:
114
+ final_line_combos = line_frame[line_frame['Site'] == str(site_var2)]
115
+ final_line_combos = final_line_combos[final_line_combos['Type'] == 'Basic']
116
+ final_line_combos = final_line_combos[final_line_combos['Slate'] == main_var2]
117
+ final_line_combos = final_line_combos[final_line_combos['Salary'] >= sal_var2[0]]
118
+ final_line_combos = final_line_combos[final_line_combos['Salary'] <= sal_var2[1]]
119
+ final_line_combos = final_line_combos.drop_duplicates(subset=['Player'])
120
+ final_line_combos = final_line_combos.sort_values(by='Median', ascending=False)
121
+ st.dataframe(final_line_combos.iloc[:, :-3].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
122
+ st.download_button(
123
+ label="Export Tables",
124
+ data=convert_df_to_csv(final_line_combos),
125
+ file_name='NHL_linecombos_export.csv',
126
+ mime='text/csv',
127
+ )
128
+
129
+ with tab3:
130
+ col1, col2 = st.columns([1, 7])
131
+ with col1:
132
+ st.info(t_stamp)
133
+ if st.button("Load/Reset Data", key='reset3'):
134
+ st.cache_data.clear()
135
+ player_frame, line_frame, pp_frame, timestamp = player_stat_table()
136
+ t_stamp = f"Last Update: " + str(timestamp) + f" CST"
137
+ site_var3 = st.radio("What table would you like to display?", ('Draftkings', 'Fanduel'), key='site_var3')
138
+ main_var3 = st.radio("Main slate or secondary slate?", ('Main Slate', 'Secondary Slate'), key='main_var3')
139
+ sal_var3 = st.slider("Is there a certain price range you want to view?", 5000, 40000, (5000, 40000), key='sal_var3')
140
+
141
+ with col2:
142
+ final_pp_combos = pp_frame[pp_frame['Site'] == str(site_var3)]
143
+ final_pp_combos = final_pp_combos[final_pp_combos['Type'] == 'Basic']
144
+ final_pp_combos = final_pp_combos[final_pp_combos['Slate'] == main_var3]
145
+ final_pp_combos = final_pp_combos[final_pp_combos['Salary'] >= sal_var3[0]]
146
+ final_pp_combos = final_pp_combos[final_pp_combos['Salary'] <= sal_var3[1]]
147
+ final_pp_combos = final_pp_combos.drop_duplicates(subset=['Player'])
148
+ final_pp_combos = final_pp_combos.sort_values(by='Median', ascending=False)
149
+ st.dataframe(final_pp_combos.iloc[:, :-3].style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(precision=2), use_container_width = True)
150
+ st.download_button(
151
+ label="Export Tables",
152
+ data=convert_df_to_csv(final_pp_combos),
153
+ file_name='NHL_powerplay_export.csv',
154
+ mime='text/csv',
155
+ )
app.yaml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ runtime: python
2
+ env: flex
3
+
4
+ runtime_config:
5
+ python_version: 3
6
+
7
+ entrypoint: streamlit run streamlit-app.py --server.port $PORT
8
+
9
+ automatic_scaling:
10
+ max_num_instances: 200
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ gspread
3
+ openpyxl
4
+ matplotlib
5
+ streamlit-aggrid
6
+ pulp
7
+ docker
8
+ plotly
9
+ scipy
10
+ pymongo