Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -203,10 +203,80 @@ for x in data_r['fantasy_content']['league']['players']:
|
|
203 |
single_list.append(0)
|
204 |
total_list.append(single_list)
|
205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
df_2023 = pd.DataFrame(data=total_list,columns=['player_id','rank_value','full','first','last','average_pick', 'average_cost','display_position','projected_auction_value','editorial_team_abbr','percent_owned'])
|
207 |
|
208 |
week_dict = yahoo_weeks.set_index('Number')['Week'].sort_index().to_dict()
|
209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
from shiny import ui, render, App
|
211 |
import matplotlib.image as mpimg
|
212 |
# app_ui = ui.page_fluid(
|
@@ -239,10 +309,17 @@ app_ui = ui.page_fluid(
|
|
239 |
ui.input_switch("a_d_id", "Ascending?"),
|
240 |
#ui.input_select("date_id", "Select Date",yahoo_weeks['Week'],width=1),
|
241 |
ui.input_date_range("date_range_id", "Date range input",start = datetime.today().date(), end = datetime.today().date() + timedelta(days=6)),
|
|
|
|
|
|
|
242 |
ui.output_table("result"),width=3),
|
243 |
|
244 |
|
245 |
-
ui.panel_main(
|
|
|
|
|
|
|
|
|
246 |
ui.div({"style": "font-size:2em;"},ui.output_text("txt_title")),
|
247 |
#ui.tags.h2("Fantasy Hockey Schedule Summary"),
|
248 |
ui.tags.h5("Created By: @TJStats, Data: NHL"),
|
@@ -251,9 +328,39 @@ app_ui = ui.page_fluid(
|
|
251 |
ui.tags.h5('Legend'),
|
252 |
ui.output_table("schedule_result_legend"),
|
253 |
ui.tags.h6('An Off Night is defined as a day in which less than half the teams in the NHL are playing'),
|
254 |
-
ui.tags.h6('The scores are determined by using games played, off-nights, B2B, and strength of opponents') )
|
255 |
-
|
256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
# ui.row(
|
258 |
# ui.column(
|
259 |
# 3,
|
@@ -337,6 +444,56 @@ def server(input, output, session):
|
|
337 |
return f'Fantasy Hockey Schedule Summary - Yahoo - Week {input.week_id()}'
|
338 |
else:
|
339 |
return f'Fantasy Hockey Schedule Summary'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
340 |
|
341 |
@output
|
342 |
@render.table
|
@@ -506,7 +663,545 @@ def server(input, output, session):
|
|
506 |
|
507 |
return off_b2b_df_style
|
508 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
509 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
510 |
|
511 |
|
512 |
|
|
|
203 |
single_list.append(0)
|
204 |
total_list.append(single_list)
|
205 |
|
206 |
+
|
207 |
+
def get_all_teams():
|
208 |
+
url = "https://statsapi.web.nhl.com/api/v1/teams"
|
209 |
+
response = requests.get(url)
|
210 |
+
data = response.json()
|
211 |
+
|
212 |
+
# Extract team information from the response
|
213 |
+
teams = []
|
214 |
+
|
215 |
+
for team in data["teams"]:
|
216 |
+
team_id = team["id"]
|
217 |
+
team_name = team["name"]
|
218 |
+
team_abbreviation = team["abbreviation"]
|
219 |
+
team_city = team["locationName"]
|
220 |
+
teams.append({"team_id": team_id, "team_name": team_name, "team_abb": team_abbreviation, "team_city": team_city})
|
221 |
+
|
222 |
+
return teams
|
223 |
+
|
224 |
+
|
225 |
+
def get_all_players_teams():
|
226 |
+
url = "https://statsapi.web.nhl.com/api/v1/teams?expand=team.roster"
|
227 |
+
response = requests.get(url)
|
228 |
+
data = response.json()
|
229 |
+
|
230 |
+
# Extract player information from the response
|
231 |
+
players_teams = []
|
232 |
+
|
233 |
+
for team in data["teams"]:
|
234 |
+
team_name = team["id"]
|
235 |
+
#full_name = team["fullName"]
|
236 |
+
|
237 |
+
for player in team["roster"]["roster"]:
|
238 |
+
player_id = player["person"]["id"]
|
239 |
+
full_name = player["person"]["fullName"]
|
240 |
+
players_teams.append({"player_id": player_id,"full_name":full_name,"team_id": team_name})
|
241 |
+
|
242 |
+
return players_teams
|
243 |
+
|
244 |
df_2023 = pd.DataFrame(data=total_list,columns=['player_id','rank_value','full','first','last','average_pick', 'average_cost','display_position','projected_auction_value','editorial_team_abbr','percent_owned'])
|
245 |
|
246 |
week_dict = yahoo_weeks.set_index('Number')['Week'].sort_index().to_dict()
|
247 |
|
248 |
+
|
249 |
+
player_games = pd.read_csv('data/player_games_cards.csv',index_col=[0])
|
250 |
+
team_games = pd.read_csv('data/team_games.csv',index_col=[0])
|
251 |
+
|
252 |
+
players_df = pd.DataFrame(get_all_players_teams())
|
253 |
+
teams_df = pd.DataFrame(get_all_teams())
|
254 |
+
team_games = team_games.merge(teams_df,left_on='team',right_on='team_name')
|
255 |
+
players_df = players_df.merge(teams_df)
|
256 |
+
|
257 |
+
yahoo_to_nhl = pd.read_csv("data/yahoo_to_nhl.csv",index_col=[0], encoding='unicode_escape')
|
258 |
+
|
259 |
+
yahoo_df_scrape = pd.DataFrame(total_list,columns=['yahoo_id','idx','full','first','last','average_pick','average_auction_cost','projected_auction_value','position','team','percent_owned'])
|
260 |
+
|
261 |
+
yahoo_final_df = yahoo_df_scrape.merge(right=yahoo_to_nhl,left_on='yahoo_id',right_index=True,how='left',suffixes=['','_x'])
|
262 |
+
|
263 |
+
player_games_dd = player_games.drop_duplicates(['player_id']).reset_index(drop=True)
|
264 |
+
|
265 |
+
player_games = player_games.merge(right=yahoo_to_nhl.reset_index(),left_on='player_id',right_on='nhl_id',how='left')
|
266 |
+
|
267 |
+
player_games = player_games.merge(yahoo_final_df,left_on='player_id_yahoo',right_on='yahoo_id',how='left')
|
268 |
+
player_games.loc[player_games.position.isna(),'position'] = player_games.loc[player_games.position.isna(),'Position']
|
269 |
+
|
270 |
+
|
271 |
+
player_games.position = player_games.position.replace({'L':'LW','R':'RW'})
|
272 |
+
player_games.percent_owned = player_games.percent_owned.fillna(0)
|
273 |
+
|
274 |
+
|
275 |
+
player_games['S_H_B'] = player_games['Shots'] + player_games['Hits'] + player_games['Shots Blocked']
|
276 |
+
|
277 |
+
|
278 |
+
player_games = player_games.merge(right=team_games,left_on=['date','team'],right_on=['date','team_abb'])
|
279 |
+
|
280 |
from shiny import ui, render, App
|
281 |
import matplotlib.image as mpimg
|
282 |
# app_ui = ui.page_fluid(
|
|
|
309 |
ui.input_switch("a_d_id", "Ascending?"),
|
310 |
#ui.input_select("date_id", "Select Date",yahoo_weeks['Week'],width=1),
|
311 |
ui.input_date_range("date_range_id", "Date range input",start = datetime.today().date(), end = datetime.today().date() + timedelta(days=6)),
|
312 |
+
|
313 |
+
ui.input_date_range("scorer_date_id", "Streamer Date Range",start = max(datetime.today().date()- timedelta(days=21),pd.to_datetime('2023-10-10')), end = datetime.today().date()),
|
314 |
+
ui.input_numeric('min_games','Min. Games (Streamers)',value=3),
|
315 |
ui.output_table("result"),width=3),
|
316 |
|
317 |
|
318 |
+
ui.panel_main(
|
319 |
+
|
320 |
+
ui.navset_tab(
|
321 |
+
ui.nav("Schedule",
|
322 |
+
ui.tags.h3(""),
|
323 |
ui.div({"style": "font-size:2em;"},ui.output_text("txt_title")),
|
324 |
#ui.tags.h2("Fantasy Hockey Schedule Summary"),
|
325 |
ui.tags.h5("Created By: @TJStats, Data: NHL"),
|
|
|
328 |
ui.tags.h5('Legend'),
|
329 |
ui.output_table("schedule_result_legend"),
|
330 |
ui.tags.h6('An Off Night is defined as a day in which less than half the teams in the NHL are playing'),
|
331 |
+
ui.tags.h6('The scores are determined by using games played, off-nights, B2B, and strength of opponents') ),
|
332 |
+
|
333 |
+
ui.nav("Scorers Streamers",
|
334 |
+
ui.tags.h3(""),
|
335 |
+
ui.div({"style": "font-size:2.7em;"},ui.output_text("txt_title_streamers")),
|
336 |
+
ui.tags.h5("Created By: @TJStats, Data: NHL, Natural Stat Trick, Yahoo Fantasy"),
|
337 |
+
ui.div({"style": "font-size:2em;"},'Forwards Scoring Streamer Targets (<50% Rostered)'),
|
338 |
+
ui.div({"style": "font-size:1em;"},ui.output_text("txt_title_streamers_dates_f")),
|
339 |
+
ui.output_table("scorer_streamers_f"),
|
340 |
+
ui.tags.h3(""),
|
341 |
+
ui.div({"style": "font-size:2em;"},'Defence Scoring Streamer Targets (<50% Rostered)'),
|
342 |
+
ui.div({"style": "font-size:1em;"},ui.output_text("txt_title_streamers_dates_d")),
|
343 |
+
ui.output_table("scorer_streamers_d"),
|
344 |
+
|
345 |
+
),
|
346 |
+
|
347 |
+
ui.nav("Bangers Streamers",
|
348 |
+
ui.tags.h3(""),
|
349 |
+
ui.div({"style": "font-size:2.7em;"},ui.output_text("txt_title_streamers_bang")),
|
350 |
+
ui.tags.h5("Created By: @TJStats, Data: NHL, Natural Stat Trick, Yahoo Fantasy"),
|
351 |
+
ui.div({"style": "font-size:2em;"},'Forwards Bangers Streamer Targets (<50% Rostered)'),
|
352 |
+
ui.div({"style": "font-size:1em;"},ui.output_text("txt_title_streamers_dates_f_bang")),
|
353 |
+
ui.output_table("banger_streamers_f"),
|
354 |
+
ui.tags.h3(""),
|
355 |
+
ui.div({"style": "font-size:2em;"},'Defence Bangers Streamer Targets (<50% Rostered)'),
|
356 |
+
ui.div({"style": "font-size:1em;"},ui.output_text("txt_title_streamers_dates_d_bang")),
|
357 |
+
ui.output_table("banger_streamers_d"),
|
358 |
+
|
359 |
+
)
|
360 |
+
|
361 |
+
|
362 |
+
|
363 |
+
))))
|
364 |
# ui.row(
|
365 |
# ui.column(
|
366 |
# 3,
|
|
|
444 |
return f'Fantasy Hockey Schedule Summary - Yahoo - Week {input.week_id()}'
|
445 |
else:
|
446 |
return f'Fantasy Hockey Schedule Summary'
|
447 |
+
|
448 |
+
|
449 |
+
|
450 |
+
@output
|
451 |
+
@render.text
|
452 |
+
def txt_title_streamers():
|
453 |
+
week_set = int(input.week_id())
|
454 |
+
if week_set != 0:
|
455 |
+
return f'Fantasy Hockey Scorers Streamers - Week {input.week_id()}'
|
456 |
+
else:
|
457 |
+
return f'Fantasy Hockey Scorers Streamers'
|
458 |
+
|
459 |
+
@output
|
460 |
+
@render.text
|
461 |
+
def txt_title_streamers_bang():
|
462 |
+
week_set = int(input.week_id())
|
463 |
+
if week_set != 0:
|
464 |
+
return f'Fantasy Hockey Scorers Streamers - Week {input.week_id()}'
|
465 |
+
else:
|
466 |
+
return f'Fantasy Hockey Scorers Streamers'
|
467 |
+
|
468 |
+
|
469 |
+
@output
|
470 |
+
@render.text
|
471 |
+
def txt_title_streamers_dates_f():
|
472 |
+
return f'2023-24 Season - {input.scorer_date_id()[0]} to {input.scorer_date_id()[1]} (min. {input.min_games()} GP)'
|
473 |
+
|
474 |
+
@output
|
475 |
+
@render.text
|
476 |
+
def txt_title_streamers_dates_d():
|
477 |
+
return f'2023-24 Season - {input.scorer_date_id()[0]} to {input.scorer_date_id()[1]} (min. {input.min_games()} GP)'
|
478 |
+
|
479 |
+
|
480 |
+
def txt_title_streamers_bang():
|
481 |
+
week_set = int(input.week_id())
|
482 |
+
if week_set != 0:
|
483 |
+
return f'Fantasy Hockey Scorers Streamers - Week {input.week_id()}'
|
484 |
+
else:
|
485 |
+
return f'Fantasy Hockey Scorers Streamers'
|
486 |
+
|
487 |
+
|
488 |
+
@output
|
489 |
+
@render.text
|
490 |
+
def txt_title_streamers_dates_f_bang():
|
491 |
+
return f'2023-24 Season - {input.scorer_date_id()[0]} to {input.scorer_date_id()[1]} (min. {input.min_games()} GP)'
|
492 |
+
|
493 |
+
@output
|
494 |
+
@render.text
|
495 |
+
def txt_title_streamers_dates_d_bang():
|
496 |
+
return f'2023-24 Season - {input.scorer_date_id()[0]} to {input.scorer_date_id()[1]} (min. {input.min_games()} GP)'
|
497 |
|
498 |
@output
|
499 |
@render.table
|
|
|
663 |
|
664 |
return off_b2b_df_style
|
665 |
|
666 |
+
@output
|
667 |
+
@render.table
|
668 |
+
def scorer_streamers_f():
|
669 |
+
|
670 |
+
|
671 |
+
week_set = int(input.week_id())
|
672 |
+
print(week_set)
|
673 |
+
|
674 |
+
if week_set == 0:
|
675 |
+
start_point = input.date_range_id()[0]
|
676 |
+
end_point = input.date_range_id()[1]
|
677 |
+
else:
|
678 |
+
start_point = yahoo_weeks[yahoo_weeks.Number==week_set].reset_index(drop=True)['Start'][0]
|
679 |
+
end_point = yahoo_weeks[yahoo_weeks.Number==week_set].reset_index(drop=True)['End'][0]
|
680 |
+
|
681 |
+
|
682 |
+
sort_value='Score'
|
683 |
+
ascend=False
|
684 |
+
|
685 |
+
weekly_stack = schedule_stack[(schedule_stack['date'].dt.date>=start_point)&(schedule_stack['date'].dt.date<=end_point)]
|
686 |
+
date_list = pd.date_range(start_point,end_point,freq='d')
|
687 |
+
test_list = [[]] * len(date_list)
|
688 |
+
|
689 |
+
|
690 |
+
|
691 |
+
for i in range(0,len(date_list)):
|
692 |
+
test_list[i] = team_abv.merge(right=weekly_stack[weekly_stack['date']==date_list[i]],left_on='team_abv',right_on='team_abv',how='left')
|
693 |
+
test_list[i] = test_list[i].fillna("")
|
694 |
+
test_list[i]['new_text'] = test_list[i]['symbol'] + ' '+ test_list[i]['team_abv_home'] + test_list[i]['away_b2b']
|
695 |
+
|
696 |
+
|
697 |
+
test_df = pd.DataFrame()
|
698 |
+
test_df['Team'] = list(team_abv['team_abv'])
|
699 |
+
test_df['Total'] = test_df.merge(right=weekly_stack.groupby('team_abv')['team_abv'].apply(lambda x: x[x != ''].count()),left_on=['Team'],right_index=True,how='left').fillna(0)['team_abv']
|
700 |
+
|
701 |
+
test_df['Off-Night'] = test_df.merge(right=weekly_stack.groupby('team_abv').sum()['offnight_score'],left_on=['Team'],right_index=True,how='left').fillna(0)['offnight_score']
|
702 |
+
test_df['B2B']= test_df.merge(right=weekly_stack.groupby('team_abv').sum()['b2b'],left_on=['Team'],right_index=True,how='left').fillna(0)['b2b']
|
703 |
+
|
704 |
+
|
705 |
+
|
706 |
+
gf_rank = np.array(test_df.merge(right=weekly_stack.groupby('team_abv').mean()['GF_Rank'],left_on=['Team'],right_index=True,how='left').fillna(0)['GF_Rank'])
|
707 |
+
ga_rank = np.array(test_df.merge(right=weekly_stack.groupby('team_abv').mean()['GA_Rank'],left_on=['Team'],right_index=True,how='left').fillna(0)['GA_Rank'])
|
708 |
+
|
709 |
+
|
710 |
+
#games_vs_tired = np.array([float(i)*0.4 for i in list(weekly_stack.groupby('team_abv')['away_b2b'].apply(lambda x: x[x != ''].count()))])
|
711 |
+
|
712 |
+
games_vs_tired = 0.4*np.array(test_df.merge(right=weekly_stack.groupby('team_abv')['away_b2b'].apply(lambda x: x[x != ''].count()),left_on=['Team'],right_index=True,how='left').fillna(0)['away_b2b'])
|
713 |
+
|
714 |
+
print('test_df')
|
715 |
+
print(test_df)
|
716 |
+
#team_score = test_df['Total']+test_df['Off-Night']*0.5+test_df['B2B']*-0.2+games_vs_tired*0.3+gf_rank*0.1+ga_rank*0.1
|
717 |
+
team_score = test_df['Total']+test_df['Off-Night']*0.03+test_df['B2B']*-0.2+games_vs_tired*0.3+gf_rank*0.1+ga_rank*0.1
|
718 |
+
test_df['Score'] = team_score
|
719 |
+
|
720 |
+
test_df['Off-Night'] = test_df.merge(right=weekly_stack.groupby('team_abv').sum()['offnight'],left_on=['Team'],right_index=True,how='left').fillna(0)['offnight']
|
721 |
+
|
722 |
+
cols = test_df.columns.tolist();
|
723 |
+
L = len(cols)
|
724 |
+
test_df = test_df[cols[4:]+cols[0:4]]
|
725 |
+
#return test_df#[cols[4:]+cols[0:4]]
|
726 |
+
|
727 |
+
test_df = test_df.sort_values(by=[sort_value,'Score'],ascending = ascend)
|
728 |
+
|
729 |
+
for i in range(0,len(date_list)):
|
730 |
+
test_df[calendar.day_name[date_list[i].weekday()]+'<br>'+str(date_list[i].month)+'-'+'{:02d}'.format(date_list[i].day)] = test_list[i]['new_text']
|
731 |
+
|
732 |
+
row = ['']*L
|
733 |
+
for x in test_df[test_df.columns[L:]]:
|
734 |
+
row.append(int(sum(test_df[x]!=" ")/2))
|
735 |
+
|
736 |
+
test_df = test_df.sort_values(by=input.sort_id(),ascending=input.a_d_id())
|
737 |
+
|
738 |
+
test_df.loc[32] = row
|
739 |
+
#test_df_html = HTML( test_df.to_html().replace("\\n","<br>") )
|
740 |
+
offnight_list = [True if x <8 else False for x in test_df.iloc[-1][L:]]
|
741 |
+
|
742 |
+
|
743 |
+
|
744 |
+
|
745 |
+
start_date = input.scorer_date_id()[0]
|
746 |
+
end_date = input.scorer_date_id()[1]
|
747 |
+
gp_min = input.min_games()
|
748 |
+
|
749 |
+
df_dated = player_games[(pd.to_datetime(player_games.date) >= pd.to_datetime(start_date))&(pd.to_datetime(player_games.date) <= pd.to_datetime(end_date))]
|
750 |
+
|
751 |
+
team_top = test_df[test_df.Score!=''][np.array(test_df[test_df.Score!=''].Score) >= np.array(test_df[test_df.Score!='']['Score']).max()*0.8]['Team'].values
|
752 |
+
if(len(team_top)) < 5:
|
753 |
+
team_top = test_df.Team.values[:5]
|
754 |
+
|
755 |
+
df_dated_score_group = df_dated.groupby(['player_id','Player','Team','position','percent_owned']).agg(
|
756 |
+
GP = ('GP','sum'),
|
757 |
+
TOI = ('TOI','sum'),
|
758 |
+
Goals = ('Goals','sum'),
|
759 |
+
ixG = ('ixG','sum'),
|
760 |
+
Assists = ('Total Assists','sum'),
|
761 |
+
Points = ('Total Points','sum'),
|
762 |
+
Shots = ('Shots','sum'),
|
763 |
+
PP_Points = ('Total Points_pp','sum'),
|
764 |
+
PP_toi = ('TOI_pp','sum'),
|
765 |
+
team_pp = ('pp_toi','sum'),
|
766 |
+
)#.reset_index()
|
767 |
+
|
768 |
+
df_dated_score_group = df_dated_score_group[(df_dated_score_group.GP >= gp_min)]
|
769 |
+
df_dated_score_group[df_dated_score_group.columns[1:]] = df_dated_score_group[df_dated_score_group.columns[1:]].divide(df_dated_score_group.GP,axis=0)
|
770 |
+
df_dated_score_group['PP_percent'] = df_dated_score_group.PP_toi / df_dated_score_group.team_pp
|
771 |
+
df_dated_score_group = df_dated_score_group.reset_index()
|
772 |
+
df_dated_score_group = df_dated_score_group[(df_dated_score_group.Team.isin(team_top))&(df_dated_score_group.percent_owned <= .50 )]
|
773 |
+
df_dated_score_group['TOI'] = ["%d:%02d" % (int(x),(x*60)%60) for x in df_dated_score_group['TOI'].astype(float)]
|
774 |
+
df_dated_score_group_table = df_dated_score_group[['Player','Team' ,'position', 'percent_owned', 'GP', 'TOI',
|
775 |
+
'Goals', 'ixG', 'Assists', 'Points', 'Shots', 'PP_Points', 'PP_percent']].sort_values(['Points','PP_percent','Goals','Shots'],ascending=False)
|
776 |
+
|
777 |
+
df_dated_score_group_table.columns = ['Player','Team' ,'Position', 'Roster%', 'GP', 'TOI/GP',
|
778 |
+
'Goals/GP', 'ixG/GP','Assists/GP', 'Points/GP', 'Shots/GP', 'PPP/GP', 'PP%']
|
779 |
+
|
780 |
+
return df_dated_score_group_table[df_dated_score_group_table.Position != 'D'].head(15).style.background_gradient(cmap=cmap_off, subset=['Points/GP']).background_gradient(cmap=cmap_total, subset=['Roster%']).hide_index().set_properties(**{'Height': '12px'},**{'text-align': 'center'})\
|
781 |
+
.set_table_styles([{
|
782 |
+
'selector': 'caption',
|
783 |
+
'props': [
|
784 |
+
('color', ''),
|
785 |
+
('fontname', 'Century Gothic'),
|
786 |
+
('font-size', '20px'),
|
787 |
+
('font-style', 'italic'),
|
788 |
+
('font-weight', ''),
|
789 |
+
('text-align', 'centre'),
|
790 |
+
]
|
791 |
+
|
792 |
+
},{'selector' :'th', 'props':[('text-align', 'center'),('Height','5px'),('border', '1px black solid !important') ]},{'selector' :'td', 'props':[('text-align', 'center'),('font-size', '13px'),('border', '1px black solid !important')]}]).format(
|
793 |
+
{'Roster%': '{:.0%}','Goals/GP': '{:.2f}','ixG/GP': '{:.2f}','Assists/GP': '{:.2f}',
|
794 |
+
'Points/GP': '{:.2f}','Shots/GP': '{:.2f}','PPP/GP': '{:.2f}','PP%': '{:.0%}'},).set_properties(
|
795 |
+
**{'border': '1px black solid !important'}).set_properties(
|
796 |
+
**{'min-width':'175px'},subset = ((df_dated_score_group_table.columns[0])),overwrite=False).set_properties(
|
797 |
+
**{'min-width':'50px'},subset = ((df_dated_score_group_table.columns[1:])),overwrite=False)
|
798 |
+
|
799 |
+
|
800 |
+
@output
|
801 |
+
@render.table
|
802 |
+
def scorer_streamers_d():
|
803 |
+
|
804 |
+
|
805 |
+
week_set = int(input.week_id())
|
806 |
+
print(week_set)
|
807 |
+
|
808 |
+
if week_set == 0:
|
809 |
+
start_point = input.date_range_id()[0]
|
810 |
+
end_point = input.date_range_id()[1]
|
811 |
+
else:
|
812 |
+
start_point = yahoo_weeks[yahoo_weeks.Number==week_set].reset_index(drop=True)['Start'][0]
|
813 |
+
end_point = yahoo_weeks[yahoo_weeks.Number==week_set].reset_index(drop=True)['End'][0]
|
814 |
+
|
815 |
+
|
816 |
+
sort_value='Score'
|
817 |
+
ascend=False
|
818 |
+
|
819 |
+
weekly_stack = schedule_stack[(schedule_stack['date'].dt.date>=start_point)&(schedule_stack['date'].dt.date<=end_point)]
|
820 |
+
date_list = pd.date_range(start_point,end_point,freq='d')
|
821 |
+
test_list = [[]] * len(date_list)
|
822 |
+
|
823 |
+
|
824 |
+
|
825 |
+
for i in range(0,len(date_list)):
|
826 |
+
test_list[i] = team_abv.merge(right=weekly_stack[weekly_stack['date']==date_list[i]],left_on='team_abv',right_on='team_abv',how='left')
|
827 |
+
test_list[i] = test_list[i].fillna("")
|
828 |
+
test_list[i]['new_text'] = test_list[i]['symbol'] + ' '+ test_list[i]['team_abv_home'] + test_list[i]['away_b2b']
|
829 |
+
|
830 |
+
|
831 |
+
test_df = pd.DataFrame()
|
832 |
+
test_df['Team'] = list(team_abv['team_abv'])
|
833 |
+
test_df['Total'] = test_df.merge(right=weekly_stack.groupby('team_abv')['team_abv'].apply(lambda x: x[x != ''].count()),left_on=['Team'],right_index=True,how='left').fillna(0)['team_abv']
|
834 |
+
|
835 |
+
test_df['Off-Night'] = test_df.merge(right=weekly_stack.groupby('team_abv').sum()['offnight_score'],left_on=['Team'],right_index=True,how='left').fillna(0)['offnight_score']
|
836 |
+
test_df['B2B']= test_df.merge(right=weekly_stack.groupby('team_abv').sum()['b2b'],left_on=['Team'],right_index=True,how='left').fillna(0)['b2b']
|
837 |
+
|
838 |
+
|
839 |
+
|
840 |
+
gf_rank = np.array(test_df.merge(right=weekly_stack.groupby('team_abv').mean()['GF_Rank'],left_on=['Team'],right_index=True,how='left').fillna(0)['GF_Rank'])
|
841 |
+
ga_rank = np.array(test_df.merge(right=weekly_stack.groupby('team_abv').mean()['GA_Rank'],left_on=['Team'],right_index=True,how='left').fillna(0)['GA_Rank'])
|
842 |
+
|
843 |
+
|
844 |
+
#games_vs_tired = np.array([float(i)*0.4 for i in list(weekly_stack.groupby('team_abv')['away_b2b'].apply(lambda x: x[x != ''].count()))])
|
845 |
+
|
846 |
+
games_vs_tired = 0.4*np.array(test_df.merge(right=weekly_stack.groupby('team_abv')['away_b2b'].apply(lambda x: x[x != ''].count()),left_on=['Team'],right_index=True,how='left').fillna(0)['away_b2b'])
|
847 |
+
|
848 |
+
print('test_df')
|
849 |
+
print(test_df)
|
850 |
+
#team_score = test_df['Total']+test_df['Off-Night']*0.5+test_df['B2B']*-0.2+games_vs_tired*0.3+gf_rank*0.1+ga_rank*0.1
|
851 |
+
team_score = test_df['Total']+test_df['Off-Night']*0.03+test_df['B2B']*-0.2+games_vs_tired*0.3+gf_rank*0.1+ga_rank*0.1
|
852 |
+
test_df['Score'] = team_score
|
853 |
+
|
854 |
+
test_df['Off-Night'] = test_df.merge(right=weekly_stack.groupby('team_abv').sum()['offnight'],left_on=['Team'],right_index=True,how='left').fillna(0)['offnight']
|
855 |
+
|
856 |
+
cols = test_df.columns.tolist();
|
857 |
+
L = len(cols)
|
858 |
+
test_df = test_df[cols[4:]+cols[0:4]]
|
859 |
+
#return test_df#[cols[4:]+cols[0:4]]
|
860 |
+
|
861 |
+
test_df = test_df.sort_values(by=[sort_value,'Score'],ascending = ascend)
|
862 |
+
|
863 |
+
for i in range(0,len(date_list)):
|
864 |
+
test_df[calendar.day_name[date_list[i].weekday()]+'<br>'+str(date_list[i].month)+'-'+'{:02d}'.format(date_list[i].day)] = test_list[i]['new_text']
|
865 |
+
|
866 |
+
row = ['']*L
|
867 |
+
for x in test_df[test_df.columns[L:]]:
|
868 |
+
row.append(int(sum(test_df[x]!=" ")/2))
|
869 |
+
|
870 |
+
test_df = test_df.sort_values(by=input.sort_id(),ascending=input.a_d_id())
|
871 |
+
|
872 |
+
test_df.loc[32] = row
|
873 |
+
#test_df_html = HTML( test_df.to_html().replace("\\n","<br>") )
|
874 |
+
offnight_list = [True if x <8 else False for x in test_df.iloc[-1][L:]]
|
875 |
+
|
876 |
+
|
877 |
+
|
878 |
+
|
879 |
+
start_date = input.scorer_date_id()[0]
|
880 |
+
end_date = input.scorer_date_id()[1]
|
881 |
+
gp_min = input.min_games()
|
882 |
+
|
883 |
+
df_dated = player_games[(pd.to_datetime(player_games.date) >= pd.to_datetime(start_date))&(pd.to_datetime(player_games.date) <= pd.to_datetime(end_date))]
|
884 |
+
|
885 |
+
team_top = test_df[test_df.Score!=''][np.array(test_df[test_df.Score!=''].Score) >= np.array(test_df[test_df.Score!='']['Score']).max()*0.8]['Team'].values
|
886 |
+
if(len(team_top)) < 5:
|
887 |
+
team_top = test_df.Team.values[:5]
|
888 |
+
|
889 |
+
df_dated_score_group = df_dated.groupby(['player_id','Player','Team','position','percent_owned']).agg(
|
890 |
+
GP = ('GP','sum'),
|
891 |
+
TOI = ('TOI','sum'),
|
892 |
+
Goals = ('Goals','sum'),
|
893 |
+
ixG = ('ixG','sum'),
|
894 |
+
Assists = ('Total Assists','sum'),
|
895 |
+
Points = ('Total Points','sum'),
|
896 |
+
Shots = ('Shots','sum'),
|
897 |
+
PP_Points = ('Total Points_pp','sum'),
|
898 |
+
PP_toi = ('TOI_pp','sum'),
|
899 |
+
team_pp = ('pp_toi','sum'),
|
900 |
+
)#.reset_index()
|
901 |
+
|
902 |
+
df_dated_score_group = df_dated_score_group[(df_dated_score_group.GP >= gp_min)]
|
903 |
+
df_dated_score_group[df_dated_score_group.columns[1:]] = df_dated_score_group[df_dated_score_group.columns[1:]].divide(df_dated_score_group.GP,axis=0)
|
904 |
+
df_dated_score_group['PP_percent'] = df_dated_score_group.PP_toi / df_dated_score_group.team_pp
|
905 |
+
df_dated_score_group = df_dated_score_group.reset_index()
|
906 |
+
df_dated_score_group = df_dated_score_group[(df_dated_score_group.Team.isin(team_top))&(df_dated_score_group.percent_owned <= .50 )]
|
907 |
+
df_dated_score_group['TOI'] = ["%d:%02d" % (int(x),(x*60)%60) for x in df_dated_score_group['TOI'].astype(float)]
|
908 |
+
df_dated_score_group_table = df_dated_score_group[['Player','Team' ,'position', 'percent_owned', 'GP', 'TOI',
|
909 |
+
'Goals', 'ixG', 'Assists', 'Points', 'Shots', 'PP_Points', 'PP_percent']].sort_values(['Points','PP_percent','Goals','Shots'],ascending=False)
|
910 |
+
|
911 |
+
df_dated_score_group_table.columns = ['Player','Team' ,'Position', 'Roster%', 'GP', 'TOI/GP',
|
912 |
+
'Goals/GP', 'ixG/GP','Assists/GP', 'Points/GP', 'Shots/GP', 'PPP/GP', 'PP%']
|
913 |
+
|
914 |
+
return df_dated_score_group_table[df_dated_score_group_table.Position == 'D'].head(15).style.background_gradient(cmap=cmap_off, subset=['Points/GP']).background_gradient(cmap=cmap_total, subset=['Roster%']).hide_index().set_properties(**{'Height': '12px'},**{'text-align': 'center'})\
|
915 |
+
.set_table_styles([{
|
916 |
+
'selector': 'caption',
|
917 |
+
'props': [
|
918 |
+
('color', ''),
|
919 |
+
('fontname', 'Century Gothic'),
|
920 |
+
('font-size', '20px'),
|
921 |
+
('font-style', 'italic'),
|
922 |
+
('font-weight', ''),
|
923 |
+
('text-align', 'centre'),
|
924 |
+
]
|
925 |
+
|
926 |
+
},{'selector' :'th', 'props':[('text-align', 'center'),('Height','5px'),('border', '1px black solid !important') ]},{'selector' :'td', 'props':[('text-align', 'center'),('font-size', '13px'),('border', '1px black solid !important')]}]).format(
|
927 |
+
{'Roster%': '{:.0%}','Goals/GP': '{:.2f}','ixG/GP': '{:.2f}','Assists/GP': '{:.2f}',
|
928 |
+
'Points/GP': '{:.2f}','Shots/GP': '{:.2f}','PPP/GP': '{:.2f}','PP%': '{:.0%}'},).set_properties(
|
929 |
+
**{'border': '1px black solid !important'}).set_properties(
|
930 |
+
**{'min-width':'175px'},subset = ((df_dated_score_group_table.columns[0])),overwrite=False).set_properties(
|
931 |
+
**{'min-width':'50px'},subset = ((df_dated_score_group_table.columns[1:])),overwrite=False)
|
932 |
+
|
933 |
+
@output
|
934 |
+
@render.table
|
935 |
+
def banger_streamers_f():
|
936 |
+
|
937 |
+
|
938 |
+
week_set = int(input.week_id())
|
939 |
+
print(week_set)
|
940 |
+
|
941 |
+
if week_set == 0:
|
942 |
+
start_point = input.date_range_id()[0]
|
943 |
+
end_point = input.date_range_id()[1]
|
944 |
+
else:
|
945 |
+
start_point = yahoo_weeks[yahoo_weeks.Number==week_set].reset_index(drop=True)['Start'][0]
|
946 |
+
end_point = yahoo_weeks[yahoo_weeks.Number==week_set].reset_index(drop=True)['End'][0]
|
947 |
+
|
948 |
+
|
949 |
+
sort_value='Score'
|
950 |
+
ascend=False
|
951 |
+
|
952 |
+
weekly_stack = schedule_stack[(schedule_stack['date'].dt.date>=start_point)&(schedule_stack['date'].dt.date<=end_point)]
|
953 |
+
date_list = pd.date_range(start_point,end_point,freq='d')
|
954 |
+
test_list = [[]] * len(date_list)
|
955 |
+
|
956 |
+
|
957 |
+
|
958 |
+
for i in range(0,len(date_list)):
|
959 |
+
test_list[i] = team_abv.merge(right=weekly_stack[weekly_stack['date']==date_list[i]],left_on='team_abv',right_on='team_abv',how='left')
|
960 |
+
test_list[i] = test_list[i].fillna("")
|
961 |
+
test_list[i]['new_text'] = test_list[i]['symbol'] + ' '+ test_list[i]['team_abv_home'] + test_list[i]['away_b2b']
|
962 |
+
|
963 |
+
|
964 |
+
test_df = pd.DataFrame()
|
965 |
+
test_df['Team'] = list(team_abv['team_abv'])
|
966 |
+
test_df['Total'] = test_df.merge(right=weekly_stack.groupby('team_abv')['team_abv'].apply(lambda x: x[x != ''].count()),left_on=['Team'],right_index=True,how='left').fillna(0)['team_abv']
|
967 |
+
|
968 |
+
test_df['Off-Night'] = test_df.merge(right=weekly_stack.groupby('team_abv').sum()['offnight_score'],left_on=['Team'],right_index=True,how='left').fillna(0)['offnight_score']
|
969 |
+
test_df['B2B']= test_df.merge(right=weekly_stack.groupby('team_abv').sum()['b2b'],left_on=['Team'],right_index=True,how='left').fillna(0)['b2b']
|
970 |
+
|
971 |
+
|
972 |
+
|
973 |
+
gf_rank = np.array(test_df.merge(right=weekly_stack.groupby('team_abv').mean()['GF_Rank'],left_on=['Team'],right_index=True,how='left').fillna(0)['GF_Rank'])
|
974 |
+
ga_rank = np.array(test_df.merge(right=weekly_stack.groupby('team_abv').mean()['GA_Rank'],left_on=['Team'],right_index=True,how='left').fillna(0)['GA_Rank'])
|
975 |
+
|
976 |
+
|
977 |
+
#games_vs_tired = np.array([float(i)*0.4 for i in list(weekly_stack.groupby('team_abv')['away_b2b'].apply(lambda x: x[x != ''].count()))])
|
978 |
+
|
979 |
+
games_vs_tired = 0.4*np.array(test_df.merge(right=weekly_stack.groupby('team_abv')['away_b2b'].apply(lambda x: x[x != ''].count()),left_on=['Team'],right_index=True,how='left').fillna(0)['away_b2b'])
|
980 |
+
|
981 |
+
print('test_df')
|
982 |
+
print(test_df)
|
983 |
+
#team_score = test_df['Total']+test_df['Off-Night']*0.5+test_df['B2B']*-0.2+games_vs_tired*0.3+gf_rank*0.1+ga_rank*0.1
|
984 |
+
team_score = test_df['Total']+test_df['Off-Night']*0.03+test_df['B2B']*-0.2+games_vs_tired*0.3+gf_rank*0.1+ga_rank*0.1
|
985 |
+
test_df['Score'] = team_score
|
986 |
|
987 |
+
test_df['Off-Night'] = test_df.merge(right=weekly_stack.groupby('team_abv').sum()['offnight'],left_on=['Team'],right_index=True,how='left').fillna(0)['offnight']
|
988 |
+
|
989 |
+
cols = test_df.columns.tolist();
|
990 |
+
L = len(cols)
|
991 |
+
test_df = test_df[cols[4:]+cols[0:4]]
|
992 |
+
#return test_df#[cols[4:]+cols[0:4]]
|
993 |
+
|
994 |
+
test_df = test_df.sort_values(by=[sort_value,'Score'],ascending = ascend)
|
995 |
+
|
996 |
+
for i in range(0,len(date_list)):
|
997 |
+
test_df[calendar.day_name[date_list[i].weekday()]+'<br>'+str(date_list[i].month)+'-'+'{:02d}'.format(date_list[i].day)] = test_list[i]['new_text']
|
998 |
+
|
999 |
+
row = ['']*L
|
1000 |
+
for x in test_df[test_df.columns[L:]]:
|
1001 |
+
row.append(int(sum(test_df[x]!=" ")/2))
|
1002 |
+
|
1003 |
+
test_df = test_df.sort_values(by=input.sort_id(),ascending=input.a_d_id())
|
1004 |
+
|
1005 |
+
test_df.loc[32] = row
|
1006 |
+
#test_df_html = HTML( test_df.to_html().replace("\\n","<br>") )
|
1007 |
+
offnight_list = [True if x <8 else False for x in test_df.iloc[-1][L:]]
|
1008 |
+
|
1009 |
+
|
1010 |
+
|
1011 |
+
|
1012 |
+
start_date = input.scorer_date_id()[0]
|
1013 |
+
end_date = input.scorer_date_id()[1]
|
1014 |
+
gp_min = input.min_games()
|
1015 |
+
|
1016 |
+
df_dated = player_games[(pd.to_datetime(player_games.date) >= pd.to_datetime(start_date))&(pd.to_datetime(player_games.date) <= pd.to_datetime(end_date))]
|
1017 |
+
|
1018 |
+
team_top = test_df[test_df.Score!=''][np.array(test_df[test_df.Score!=''].Score) >= np.array(test_df[test_df.Score!='']['Score']).max()*0.8]['Team'].values
|
1019 |
+
if(len(team_top)) < 5:
|
1020 |
+
team_top = test_df.Team.values[:5]
|
1021 |
+
|
1022 |
+
df_dated_bangers_group = df_dated.groupby(['player_id','Player','Team','position','percent_owned']).agg(
|
1023 |
+
GP = ('GP','sum'),
|
1024 |
+
TOI = ('TOI','sum'),
|
1025 |
+
Goals = ('Goals','sum'),
|
1026 |
+
Assists = ('Total Assists','sum'),
|
1027 |
+
Points = ('Total Points','sum'),
|
1028 |
+
Shots = ('Shots','sum'),
|
1029 |
+
Hits = ('Total Points','sum'),
|
1030 |
+
Blocks = ('Shots Blocked','sum'),
|
1031 |
+
S_H_B = ('S_H_B','sum'),
|
1032 |
+
)#.reset_index()
|
1033 |
+
|
1034 |
+
df_dated_bangers_group = df_dated_bangers_group[(df_dated_bangers_group.GP >= gp_min)]
|
1035 |
+
df_dated_bangers_group[df_dated_bangers_group.columns[1:]] = df_dated_bangers_group[df_dated_bangers_group.columns[1:]].divide(df_dated_bangers_group.GP,axis=0)
|
1036 |
+
#df_dated_bangers_group['PP_percent'] = df_dated_bangers_group.PP_toi / df_dated_bangers_group.team_pp
|
1037 |
+
df_dated_bangers_group = df_dated_bangers_group.reset_index()
|
1038 |
+
df_dated_bangers_group = df_dated_bangers_group[(df_dated_bangers_group.Team.isin(team_top))&(df_dated_bangers_group.percent_owned <= .50 )]
|
1039 |
+
df_dated_bangers_group['TOI'] = ["%d:%02d" % (int(x),(x*60)%60) for x in df_dated_bangers_group['TOI'].astype(float)]
|
1040 |
+
df_dated_bangers_group_table = df_dated_bangers_group[['Player','Team' ,'position', 'percent_owned', 'GP', 'TOI',
|
1041 |
+
'Goals', 'Assists', 'Points', 'Shots', 'Hits', 'Blocks','S_H_B']].sort_values(['S_H_B','Shots','Hits','Blocks','Points'],ascending=False)
|
1042 |
+
|
1043 |
+
df_dated_bangers_group_table.columns = ['Player','Team' ,'Position', 'Roster%', 'GP', 'TOI/GP',
|
1044 |
+
'Goals/GP', 'Assists/GP', 'Points/GP', 'Shots/GP', 'Hits/GP', 'Blocks/GP','S+H+B/GP']
|
1045 |
+
|
1046 |
+
#df_dated_bangers_group_table.head(15)
|
1047 |
+
|
1048 |
+
return df_dated_bangers_group_table[df_dated_bangers_group_table.Position != 'D'].head(15).style.background_gradient(cmap=cmap_off, subset=['S+H+B/GP']).background_gradient(cmap=cmap_total, subset=['Roster%']).hide_index().set_properties(**{'Height': '12px'},**{'text-align': 'center'})\
|
1049 |
+
.set_table_styles([{
|
1050 |
+
'selector': 'caption',
|
1051 |
+
'props': [
|
1052 |
+
('color', ''),
|
1053 |
+
('fontname', 'Century Gothic'),
|
1054 |
+
('font-size', '20px'),
|
1055 |
+
('font-style', 'italic'),
|
1056 |
+
('font-weight', ''),
|
1057 |
+
('text-align', 'centre'),
|
1058 |
+
]
|
1059 |
+
|
1060 |
+
},{'selector' :'th', 'props':[('text-align', 'center'),('Height','5px'),('border', '1px black solid !important') ]},{'selector' :'td', 'props':[('text-align', 'center'),('font-size', '13px'),('border', '1px black solid !important')]}]).format(
|
1061 |
+
{'Roster%': '{:.0%}','Goals/GP': '{:.2f}','Assists/GP': '{:.2f}','Points/GP': '{:.2f}','PIM/GP': '{:.2f}','Shots/GP': '{:.2f}','Hits/GP': '{:.2f}','Blocks/GP': '{:.2f}','S+H+B/GP': '{:.2f}'},).set_properties(
|
1062 |
+
**{'border': '1px black solid !important'}).set_properties(
|
1063 |
+
**{'min-width':'175px'},subset = ((df_dated_bangers_group_table.columns[0])),overwrite=False).set_properties(
|
1064 |
+
**{'min-width':'50px'},subset = ((df_dated_bangers_group_table.columns[1:])),overwrite=False)
|
1065 |
+
|
1066 |
+
|
1067 |
+
@output
|
1068 |
+
@render.table
|
1069 |
+
def banger_streamers_d():
|
1070 |
+
|
1071 |
+
|
1072 |
+
week_set = int(input.week_id())
|
1073 |
+
print(week_set)
|
1074 |
+
|
1075 |
+
if week_set == 0:
|
1076 |
+
start_point = input.date_range_id()[0]
|
1077 |
+
end_point = input.date_range_id()[1]
|
1078 |
+
else:
|
1079 |
+
start_point = yahoo_weeks[yahoo_weeks.Number==week_set].reset_index(drop=True)['Start'][0]
|
1080 |
+
end_point = yahoo_weeks[yahoo_weeks.Number==week_set].reset_index(drop=True)['End'][0]
|
1081 |
+
|
1082 |
+
|
1083 |
+
sort_value='Score'
|
1084 |
+
ascend=False
|
1085 |
+
|
1086 |
+
weekly_stack = schedule_stack[(schedule_stack['date'].dt.date>=start_point)&(schedule_stack['date'].dt.date<=end_point)]
|
1087 |
+
date_list = pd.date_range(start_point,end_point,freq='d')
|
1088 |
+
test_list = [[]] * len(date_list)
|
1089 |
+
|
1090 |
+
|
1091 |
+
|
1092 |
+
for i in range(0,len(date_list)):
|
1093 |
+
test_list[i] = team_abv.merge(right=weekly_stack[weekly_stack['date']==date_list[i]],left_on='team_abv',right_on='team_abv',how='left')
|
1094 |
+
test_list[i] = test_list[i].fillna("")
|
1095 |
+
test_list[i]['new_text'] = test_list[i]['symbol'] + ' '+ test_list[i]['team_abv_home'] + test_list[i]['away_b2b']
|
1096 |
+
|
1097 |
+
|
1098 |
+
test_df = pd.DataFrame()
|
1099 |
+
test_df['Team'] = list(team_abv['team_abv'])
|
1100 |
+
test_df['Total'] = test_df.merge(right=weekly_stack.groupby('team_abv')['team_abv'].apply(lambda x: x[x != ''].count()),left_on=['Team'],right_index=True,how='left').fillna(0)['team_abv']
|
1101 |
+
|
1102 |
+
test_df['Off-Night'] = test_df.merge(right=weekly_stack.groupby('team_abv').sum()['offnight_score'],left_on=['Team'],right_index=True,how='left').fillna(0)['offnight_score']
|
1103 |
+
test_df['B2B']= test_df.merge(right=weekly_stack.groupby('team_abv').sum()['b2b'],left_on=['Team'],right_index=True,how='left').fillna(0)['b2b']
|
1104 |
+
|
1105 |
+
|
1106 |
+
|
1107 |
+
gf_rank = np.array(test_df.merge(right=weekly_stack.groupby('team_abv').mean()['GF_Rank'],left_on=['Team'],right_index=True,how='left').fillna(0)['GF_Rank'])
|
1108 |
+
ga_rank = np.array(test_df.merge(right=weekly_stack.groupby('team_abv').mean()['GA_Rank'],left_on=['Team'],right_index=True,how='left').fillna(0)['GA_Rank'])
|
1109 |
+
|
1110 |
+
|
1111 |
+
#games_vs_tired = np.array([float(i)*0.4 for i in list(weekly_stack.groupby('team_abv')['away_b2b'].apply(lambda x: x[x != ''].count()))])
|
1112 |
+
|
1113 |
+
games_vs_tired = 0.4*np.array(test_df.merge(right=weekly_stack.groupby('team_abv')['away_b2b'].apply(lambda x: x[x != ''].count()),left_on=['Team'],right_index=True,how='left').fillna(0)['away_b2b'])
|
1114 |
+
|
1115 |
+
print('test_df')
|
1116 |
+
print(test_df)
|
1117 |
+
#team_score = test_df['Total']+test_df['Off-Night']*0.5+test_df['B2B']*-0.2+games_vs_tired*0.3+gf_rank*0.1+ga_rank*0.1
|
1118 |
+
team_score = test_df['Total']+test_df['Off-Night']*0.03+test_df['B2B']*-0.2+games_vs_tired*0.3+gf_rank*0.1+ga_rank*0.1
|
1119 |
+
test_df['Score'] = team_score
|
1120 |
+
|
1121 |
+
test_df['Off-Night'] = test_df.merge(right=weekly_stack.groupby('team_abv').sum()['offnight'],left_on=['Team'],right_index=True,how='left').fillna(0)['offnight']
|
1122 |
+
|
1123 |
+
cols = test_df.columns.tolist();
|
1124 |
+
L = len(cols)
|
1125 |
+
test_df = test_df[cols[4:]+cols[0:4]]
|
1126 |
+
#return test_df#[cols[4:]+cols[0:4]]
|
1127 |
+
|
1128 |
+
test_df = test_df.sort_values(by=[sort_value,'Score'],ascending = ascend)
|
1129 |
+
|
1130 |
+
for i in range(0,len(date_list)):
|
1131 |
+
test_df[calendar.day_name[date_list[i].weekday()]+'<br>'+str(date_list[i].month)+'-'+'{:02d}'.format(date_list[i].day)] = test_list[i]['new_text']
|
1132 |
+
|
1133 |
+
row = ['']*L
|
1134 |
+
for x in test_df[test_df.columns[L:]]:
|
1135 |
+
row.append(int(sum(test_df[x]!=" ")/2))
|
1136 |
+
|
1137 |
+
test_df = test_df.sort_values(by=input.sort_id(),ascending=input.a_d_id())
|
1138 |
+
|
1139 |
+
test_df.loc[32] = row
|
1140 |
+
#test_df_html = HTML( test_df.to_html().replace("\\n","<br>") )
|
1141 |
+
offnight_list = [True if x <8 else False for x in test_df.iloc[-1][L:]]
|
1142 |
+
|
1143 |
+
|
1144 |
+
|
1145 |
+
|
1146 |
+
start_date = input.scorer_date_id()[0]
|
1147 |
+
end_date = input.scorer_date_id()[1]
|
1148 |
+
gp_min = input.min_games()
|
1149 |
+
|
1150 |
+
df_dated = player_games[(pd.to_datetime(player_games.date) >= pd.to_datetime(start_date))&(pd.to_datetime(player_games.date) <= pd.to_datetime(end_date))]
|
1151 |
+
|
1152 |
+
team_top = test_df[test_df.Score!=''][np.array(test_df[test_df.Score!=''].Score) >= np.array(test_df[test_df.Score!='']['Score']).max()*0.8]['Team'].values
|
1153 |
+
if(len(team_top)) < 5:
|
1154 |
+
team_top = test_df.Team.values[:5]
|
1155 |
+
|
1156 |
+
df_dated_bangers_group = df_dated.groupby(['player_id','Player','Team','position','percent_owned']).agg(
|
1157 |
+
GP = ('GP','sum'),
|
1158 |
+
TOI = ('TOI','sum'),
|
1159 |
+
Goals = ('Goals','sum'),
|
1160 |
+
Assists = ('Total Assists','sum'),
|
1161 |
+
Points = ('Total Points','sum'),
|
1162 |
+
Shots = ('Shots','sum'),
|
1163 |
+
Hits = ('Total Points','sum'),
|
1164 |
+
Blocks = ('Shots Blocked','sum'),
|
1165 |
+
S_H_B = ('S_H_B','sum'),
|
1166 |
+
)#.reset_index()
|
1167 |
+
|
1168 |
+
df_dated_bangers_group = df_dated_bangers_group[(df_dated_bangers_group.GP >= gp_min)]
|
1169 |
+
df_dated_bangers_group[df_dated_bangers_group.columns[1:]] = df_dated_bangers_group[df_dated_bangers_group.columns[1:]].divide(df_dated_bangers_group.GP,axis=0)
|
1170 |
+
#df_dated_bangers_group['PP_percent'] = df_dated_bangers_group.PP_toi / df_dated_bangers_group.team_pp
|
1171 |
+
df_dated_bangers_group = df_dated_bangers_group.reset_index()
|
1172 |
+
df_dated_bangers_group = df_dated_bangers_group[(df_dated_bangers_group.Team.isin(team_top))&(df_dated_bangers_group.percent_owned <= .50 )]
|
1173 |
+
df_dated_bangers_group['TOI'] = ["%d:%02d" % (int(x),(x*60)%60) for x in df_dated_bangers_group['TOI'].astype(float)]
|
1174 |
+
df_dated_bangers_group_table = df_dated_bangers_group[['Player','Team' ,'position', 'percent_owned', 'GP', 'TOI',
|
1175 |
+
'Goals', 'Assists', 'Points', 'Shots', 'Hits', 'Blocks','S_H_B']].sort_values(['S_H_B','Shots','Hits','Blocks','Points'],ascending=False)
|
1176 |
+
|
1177 |
+
df_dated_bangers_group_table.columns = ['Player','Team' ,'Position', 'Roster%', 'GP', 'TOI/GP',
|
1178 |
+
'Goals/GP', 'Assists/GP', 'Points/GP', 'Shots/GP', 'Hits/GP', 'Blocks/GP','S+H+B/GP']
|
1179 |
+
|
1180 |
+
#df_dated_bangers_group_table.head(15)
|
1181 |
+
|
1182 |
+
return df_dated_bangers_group_table[df_dated_bangers_group_table.Position == 'D'].head(15).style.background_gradient(cmap=cmap_off, subset=['S+H+B/GP']).background_gradient(cmap=cmap_total, subset=['Roster%']).hide_index().set_properties(**{'Height': '12px'},**{'text-align': 'center'})\
|
1183 |
+
.set_table_styles([{
|
1184 |
+
'selector': 'caption',
|
1185 |
+
'props': [
|
1186 |
+
('color', ''),
|
1187 |
+
('fontname', 'Century Gothic'),
|
1188 |
+
('font-size', '20px'),
|
1189 |
+
('font-style', 'italic'),
|
1190 |
+
('font-weight', ''),
|
1191 |
+
('text-align', 'centre'),
|
1192 |
+
]
|
1193 |
+
|
1194 |
+
},{'selector' :'th', 'props':[('text-align', 'center'),('Height','5px'),('border', '1px black solid !important') ]},{'selector' :'td', 'props':[('text-align', 'center'),('font-size', '13px'),('border', '1px black solid !important')]}]).format(
|
1195 |
+
{'Roster%': '{:.0%}','Goals/GP': '{:.2f}','Assists/GP': '{:.2f}','Points/GP': '{:.2f}','PIM/GP': '{:.2f}','Shots/GP': '{:.2f}','Hits/GP': '{:.2f}','Blocks/GP': '{:.2f}','S+H+B/GP': '{:.2f}'},).set_properties(
|
1196 |
+
**{'border': '1px black solid !important'}).set_properties(
|
1197 |
+
**{'min-width':'175px'},subset = ((df_dated_bangers_group_table.columns[0])),overwrite=False).set_properties(
|
1198 |
+
**{'min-width':'50px'},subset = ((df_dated_bangers_group_table.columns[1:])),overwrite=False)
|
1199 |
+
|
1200 |
+
|
1201 |
+
|
1202 |
+
|
1203 |
+
|
1204 |
+
|
1205 |
|
1206 |
|
1207 |
|