James McCool commited on
Commit
60577ca
·
1 Parent(s): 39eff33

Refactor database connection and data initialization in app.py

Browse files

Separated database connection and data initialization into distinct functions. Updated the initialization process to improve modularity and leverage Streamlit's caching mechanism for data retrieval. Removed redundant code and simplified the data loading workflow.

Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -14,17 +14,22 @@ def init_conn():
14
  client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
15
  db = client["League_of_Legends_Database"]
16
 
17
- current_date = datetime.now()
18
 
19
- collection = db["gamelogs"]
20
- max_date = current_date - timedelta(days=1)
21
- min_date = current_date - timedelta(days=365)
22
- team_names = collection.distinct("teamname")
23
- player_names = collection.distinct("playername")
 
 
 
 
24
 
25
- return db, team_names, player_names, min_date, max_date
26
 
27
- db, team_names, player_names, min_date, max_date = init_conn()
 
28
 
29
  display_formats = {'wKill%': '{:.2%}', 'wDeath%': '{:.2%}', 'wAssist%': '{:.2%}', 'lKill%': '{:.2%}', 'lDeath%': '{:.2%}', 'lAssist%': '{:.2%}', 'Over %': '{:.2%}', 'Under %': '{:.2%}'}
30
  leagues = ['AL', 'CBLOL', 'GLL', 'HM', 'LCK', 'LCS', 'LEC', 'LFL', 'LLA', 'LPL', 'LPLOL', 'LVP SL', 'MSI', 'PCS', 'PGN', 'PRM', 'TCL', 'VCS', 'LTAN', 'LTAS',
 
14
  client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
15
  db = client["League_of_Legends_Database"]
16
 
17
+ return db
18
 
19
+ @st.cache_resource(ttl = 300)
20
+ def init_data():
21
+ current_date = datetime.now()
22
+
23
+ collection = db["gamelogs"]
24
+ max_date = current_date - timedelta(days=1)
25
+ min_date = current_date - timedelta(days=365)
26
+ team_names = collection.distinct("teamname")
27
+ player_names = collection.distinct("playername")
28
 
29
+ return team_names, player_names, min_date, max_date
30
 
31
+ db = init_conn()
32
+ team_names, player_names, min_date, max_date = init_data()
33
 
34
  display_formats = {'wKill%': '{:.2%}', 'wDeath%': '{:.2%}', 'wAssist%': '{:.2%}', 'lKill%': '{:.2%}', 'lDeath%': '{:.2%}', 'lAssist%': '{:.2%}', 'Over %': '{:.2%}', 'Under %': '{:.2%}'}
35
  leagues = ['AL', 'CBLOL', 'GLL', 'HM', 'LCK', 'LCS', 'LEC', 'LFL', 'LLA', 'LPL', 'LPLOL', 'LVP SL', 'MSI', 'PCS', 'PGN', 'PRM', 'TCL', 'VCS', 'LTAN', 'LTAS',