James McCool commited on
Commit
a47b0a6
·
1 Parent(s): 9128800

Add WNBA database support in app.py: modify connection initialization and update overall stats retrieval to handle both NBA and WNBA data, enhancing functionality for player statistics management.

Browse files
Files changed (1) hide show
  1. app.py +40 -16
app.py CHANGED
@@ -13,10 +13,11 @@ def init_conn():
13
  uri = st.secrets['mongo_uri']
14
  client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
15
  db = client["NBA_DFS"]
 
16
 
17
- return db
18
 
19
- db = init_conn()
20
 
21
  dk_columns = ['PG', 'SG', 'SF', 'PF', 'C', 'G', 'F', 'FLEX', 'salary', 'proj', 'Team', 'Team_count', 'Secondary', 'Secondary_count', 'Own']
22
  dk_sd_columns = ['CPT', 'FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5', 'salary', 'proj', 'Team', 'Team_count', 'Secondary', 'Secondary_count', 'Own']
@@ -58,8 +59,11 @@ st.markdown("""
58
  </style>""", unsafe_allow_html=True)
59
 
60
  @st.cache_data(ttl=60)
61
- def load_overall_stats():
62
- collection = db["DK_Player_Stats"]
 
 
 
63
  cursor = collection.find()
64
 
65
  raw_display = pd.DataFrame(list(cursor))
@@ -70,7 +74,10 @@ def load_overall_stats():
70
  raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
71
  dk_raw = raw_display.sort_values(by='Median', ascending=False)
72
 
73
- collection = db["FD_Player_Stats"]
 
 
 
74
  cursor = collection.find()
75
 
76
  raw_display = pd.DataFrame(list(cursor))
@@ -81,7 +88,10 @@ def load_overall_stats():
81
  raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
82
  fd_raw = raw_display.sort_values(by='Median', ascending=False)
83
 
84
- collection = db["Secondary_DK_Player_Stats"]
 
 
 
85
  cursor = collection.find()
86
 
87
  raw_display = pd.DataFrame(list(cursor))
@@ -92,7 +102,10 @@ def load_overall_stats():
92
  raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
93
  dk_raw_sec = raw_display.sort_values(by='Median', ascending=False)
94
 
95
- collection = db["Secondary_FD_Player_Stats"]
 
 
 
96
  cursor = collection.find()
97
 
98
  raw_display = pd.DataFrame(list(cursor))
@@ -103,7 +116,10 @@ def load_overall_stats():
103
  raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
104
  fd_raw_sec = raw_display.sort_values(by='Median', ascending=False)
105
 
106
- collection = db["Player_SD_Range_Of_Outcomes"]
 
 
 
107
  cursor = collection.find()
108
 
109
  raw_display = pd.DataFrame(list(cursor))
@@ -119,7 +135,10 @@ def load_overall_stats():
119
 
120
  print(sd_raw.head(10))
121
 
122
- collection = db["Player_Range_Of_Outcomes"]
 
 
 
123
  cursor = collection.find()
124
 
125
  raw_display = pd.DataFrame(list(cursor))
@@ -251,7 +270,8 @@ def convert_df(array):
251
  array = pd.DataFrame(array, columns=column_names)
252
  return array.to_csv().encode('utf-8')
253
 
254
- dk_raw, fd_raw, dk_raw_sec, fd_raw_sec, roo_raw, sd_raw, dk_sd_raw, fd_sd_raw, timestamp = load_overall_stats()
 
255
  salary_dict = dict(zip(roo_raw.Player, roo_raw.Salary))
256
  id_dict = dict(zip(roo_raw.Player, roo_raw.player_ID))
257
  salary_dict_sd = dict(zip(sd_raw.Player, sd_raw.Salary))
@@ -279,7 +299,8 @@ with tab1:
279
  with col2:
280
  if st.button("Load/Reset Data", key='reset1'):
281
  st.cache_data.clear()
282
- dk_raw, fd_raw, dk_raw_sec, fd_raw_sec, roo_raw, sd_raw, dk_sd_raw, fd_sd_raw, timestamp = load_overall_stats()
 
283
  salary_dict = dict(zip(roo_raw.Player, roo_raw.Salary))
284
  id_dict = dict(zip(roo_raw.Player, roo_raw.player_ID))
285
  salary_dict_sd = dict(zip(sd_raw.Player, sd_raw.Salary))
@@ -292,12 +313,14 @@ with tab1:
292
  t_stamp = f"Last Update: " + str(timestamp) + f" CST"
293
  for key in st.session_state.keys():
294
  del st.session_state[key]
