James McCool commited on
Commit
b7e1378
·
1 Parent(s): ea8bf12

Initial commit and modernization

Browse files
.streamlit/secrets.toml ADDED
@@ -0,0 +1 @@
 
 
1
+ mongo_uri = "mongodb+srv://multichem:Xr1q5wZdXPbxdUmJ@testcluster.lgwtp5i.mongodb.net/?retryWrites=true&w=majority&appName=TestCluster"
Dockerfile CHANGED
@@ -5,11 +5,22 @@ WORKDIR /app
5
  RUN apt-get update && apt-get install -y \
6
  build-essential \
7
  curl \
 
8
  git \
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
  COPY requirements.txt ./
12
  COPY src/ ./src/
 
 
 
 
 
 
 
 
 
 
13
 
14
  RUN pip3 install -r requirements.txt
15
 
 
5
  RUN apt-get update && apt-get install -y \
6
  build-essential \
7
  curl \
8
+ software-properties-common \
9
  git \
10
  && rm -rf /var/lib/apt/lists/*
11
 
12
  COPY requirements.txt ./
13
  COPY src/ ./src/
14
+ COPY .streamlit/ ./.streamlit/
15
+
16
+ ENV MONGO_URI="mongodb+srv://multichem:Xr1q5wZdXPbxdUmJ@testcluster.lgwtp5i.mongodb.net/?retryWrites=true&w=majority&appName=TestCluster"
17
+ RUN useradd -m -u 1000 user
18
+ USER user
19
+ ENV HOME=/home/user\
20
+ PATH=/home/user/.local/bin:$PATH
21
+ WORKDIR $HOME/app
22
+ RUN pip install --no-cache-dir --upgrade pip
23
+ COPY --chown=user . $HOME/app
24
 
25
  RUN pip3 install -r requirements.txt
26
 
requirements.txt CHANGED
@@ -1,3 +1,8 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
 
 
1
+ streamlit
2
+ openpyxl
3
+ matplotlib
4
+ pulp
5
+ docker
6
+ plotly
7
+ scipy
8
+ pymongo
src/database.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pymongo
3
+ import os
4
+
5
+ @st.cache_resource
6
+ def init_conn():
7
+ # Try to get from environment variable first, fall back to secrets
8
+ uri = os.getenv('MONGO_URI')
9
+ if not uri:
10
+ uri = st.secrets['mongo_uri']
11
+ client = pymongo.MongoClient(uri, retryWrites=True, serverSelectionTimeoutMS=500000)
12
+ db = client["NHL_Database"]
13
+
14
+ return db
15
+
16
+ db = init_conn()
src/streamlit_app.py CHANGED
@@ -1,40 +1,322 @@
1
- import altair as alt
2
  import numpy as np
 
3
  import pandas as pd
4
  import streamlit as st
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import numpy as np
2
+ from numpy import random
3
  import pandas as pd
4
  import streamlit as st
5
+ from database import db
6
 
7
+ st.set_page_config(layout="wide")
8
+
9
+ wrong_acro = ['WSH', 'AZ']
10
+ right_acro = ['WAS', 'ARI']
11
+
12
+ game_format = {'Win Percentage': '{:.2%}','First Inning Lead Percentage': '{:.2%}',
13
+ 'Fifth Inning Lead Percentage': '{:.2%}', '8+ runs': '{:.2%}', 'DK LevX': '{:.2%}', 'FD LevX': '{:.2%}'}
14
+
15
+ team_roo_format = {'Top Score%': '{:.2%}','0 Runs': '{:.2%}', '1 Run': '{:.2%}', '2 Runs': '{:.2%}', '3 Runs': '{:.2%}', '4 Runs': '{:.2%}',
16
+ '5 Runs': '{:.2%}','6 Runs': '{:.2%}', '7 Runs': '{:.2%}', '8 Runs': '{:.2%}', '9 Runs': '{:.2%}', '10 Runs': '{:.2%}'}
17
+
18
+ player_roo_format = {'Top_finish': '{:.2%}','Top_5_finish': '{:.2%}', 'Top_10_finish': '{:.2%}', '20+%': '{:.2%}', '2x%': '{:.2%}', '3x%': '{:.2%}',
19
+ '4x%': '{:.2%}','GPP%': '{:.2%}'}
20
+
21
+ st.markdown("""
22
+ <style>
23
+ /* Tab styling */
24
+ .stTabs [data-baseweb="tab-list"] {
25
+ gap: 8px;
26
+ padding: 4px;
27
+ }
28
+
29
+ .stTabs [data-baseweb="tab"] {
30
+ height: 50px;
31
+ white-space: pre-wrap;
32
+ background-color: #FFD700;
33
+ color: white;
34
+ border-radius: 10px;
35
+ gap: 1px;
36
+ padding: 10px 20px;
37
+ font-weight: bold;
38
+ transition: all 0.3s ease;
39
+ }
40
+
41
+ .stTabs [aria-selected="true"] {
42
+ background-color: #DAA520;
43
+ color: white;
44
+ }
45
+
46
+ .stTabs [data-baseweb="tab"]:hover {
47
+ background-color: #DAA520;
48
+ cursor: pointer;
49
+ }
50
+ </style>""", unsafe_allow_html=True)
51
+
52
+ @st.cache_resource(ttl = 599)
53
+ def player_stat_table():
54
+ collection = db["Player_Level_ROO"]
55
+ cursor = collection.find()
56
+ load_display = pd.DataFrame(cursor)
57
+
58
+ load_display.replace('', np.nan, inplace=True)
59
+ player_stats = load_display.copy()
60
+
61
+ dk_load_display = load_display[load_display['Site'] == 'Draftkings']
62
+ fd_load_display = load_display[load_display['Site'] == 'Fanduel']
63
+
64
+ dk_load_display = dk_load_display.sort_values(by='Own', ascending=False)
65
+ fd_load_display = fd_load_display.sort_values(by='Own', ascending=False)
66
+
67
+ dk_load_display = dk_load_display.dropna(subset=['Own'])
68
+ fd_load_display = fd_load_display.dropna(subset=['Own'])
69
+
70
+ dk_roo_raw = dk_load_display
71
+ fd_roo_raw = fd_load_display
72
+
73
+ return player_stats, dk_roo_raw, fd_roo_raw
74
+
75
+ @st.cache_data
76
+ def convert_df_to_csv(df):
77
+ return df.to_csv().encode('utf-8')
78
+
79
+ player_stats, dk_roo_raw, fd_roo_raw = player_stat_table()
80
+ opp_dict = dict(zip(dk_roo_raw.Team, dk_roo_raw.Opp))
81
+ t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
82
+
83
+ st.header("NHL Pivot Finder Tool")
84
+ with st.expander("Info and Filters"):
85
+ st.info(t_stamp)
86
+ if st.button("Load/Reset Data", key='reset1'):
87
+ st.cache_data.clear()
88
+ for key in st.session_state.keys():
89
+ del st.session_state[key]
90
+ player_stats, dk_roo_raw, fd_roo_raw = player_stat_table()
91
+ opp_dict = dict(zip(dk_roo_raw.Team, dk_roo_raw.Opp))
92
+ t_stamp = f"Last Update: " + str(dk_roo_raw['timestamp'][0]) + f" CST"
93
+ site_var1 = st.radio("What site are you working with?", ('Draftkings', 'Fanduel'), key='site_var1')
94
+ if site_var1 == 'Draftkings':
95
+ raw_baselines = dk_roo_raw[dk_roo_raw['Slate'] == 'Main Slate']
96
+ raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
97
+ elif site_var1 == 'Fanduel':
98
+ raw_baselines = fd_roo_raw[fd_roo_raw['Slate'] == 'Main Slate']
99
+ raw_baselines = raw_baselines.sort_values(by='Own', ascending=False)
100
+ check_seq = st.radio("Do you want to check a single player or the top 10 in ownership?", ('Single Player', 'Top X Owned'), key='check_seq')
101
+ if check_seq == 'Single Player':
102
+ player_check = st.selectbox('Select player to create comps', options = raw_baselines['Player'].unique(), key='dk_player')
103
+ elif check_seq == 'Top X Owned':
104
+ top_x_var = st.number_input('How many players would you like to check?', min_value = 1, max_value = 10, value = 5, step = 1)
105
+ Salary_var = st.number_input('Acceptable +/- Salary range', min_value = 0, max_value = 1000, value = 300, step = 100)
106
+ Median_var = st.number_input('Acceptable +/- Median range', min_value = 0, max_value = 10, value = 3, step = 1)
107
+ pos_var1 = st.radio("Compare to all positions or specific positions?", ('All Positions', 'Specific Positions'), key='pos_var1')
108
+ if pos_var1 == 'Specific Positions':
109
+ pos_var_list = st.multiselect('Which positions would you like to include?', options = raw_baselines['Position'].unique(), key='pos_var_list')
110
+ elif pos_var1 == 'All Positions':
111
+ pos_var_list = raw_baselines.Position.values.tolist()
112
+ split_var1 = st.radio("Are you running the full slate or certain games?", ('Full Slate Run', 'Specific Games'), key='split_var1')
113
+ if split_var1 == 'Specific Games':
114
+ team_var1 = st.multiselect('Which teams would you like to include?', options = raw_baselines['Team'].unique(), key='team_var1')
115
+ elif split_var1 == 'Full Slate Run':
116
+ team_var1 = raw_baselines.Team.values.tolist()
117
+
118
+ placeholder = st.empty()
119
+ displayholder = st.empty()
120
+
121
+ if st.button('Simulate appropriate pivots'):
122
+ with placeholder:
123
+ if site_var1 == 'Draftkings':
124
+ working_roo = raw_baselines
125
+ working_roo.replace('', 0, inplace=True)
126
+ if site_var1 == 'Fanduel':
127
+ working_roo = raw_baselines
128
+ working_roo.replace('', 0, inplace=True)
129
+
130
+ own_dict = dict(zip(working_roo.Player, working_roo.Own))
131
+ team_dict = dict(zip(working_roo.Player, working_roo.Team))
132
+ opp_dict = dict(zip(working_roo.Player, working_roo.Opp))
133
+ pos_dict = dict(zip(working_roo.Player, working_roo.Position))
134
+ total_sims = 1000
135
+
136
+ if check_seq == 'Single Player':
137
+ player_var = working_roo.loc[working_roo['Player'] == player_check]
138
+ player_var = player_var.reset_index()
139
+
140
+ working_roo = working_roo[working_roo['Position'].isin(pos_var_list)]
141
+ working_roo = working_roo[working_roo['Team'].isin(team_var1)]
142
+ working_roo = working_roo.loc[(working_roo['Salary'] >= player_var['Salary'][0] - Salary_var) & (working_roo['Salary'] <= player_var['Salary'][0] + Salary_var)]
143
+ working_roo = working_roo.loc[(working_roo['Median'] >= player_var['Median'][0] - Median_var) & (working_roo['Median'] <= player_var['Median'][0] + Median_var)]
144
+
145
+ flex_file = working_roo[['Player', 'Position', 'Salary', 'Median']]
146
+ flex_file['Floor_raw'] = flex_file['Median'] * .25
147
+ flex_file['Ceiling_raw'] = flex_file['Median'] * 2
148
+ flex_file['Floor'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * .5, flex_file['Floor_raw'])
149
+ flex_file['Floor'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * .1, flex_file['Floor_raw'])
150
+ flex_file['Ceiling'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
151
+ flex_file['Ceiling'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
152
+ flex_file['STD'] = flex_file['Median'] / 3
153
+ flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
154
+ hold_file = flex_file.copy()
155
+ overall_file = flex_file.copy()
156
+ salary_file = flex_file.copy()
157
+
158
+ overall_players = overall_file[['Player']]
159
+
160
+ for x in range(0,total_sims):
161
+ salary_file[x] = salary_file['Salary']
162
+ overall_file[x] = random.normal(overall_file['Median'],overall_file['STD'])
163
+
164
+ salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
165
+
166
+ salary_file = salary_file.div(1000)
167
+
168
+ overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
169
+
170
+ players_only = hold_file[['Player']]
171
+ raw_lineups_file = players_only
172
+
173
+ for x in range(0,total_sims):
174
+ maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
175
+ raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
176
+ players_only[x] = raw_lineups_file[x].rank(ascending=False)
177
+
178
+ players_only=players_only.drop(['Player'], axis=1)
179
+
180
+ salary_2x_check = (overall_file - (salary_file*2))
181
+ salary_3x_check = (overall_file - (salary_file*3))
182
+ salary_4x_check = (overall_file - (salary_file*4))
183
+
184
+ players_only['Average_Rank'] = players_only.mean(axis=1)
185
+ players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
186
+ players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
187
+ players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
188
+ players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
189
+ players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
190
+ players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
191
+ players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
192
+
193
+ players_only['Player'] = hold_file[['Player']]
194
+
195
+ final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
196
+
197
+ final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
198
+ final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
199
+ final_Proj['Own'] = final_Proj['Player'].map(own_dict)
200
+ final_Proj['Team'] = final_Proj['Player'].map(team_dict)
201
+ final_Proj['Opp'] = final_Proj['Player'].map(opp_dict)
202
+ final_Proj = final_Proj[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own']]
203
+ final_Proj['Projection Rank'] = final_Proj.Median.rank(pct = True)
204
+ final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
205
+ final_Proj['LevX'] = 0
206
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'C', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['20+%'] - final_Proj['Own Rank'], final_Proj['LevX'])
207
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'W', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['20+%'] - final_Proj['Own Rank'], final_Proj['LevX'])
208
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'D', final_Proj[['Projection Rank', '2x%']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
209
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'G', final_Proj[['Projection Rank', '2x%']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
210
+ final_Proj['CPT_Own'] = final_Proj['Own'] / 4
211
+
212
+ final_Proj = final_Proj[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own', 'LevX']]
213
+ final_Proj = final_Proj.set_index('Player')
214
+ st.session_state.final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
215
+
216
+ elif check_seq == 'Top X Owned':
217
+ if pos_var1 == 'Specific Positions':
218
+ raw_baselines = raw_baselines[raw_baselines['Position'].isin(pos_var_list)]
219
+ player_check = raw_baselines['Player'].head(top_x_var).tolist()
220
+ final_proj_list = []
221
+ for players in player_check:
222
+ players_pos = pos_dict[players]
223
+ player_var = working_roo.loc[working_roo['Player'] == players]
224
+ player_var = player_var.reset_index()
225
+ working_roo_temp = working_roo[working_roo['Position'] == players_pos]
226
+ working_roo_temp = working_roo_temp[working_roo_temp['Team'].isin(team_var1)]
227
+ working_roo_temp = working_roo_temp.loc[(working_roo_temp['Salary'] >= player_var['Salary'][0] - Salary_var) & (working_roo_temp['Salary'] <= player_var['Salary'][0] + Salary_var)]
228
+ working_roo_temp = working_roo_temp.loc[(working_roo_temp['Median'] >= player_var['Median'][0] - Median_var) & (working_roo_temp['Median'] <= player_var['Median'][0] + Median_var)]
229
+
230
+ flex_file = working_roo_temp[['Player', 'Position', 'Salary', 'Median']]
231
+ flex_file['Floor_raw'] = flex_file['Median'] * .25
232
+ flex_file['Ceiling_raw'] = flex_file['Median'] * 2
233
+ flex_file['Floor'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * .5, flex_file['Floor_raw'])
234
+ flex_file['Floor'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * .1, flex_file['Floor_raw'])
235
+ flex_file['Ceiling'] = np.where(flex_file['Position'] == 'G', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
236
+ flex_file['Ceiling'] = np.where(flex_file['Position'] == 'D', flex_file['Median'] * 1.75, flex_file['Ceiling_raw'])
237
+ flex_file['STD'] = flex_file['Median'] / 3
238
+ flex_file = flex_file[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD']]
239
+ hold_file = flex_file.copy()
240
+ overall_file = flex_file.copy()
241
+ salary_file = flex_file.copy()
242
+
243
+ overall_players = overall_file[['Player']]
244
+
245
+ for x in range(0,total_sims):
246
+ salary_file[x] = salary_file['Salary']
247
+ overall_file[x] = random.normal(overall_file['Median'],overall_file['STD'])
248
+
249
+ salary_file=salary_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
250
+
251
+ salary_file = salary_file.div(1000)
252
+
253
+ overall_file=overall_file.drop(['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'STD'], axis=1)
254
+
255
+ players_only = hold_file[['Player']]
256
+ raw_lineups_file = players_only
257
+
258
+ for x in range(0,total_sims):
259
+ maps_dict = {'proj_map':dict(zip(hold_file.Player,overall_file[x]))}
260
+ raw_lineups_file[x] = sum([raw_lineups_file['Player'].map(maps_dict['proj_map'])])
261
+ players_only[x] = raw_lineups_file[x].rank(ascending=False)
262
+
263
+ players_only=players_only.drop(['Player'], axis=1)
264
+
265
+ salary_2x_check = (overall_file - (salary_file*2))
266
+ salary_3x_check = (overall_file - (salary_file*3))
267
+ salary_4x_check = (overall_file - (salary_file*4))
268
+
269
+ players_only['Average_Rank'] = players_only.mean(axis=1)
270
+ players_only['Top_finish'] = players_only[players_only == 1].count(axis=1)/total_sims
271
+ players_only['Top_5_finish'] = players_only[players_only <= 5].count(axis=1)/total_sims
272
+ players_only['Top_10_finish'] = players_only[players_only <= 10].count(axis=1)/total_sims
273
+ players_only['20+%'] = overall_file[overall_file >= 20].count(axis=1)/float(total_sims)
274
+ players_only['2x%'] = salary_2x_check[salary_2x_check >= 1].count(axis=1)/float(total_sims)
275
+ players_only['3x%'] = salary_3x_check[salary_3x_check >= 1].count(axis=1)/float(total_sims)
276
+ players_only['4x%'] = salary_4x_check[salary_4x_check >= 1].count(axis=1)/float(total_sims)
277
+
278
+ players_only['Player'] = hold_file[['Player']]
279
+
280
+ final_outcomes = players_only[['Player', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
281
+
282
+ final_Proj = pd.merge(hold_file, final_outcomes, on="Player")
283
+ final_Proj = final_Proj[['Player', 'Position', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%']]
284
+ final_Proj['Own'] = final_Proj['Player'].map(own_dict)
285
+ final_Proj['Team'] = final_Proj['Player'].map(team_dict)
286
+ final_Proj['Opp'] = final_Proj['Player'].map(opp_dict)
287
+ final_Proj = final_Proj[['Player', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own']]
288
+ final_Proj['Projection Rank'] = final_Proj.Median.rank(pct = True)
289
+ final_Proj['Own Rank'] = final_Proj.Own.rank(pct = True)
290
+ final_Proj['LevX'] = 0
291
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'C', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['20+%'] - final_Proj['Own Rank'], final_Proj['LevX'])
292
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'W', final_Proj[['Projection Rank', 'Top_5_finish']].mean(axis=1) + final_Proj['20+%'] - final_Proj['Own Rank'], final_Proj['LevX'])
293
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'D', final_Proj[['Projection Rank', '2x%']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
294
+ final_Proj['LevX'] = np.where(final_Proj['Position'] == 'G', final_Proj[['Projection Rank', '2x%']].mean(axis=1) + final_Proj['4x%'] - final_Proj['Own Rank'], final_Proj['LevX'])
295
+ final_Proj['CPT_Own'] = final_Proj['Own'] / 4
296
+ final_Proj['Pivot_source'] = players
297
+
298
+ final_Proj = final_Proj[['Player', 'Pivot_source', 'Position', 'Team', 'Opp', 'Salary', 'Floor', 'Median', 'Ceiling', 'Top_finish', 'Top_5_finish', 'Top_10_finish', '20+%', '2x%', '3x%', '4x%', 'Own', 'LevX']]
299
+
300
+ final_Proj = final_Proj.sort_values(by='Top_finish', ascending=False)
301
+ final_proj_list.append(final_Proj)
302
+ st.write(f'finished run for {players}')
303
+
304
+ # Concatenate all the final_Proj dataframes
305
+ final_Proj_combined = pd.concat(final_proj_list)
306
+ final_Proj_combined = final_Proj_combined.sort_values(by='LevX', ascending=False)
307
+ final_Proj_combined = final_Proj_combined[final_Proj_combined['Player'] != final_Proj_combined['Pivot_source']]
308
+ st.session_state.final_Proj = final_Proj_combined.reset_index(drop=True) # Assign the combined dataframe back to final_Proj
309
+ placeholder.empty()
310
+
311
+ with displayholder.container():
312
+ if 'final_Proj' in st.session_state:
313
+ st.dataframe(st.session_state.final_Proj.style.background_gradient(axis=0).background_gradient(cmap='RdYlGn').format(player_roo_format, precision=2), use_container_width = True)
314
+
315
+ st.download_button(
316
+ label="Export Tables",
317
+ data=convert_df_to_csv(st.session_state.final_Proj),
318
+ file_name='NHL_pivot_export.csv',
319
+ mime='text/csv',
320
+ )
321
+ else:
322
+ st.write("Run some pivots my dude/dudette")