PGA_DFS_models / app.py
James McCool
Refactor app.py: Optimize data filtering for site selection. Updated the display logic to directly filter the hold_display DataFrame based on the selected site, improving performance and readability.
792293e
raw
history blame
2.55 kB
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
@st.cache_resource
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%}'}
@st.cache_resource(ttl = 600)
def init_baselines():
collection = db["PGA_Range_of_Outcomes"]
cursor = collection.find()
player_frame = pd.DataFrame(cursor)
roo_data = player_frame
return roo_data
def convert_df_to_csv(df):
return df.to_csv().encode('utf-8')
roo_data = 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 = init_baselines()
hold_display = roo_data
lineup_display = []
check_list = []
rand_player = 0
boost_player = 0
salaryCut = 0
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")