295
- col1, col2, col3, col4, col5 = st.columns(5)
296
  with col1:
297
  view_var2 = st.radio("View Type", ('Simple', 'Advanced'), key='view_var2')
298
  with col2:
299
- slate_type_var2 = st.radio("What slate type are you working with?", ('Regular', 'Showdown'), key='slate_type_var2')
300
  with col3:
 
 
301
  site_var2 = st.radio("Site", ('Draftkings', 'Fanduel'), key='site_var2')
302
 
303
  # Process site selection
@@ -311,7 +334,7 @@ with tab1:
311
  site_baselines = roo_raw[roo_raw['site'] == 'Fanduel']
312
  elif slate_type_var2 == 'Showdown':
313
  site_baselines = sd_raw[sd_raw['site'] == 'Fanduel']
314
- with col4:
315
  slate_split = st.radio("Slate Type", ('Main Slate', 'Secondary'), key='slate_split')
316
 
317
  if slate_split == 'Main Slate':
@@ -325,7 +348,7 @@ with tab1:
325
  elif slate_type_var2 == 'Showdown':
326
  raw_baselines = site_baselines[site_baselines['slate'] == 'Showdown #2']
327
 
328
- with col5:
329
  split_var2 = st.radio("Slate Range", ('Full Slate Run', 'Specific Games'), key='split_var2')
330
  if split_var2 == 'Specific Games':
331
  team_var2 = st.multiselect('Select teams for ROO', options=raw_baselines['Team'].unique(), key='team_var2')
@@ -377,7 +400,8 @@ with tab2:
377
  with st.expander("Info and Filters"):
378
  if st.button("Load/Reset Data", key='reset2'):
379
  st.cache_data.clear()
380
- dk_raw, fd_raw, dk_raw_sec, fd_raw_sec, roo_raw, sd_raw, dk_sd_raw, fd_sd_raw, timestamp = load_overall_stats()
 
381
  salary_dict = dict(zip(roo_raw.Player, roo_raw.Salary))
382
  id_dict = dict(zip(roo_raw.Player, roo_raw.player_ID))
383
  salary_dict_sd = dict(zip(sd_raw.Player, sd_raw.Salary))
 
13
  uri = st.secrets['mongo_uri']
14
  client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
15
  db = client["NBA_DFS"]
16
+ wnba_db = client["WNBA_DFS"]
17
 
18
+ return db, wnba_db
19
 
20
+ db, wnba_db = init_conn()
21
 
22
  dk_columns = ['PG', 'SG', 'SF', 'PF', 'C', 'G', 'F', 'FLEX', 'salary', 'proj', 'Team', 'Team_count', 'Secondary', 'Secondary_count', 'Own']
23
  dk_sd_columns = ['CPT', 'FLEX1', 'FLEX2', 'FLEX3', 'FLEX4', 'FLEX5', 'salary', 'proj', 'Team', 'Team_count', 'Secondary', 'Secondary_count', 'Own']
 
