Jon Solow commited on
Commit
b441403
·
1 Parent(s): 96430ae

Add my keepers page with login

Browse files
Files changed (2) hide show
  1. src/pages/2_My_Keepers.py +75 -0
  2. src/yahoo_client.py +1 -1
src/pages/2_My_Keepers.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ import streamlit as st
4
+
5
+ from config import DEFAULT_ICON, LEAGUE_NAME, KEEPER_DATA_URL
6
+ from shared_page import common_page_config
7
+
8
+
9
+ from login_component import is_token_in_session
10
+
11
+
12
+ @st.cache_data(ttl=60 * 60 * 24)
13
+ def load_data():
14
+ data = pd.read_csv(os.path.join(os.path.dirname(__file__), KEEPER_DATA_URL), index_col=0)
15
+ # Hack to get position, replace with better position from yahoo api in future
16
+ data["position"] = (
17
+ data["eligible_positions"].apply(lambda x: x.split(" ")[0]).replace("[", "").replace("]", "").replace("'", "")
18
+ )
19
+ data.columns = data.columns.str.lower()
20
+ data.sort_values(["team_name", "keeper_cost"], inplace=True)
21
+ teams_list = sorted(list(data["team_name"].unique()))
22
+
23
+ return data, teams_list
24
+
25
+
26
+ def get_page():
27
+ page_title = f"{LEAGUE_NAME} - My Keepers"
28
+ st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
29
+ common_page_config()
30
+ st.title(page_title)
31
+
32
+ if not is_token_in_session():
33
+ st.write(
34
+ "You must authorize the application to access your account in order to use this feature."
35
+ " Please click Login button above."
36
+ )
37
+
38
+ else:
39
+ st.warning("Work in progress. Ability to save selections is yet to be completed.")
40
+ data, _ = load_data()
41
+ data_user = data[(data["manager_guid"] == st.session_state.get("logged_in_guid")) & (data["eligible"])]
42
+ st.header("Options")
43
+ st.dataframe(
44
+ data_user,
45
+ hide_index=True,
46
+ column_order=[
47
+ "team_name",
48
+ "headshot_url",
49
+ "name",
50
+ "team",
51
+ "position",
52
+ "keeper_cost",
53
+ "eligible",
54
+ "years_eligible",
55
+ ],
56
+ column_config={
57
+ "team_name": st.column_config.TextColumn(label="League Team", help="Name of fantasy League team."),
58
+ "headshot_url": st.column_config.ImageColumn(label="", help="Player image"),
59
+ "name": st.column_config.TextColumn(label="Name", help="Player's name"),
60
+ "team": st.column_config.TextColumn(label="NFL Team"),
61
+ "position": st.column_config.TextColumn(label="Position", help="Player's position"),
62
+ "keeper_cost": st.column_config.NumberColumn(
63
+ label="Keeper Cost", help="Draft Round Cost to keep player. See Rules for details."
64
+ ),
65
+ "eligible": st.column_config.CheckboxColumn(label="Eligible", help="Is player eligible to be keeper?"),
66
+ "years_eligible": st.column_config.NumberColumn(
67
+ label="Years Eligible",
68
+ help="Number of further consecutive seasons player can be kept (subject to maximum of 2)",
69
+ ),
70
+ },
71
+ )
72
+
73
+
74
+ if __name__ == "__main__":
75
+ get_page()
src/yahoo_client.py CHANGED
@@ -81,7 +81,7 @@ class YahooFantasyClient:
81
  )
82
  return league_keys
83
 
84
- def get_guid_for_logged_in_user(self) -> str:
85
  url = "https://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;game_keys=nfl/teams"
86
  root, _ = self.yahoo_request_to_xml(url)
87
  user_guid = root.findtext("./users/user/guid")
 
81
  )
82
  return league_keys
83
 
84
+ def get_guid_for_logged_in_user(self) -> str | None:
85
  url = "https://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;game_keys=nfl/teams"
86
  root, _ = self.yahoo_request_to_xml(url)
87
  user_guid = root.findtext("./users/user/guid")