Spaces:
Running
Running
James McCool
Refactor app.py: Enhance data initialization and timestamp handling. Updated init_baselines to return both processed data and timestamp, ensuring better data management. Adjusted references to the updated function throughout the code, including site selection logic and display updates.
e54a8b1
import streamlit as st | |
st.set_page_config(layout="wide") | |
for name in dir(): | |
if not name.startswith('_'): | |
del globals()[name] | |
import numpy as np | |
import pandas as pd | |
import streamlit as st | |
import gc | |
import pymongo | |
def init_conn(): | |
uri = st.secrets['mongo_uri'] | |
client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000) | |
db = client["PGA_Database"] | |
return db | |
db = init_conn() | |
dk_player_url = 'https://docs.google.com/spreadsheets/d/1lMLxWdvCnOFBtG9dhM0zv2USuxZbkogI_2jnxFfQVVs/edit#gid=1828092624' | |
CSV_URL = 'https://docs.google.com/spreadsheets/d/1lMLxWdvCnOFBtG9dhM0zv2USuxZbkogI_2jnxFfQVVs/edit#gid=1828092624' | |
player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '100+%': '{:.2%}', '10x%': '{:.2%}', '11x%': '{:.2%}', | |
'12x%': '{:.2%}','LevX': '{:.2%}'} | |
def init_baselines(): | |
collection = db["PGA_Range_of_Outcomes"] | |
cursor = collection.find() | |
player_frame = pd.DataFrame(cursor) | |
timestamp = player_frame['Timestamp'][0] | |
roo_data = player_frame.drop(columns=['_id', 'index', 'Timestamp']) | |
roo_data['Salary'] = roo_data['Salary'].astype(int) | |
return roo_data, timestamp | |
def convert_df_to_csv(df): | |
return df.to_csv().encode('utf-8') | |
roo_data, timestamp = init_baselines() | |
hold_display = roo_data | |
lineup_display = [] | |
check_list = [] | |
rand_player = 0 | |
boost_player = 0 | |
salaryCut = 0 | |
tab1, tab2 = st.tabs(["Player Overall Projections", "Not Ready Yet"]) | |
with tab1: | |
if st.button("Reset Data", key='reset1'): | |
# Clear values from *all* all in-memory and on-disk data caches: | |
# i.e. clear values from both square and cube | |
st.cache_data.clear() | |
roo_data, timestamp = init_baselines() | |
hold_display = roo_data | |
lineup_display = [] | |
check_list = [] | |
rand_player = 0 | |
boost_player = 0 | |
salaryCut = 0 | |
st.write(timestamp) | |
options_container = st.empty() | |
hold_container = st.empty() | |
with options_container: | |
site_var = st.selectbox("Select a Site", ["Draftkings", "FanDuel"]) | |
with hold_container: | |
display = hold_display[hold_display['Site'] == site_var] | |
st.dataframe(display.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), height=750, use_container_width = True) | |
st.download_button( | |
label="Export Projections", | |
data=convert_df_to_csv(display), | |
file_name='PGA_DFS_export.csv', | |
mime='text/csv', | |
) | |
with tab2: | |
st.write("Not Ready Yet") |