59
  </style>""", unsafe_allow_html=True)
60
 
61
  @st.cache_data(ttl=60)
62
+ def load_overall_stats(league: str):
63
+ if league == 'NBA':
64
+ collection = db["DK_Player_Stats"]
65
+ elif league == 'WNBA':
66
+ collection = wnba_db["DK_Player_Stats"]
67
  cursor = collection.find()
68
 
69
  raw_display = pd.DataFrame(list(cursor))
 
74
  raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
75
  dk_raw = raw_display.sort_values(by='Median', ascending=False)
76
 
77
+ if league == 'NBA':
78
+ collection = db["FD_Player_Stats"]
79
+ elif league == 'WNBA':
80
+ collection = wnba_db["FD_Player_Stats"]
81
  cursor = collection.find()
82
 
83
  raw_display = pd.DataFrame(list(cursor))
 
88
  raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
89
  fd_raw = raw_display.sort_values(by='Median', ascending=False)
90
 
91
+ if league == 'NBA':
92
+ collection = db["Secondary_DK_Player_Stats"]
93
+ elif league == 'WNBA':
94
+ collection = wnba_db["Secondary_DK_Player_Stats"]
95
  cursor = collection.find()
96
 
97
  raw_display = pd.DataFrame(list(cursor))
 
102
  raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
103
  dk_raw_sec = raw_display.sort_values(by='Median', ascending=False)
104
 
105
+ if league == 'NBA':
106
+ collection = db["Secondary_FD_Player_Stats"]
107
+ elif league == 'WNBA':
108
+ collection = wnba_db["Secondary_FD_Player_Stats"]
109
  cursor = collection.find()
110
 
111
  raw_display = pd.DataFrame(list(cursor))
 
116
  raw_display = raw_display.apply(pd.to_numeric, errors='ignore')
117
  fd_raw_sec = raw_display.sort_values(by='Median', ascending=False)
118
 
119
+ if league == 'NBA':
120
+ collection = db["Player_SD_Range_Of_Outcomes"]
121
+ elif league == 'WNBA':
122
+ collection = wnba_db["Player_SD_Range_Of_Outcomes"]
123
  cursor = collection.find()
124
 
125
  raw_display = pd.DataFrame(list(cursor))
 
135
 
136
  print(sd_raw.head(10))
137
 
138
+ if league == 'NBA':
139
+ collection = db["Player_Range_Of_Outcomes"]
140
+ elif league == 'WNBA':
141
+ collection = wnba_db["Player_Range_Of_Outcomes"]
142
  cursor = collection.find()
143
 
144
  raw_display = pd.DataFrame(list(cursor))
 
270
  array = pd.DataFrame(array, columns=column_names)
271
  return array.to_csv().encode('utf-8')
272
 
273
+ league_var = 'NBA'
274
+ dk_raw, fd_raw, dk_raw_sec, fd_raw_sec, roo_raw, sd_raw, dk_sd_raw, fd_sd_raw, timestamp = load_overall_stats(league_var)
275
  salary_dict = dict(zip(roo_raw.Player, roo_raw.Salary))
276
  id_dict = dict(zip(roo_raw.Player, roo_raw.player_ID))
277
  salary_dict_sd = dict(zip(sd_raw.Player, sd_raw.Salary))
 
299
  with col2:
300
  if st.button("Load/Reset Data", key='reset1'):
301
  st.cache_data.clear()
302
+ league_var = 'NBA'
303
+ dk_raw, fd_raw, dk_raw_sec, fd_raw_sec, roo_raw, sd_raw, dk_sd_raw, fd_sd_raw, timestamp = load_overall_stats(league_var)
304
  salary_dict = dict(zip(roo_raw.Player, roo_raw.Salary))
305
  id_dict = dict(zip(roo_raw.Player, roo_raw.player_ID))
306
  salary_dict_sd = dict(zip(sd_raw.Player, sd_raw.Salary))
 
313
  t_stamp = f"Last Update: " + str(timestamp) + f" CST"
314
  for key in st.session_state.keys():
315
  del st.session_state[key]
316
+ col1, col2, col3, col4, col5, col6 = st.columns(6)
317
  with col1:
318
  view_var2 = st.radio("View Type", ('Simple', 'Advanced'), key='view_var2')
319
  with col2:
320
+ league_var = st.radio("What League to load:", ('NBA', 'WNBA'), key='league_var')
321
  with col3:
322
+ slate_type_var2 = st.radio("What slate type are you working with?", ('Regular', 'Showdown'), key='slate_type_var2')
323
+ with col4:
324
  site_var2 = st.radio("Site", ('Draftkings', 'Fanduel'), key='site_var2')
325
 
326
  # Process site selection
 
334
  site_baselines = roo_raw[roo_raw['site'] == 'Fanduel']
335
  elif slate_type_var2 == 'Showdown':
336
  site_baselines = sd_raw[sd_raw['site'] == 'Fanduel']
337
+ with col5:
338
  slate_split = st.radio("Slate Type", ('Main Slate', 'Secondary'), key='slate_split')
339
 
340
  if slate_split == 'Main Slate':
 
348
  elif slate_type_var2 == 'Showdown':
349
  raw_baselines = site_baselines[site_baselines['slate'] == 'Showdown #2']
350
 
351
+ with col6:
352
  split_var2 = st.radio("Slate Range", ('Full Slate Run', 'Specific Games'), key='split_var2')
353
  if split_var2 == 'Specific Games':
354
  team_var2 = st.multiselect('Select teams for ROO', options=raw_baselines['Team'].unique(), key='team_var2')
 
400
  with st.expander("Info and Filters"):
401
  if st.button("Load/Reset Data", key='reset2'):
402
  st.cache_data.clear()
403
+ league_var = 'NBA'
404
+ dk_raw, fd_raw, dk_raw_sec, fd_raw_sec, roo_raw, sd_raw, dk_sd_raw, fd_sd_raw, timestamp = load_overall_stats(league_var)
405
  salary_dict = dict(zip(roo_raw.Player, roo_raw.Salary))
406
  id_dict = dict(zip(roo_raw.Player, roo_raw.player_ID))
407
  salary_dict_sd = dict(zip(sd_raw.Player, sd_raw.Salary))