Spaces:
Running
Running
print('Running') | |
import time | |
import requests | |
import pandas as pd | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
from matplotlib.pyplot import figure | |
from matplotlib.offsetbox import OffsetImage, AnnotationBbox | |
from scipy import stats | |
import matplotlib.lines as mlines | |
import matplotlib.transforms as mtransforms | |
import numpy as np | |
import time | |
#import plotly.express as px | |
#!pip install chart_studio | |
#import chart_studio.tools as tls | |
from bs4 import BeautifulSoup | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import matplotlib.font_manager as font_manager | |
from datetime import datetime | |
import pytz | |
from matplotlib.ticker import MaxNLocator | |
from matplotlib.patches import Ellipse | |
import matplotlib.transforms as transforms | |
from matplotlib.gridspec import GridSpec | |
datetime.now(pytz.timezone('US/Pacific')).strftime('%B %d, %Y') | |
# Configure Notebook | |
#%matplotlib inline | |
plt.style.use('fivethirtyeight') | |
sns.set_context("notebook") | |
import warnings | |
warnings.filterwarnings('ignore') | |
# import yfpy | |
# from yfpy.query import YahooFantasySportsQuery | |
# import yahoo_oauth | |
import json | |
#import openpyxl | |
# from sklearn import preprocessing | |
from datetime import timedelta | |
# import dataframe_image as dfi | |
# from google.colab import drive | |
def percentile(n): | |
def percentile_(x): | |
return np.percentile(x, n) | |
percentile_.__name__ = 'percentile_%s' % n | |
return percentile_ | |
# import os | |
# import praw | |
# import matplotlib.pyplot as plt | |
# import matplotlib.colors | |
# import matplotlib.colors as mcolors | |
# cmap_sum = matplotlib.colors.LinearSegmentedColormap.from_list("", ["#4285f4","#FFFFFF","#F0E442"]) | |
#import pybaseball | |
import math | |
# import matplotlib.ticker as mtick | |
# import matplotlib.ticker as ticker | |
# colour_palette = ['#FFB000','#648FFF','#785EF0', | |
# '#DC267F','#FE6100','#3D1EB2','#894D80','#16AA02','#B5592B','#A3C1ED'] | |
# import matplotlib.colors as mcolors | |
# from matplotlib.ticker import FuncFormatter | |
# from matplotlib.font_manager import FontProperties | |
import numpy as np | |
# import matplotlib.pyplot as plt | |
import matplotlib.colors | |
import undetected_chromedriver as uc | |
from selenium import webdriver | |
options = webdriver.ChromeOptions() | |
options.add_argument("start-maximized") | |
driver = uc.Chrome(options=options) | |
driver.get('https://www.naturalstattrick.com') | |
#x,y,c = zip(*np.random.rand(30,3)*4-2) | |
#norm=plt.Normalize(-2,2) | |
co = matplotlib.colors.LinearSegmentedColormap.from_list("", ["#ffffff","#F0E442"]) | |
try: | |
data_r = requests.get("https://pub-api-ro.fantasysports.yahoo.com/fantasy/v2/league/427.l.public;out=settings/players;position=ALL;start=0;count=3000;sort=rank_season;search=;out=percent_owned;out=auction_values,ranks;ranks=season;ranks_by_position=season;out=expert_ranks;expert_ranks.rank_type=projected_season_remaining/draft_analysis;cut_types=diamond;slices=last7days?format=json_f").json() | |
key_check = data_r['fantasy_content']['league']['players'] | |
except KeyError: | |
data_r = requests.get("https://pub-api-ro.fantasysports.yahoo.com/fantasy/v2/league/427.l.public;out=settings/players;position=ALL;start=0;count=1151;sort=rank_season;search=;out=percent_owned;out=auction_values,ranks;ranks=season;ranks_by_position=season;out=expert_ranks;expert_ranks.rank_type=projected_season_remaining/draft_analysis;cut_types=diamond;slices=last7days?format=json_f").json() | |
print('key_checked') | |
total_list = [] | |
for x in data_r['fantasy_content']['league']['players']: | |
single_list = [] | |
single_list.append(int(x['player']['player_id'])) | |
single_list.append(int(x['player']['player_ranks'][0]['player_rank']['rank_value'])) | |
single_list.append(x['player']['name']['full']) | |
single_list.append(x['player']['name']['first']) | |
single_list.append(x['player']['name']['last']) | |
single_list.append(x['player']['draft_analysis']['average_pick']) | |
single_list.append(x['player']['average_auction_cost']) | |
single_list.append(x['player']['display_position']) | |
single_list.append(x['player']['editorial_team_abbr']) | |
if 'value' in x['player']['percent_owned']: | |
single_list.append(x['player']['percent_owned']['value']/100) | |
else: | |
single_list.append(0) | |
total_list.append(single_list) | |
yahoo_df = pd.DataFrame(total_list,columns = ['player_id','rank_value','full','first','last','average_pick','average_auction_cost','display_position','editorial_team_abbr','percent_owned']) | |
yahoo_df_2 = yahoo_df.copy() | |
# # Write your code here. | |
# response = requests.get("https://www.naturalstattrick.com/playerlist.php?fromseason=20232024&thruseason=20232024&stype=2&sit=all&stdoi=oi&rate=n") | |
# soup = BeautifulSoup(response.text, 'html.parser') | |
# table_rows = soup.findAll('tr') | |
# table_rows = table_rows[1:-1] | |
# table_rows[0].findAll('td') | |
# player_name = [] | |
# player_position = [] | |
# player_team = [] | |
# player_id = [] | |
# for i in range(0,len(table_rows)-1): | |
# player_name.append(str(table_rows[i].findAll('td')[0].contents[0])) | |
# player_position.append(table_rows[i].findAll('td')[1].contents[0]) | |
# player_team.append(table_rows[i].findAll('td')[2].contents[0]) | |
# player_id.append(str(table_rows[i].findAll('td')[3].contents[0])[-76:][:7]) | |
# player_id_df = pd.DataFrame({'Player':player_name,'Player ID':player_id,'Position':player_position,'Team':player_team}) | |
# #player_id_df.index.name = 'Player Name' | |
# player_id_df.head() | |
# skater_df = player_id_df[player_id_df['Position'] != 'G'] | |
# goalie_df = player_id_df[player_id_df['Position'] == 'G'] | |
season = 20232024 | |
seasontype = 2 | |
def nat_stat_trick_range_pp_gp(rookie='n',start_date='2022-10-01',end_date=str(pd.to_datetime(datetime.now(pytz.timezone('US/Pacific')).strftime('%Y-%m-%d')).date()),sit='all',gp=1): | |
time.sleep(2) | |
url = f'https://www.naturalstattrick.com/playerteams.php?fromseason={season}&thruseason={season}&stype={seasontype}&sit=pp&score=all&stdoi=std&rate=y&team=ALL&pos=S&loc=B&toi=0&gpfilt=gpteam&fd=&td=&tgp='+str(gp)+'&lines=single&draftteam=ALL' | |
player_list_all = [] | |
response = requests.get(url) | |
soup = BeautifulSoup(response.text, 'html.parser') | |
table_rows = soup.findAll('tr') | |
table_rows = table_rows[1:] | |
for j in range(0,len(table_rows)): | |
p_string = [str(x).strip('<td>').strip('</') for x in list(table_rows[j].findAll('td')) if "<td>" in str(x)] | |
player_list_all.append([p_string[0]]+[str(table_rows[j].findAll('td')[1]).split('>')[2].split('<')[0]]+p_string[1:]+[str(table_rows[j].findAll('td')[1])[98:105].strip('</a></td>')]) | |
#table_rows[0].findAll('td') | |
if soup != "": | |
columns_list = [str(x).split('>')[1].split('<')[0] for x in soup.findAll('th')]+['player_id'] | |
df_url = pd.DataFrame(data=player_list_all,columns=columns_list) | |
df_url = df_url.fillna(0) | |
df_url['Shots+Hits+Blocks/60'] = df_url['Shots/60'].astype(float)+df_url['Hits/60'].astype(float)+df_url['Shots Blocked/60'].astype(float) | |
df_url['Shots+Hits/60'] = df_url['Shots/60'].astype(float)+df_url['Hits/60'].astype(float) | |
#print(url) | |
return df_url | |
team_abv = pd.read_csv('team_abv.csv') | |
team_dict = team_abv.set_index('team_abv').to_dict()['team_name'] | |
yahoo_nhl_df = pd.read_csv('yahoo_to_nhl.csv', encoding='unicode_escape') | |
def nat_stat_convert(df): | |
for i in range(0,len(df.columns)): | |
if df.columns[i][-3:]=='/60': | |
if 'ix' not in df.columns[i]: | |
df[df.columns[i]] = np.round(df[df.columns[i]].astype(float)*df['TOI'].astype(float)/60,0) | |
df = df.rename(columns={df.columns[i]: df.columns[i].replace('/60','')}) | |
else: | |
df[df.columns[i]] = df[df.columns[i]].astype(float)*df['TOI'].astype(float)/60 | |
df = df.rename(columns={df.columns[i]: df.columns[i].replace('/60','')}) | |
df['Faceoffs %'] = df['Faceoffs Won']/(df['Faceoffs Won']+df['Faceoffs Lost']) | |
return df | |
from shiny import ui, render, App | |
import matplotlib.image as mpimg | |
app_ui = ui.page_fluid( | |
#ui.panel_title("Simulate a normal distribution"), | |
ui.layout_sidebar( | |
ui.panel_sidebar( | |
#ui.input_date_range("date_range_id", "Date range input",start = statcast_df.game_date.min(), end = statcast_df.game_date.max()), | |
ui.input_select("team_id", "Select Team",team_dict,width=1,size=1,selected='ANA',), | |
ui.input_numeric("n_1", "Last Games x", value=1), | |
ui.input_numeric("n_2", "Last Games y", value=0), | |
ui.input_numeric("n_3", "Last Games z", value=0), | |
ui.input_numeric("top_n", "Show top 'n'", value=10), | |
ui.input_switch("x", "Drop N/A"), | |
#ui.input_select("ignore_id", "Remove Columns",['Position','Roster%'],multiple=True,selectize=True), | |
), | |
ui.panel_main(ui.tags.h3(""), | |
ui.div({"style": "font-size:2.7em;"},ui.output_text("txt_title")), | |
#ui.tags.h2("Fantasy Hockey Schedule Summary"), | |
ui.tags.h5("Created By: @TJStats, Data: Natural Stat Trick, Yahoo Fantasy"), | |
ui.div({"style": "font-size:1.6em;"},ui.output_text("txt")), | |
ui.output_table("pp_roundup"), | |
#ui.tags.h5('Legend'), | |
#ui.tags.h6('An Off Night is defined as a day in which less than half the teams in the NHL are playing'), | |
#ui.tags.h6('The scores are determined by using games played, off-nights, B2B, and strength of opponents') ) | |
) | |
), | |
) | |
from urllib.request import Request, urlopen | |
from shiny import App, reactive, ui | |
from shiny.ui import h2, tags | |
# importing OpenCV(cv2) module | |
#print(app_ui) | |
def server(input, output, session): | |
def txt(): | |
return f'{team_dict[input.team_id()]} Last Games PP Summary' | |
def txt_title(): | |
return f'Team Last Games PP% Leaders' | |
def pp_roundup(): | |
top_n = input.top_n() | |
n_1 = input.n_1() | |
n_2 = input.n_2() | |
n_3 = input.n_3() | |
list_of_columns = ['Player', 'Team', 'display_position','percent_owned','L'+str(n_1)+' PP TOI','L'+str(n_2)+' PP TOI','L'+str(n_3)+' PP TOI', | |
'L'+str(n_1)+' PP%','L'+str(n_2)+' PP%','L'+str(n_3)+' PP%'] | |
list_of_columns_name = ['Player', 'Team', 'Position','Roster%','L'+str(n_1)+' PP TOI','L'+str(n_2)+' PP TOI','L'+str(n_3)+' PP TOI', | |
'L'+str(n_1)+' PP%','L'+str(n_2)+' PP%','L'+str(n_3)+' PP%'] | |
if type(n_1) is not int: | |
n_1 = 1 | |
if n_2 == 0: | |
list_of_columns.remove(f'L{str(n_2)} PP TOI') | |
list_of_columns.remove(f'L{str(n_2)} PP%') | |
list_of_columns_name.remove(f'L{str(n_2)} PP TOI') | |
list_of_columns_name.remove(f'L{str(n_2)} PP%') | |
if n_3 == 0: | |
list_of_columns.remove(f'L{str(n_3)} PP TOI') | |
list_of_columns.remove(f'L{str(n_3)} PP%') | |
list_of_columns_name.remove(f'L{str(n_3)} PP TOI') | |
list_of_columns_name.remove(f'L{str(n_3)} PP%') | |
start_date ='2023-09-01' | |
end_date = '2024-05-01' | |
df_pp_1 = nat_stat_trick_range_pp_gp(rookie='n',start_date = start_date,end_date = end_date, sit='pp',gp=n_1) | |
df_pp_2 = nat_stat_trick_range_pp_gp(rookie='n',start_date = start_date,end_date = end_date, sit='pp',gp=n_2) | |
df_pp_3 = nat_stat_trick_range_pp_gp(rookie='n',start_date = start_date,end_date = end_date, sit='pp',gp=n_3) | |
df_all_pp_1 = nat_stat_convert(df_pp_1) | |
df_all_pp_2 = nat_stat_convert(df_pp_2) | |
df_all_pp_3 = nat_stat_convert(df_pp_3) | |
df_final = df_all_pp_1.merge( df_all_pp_2,how='outer',left_on=['player_id'],right_on=['player_id'],suffixes=("","_2")) | |
df_final = df_final.merge( df_all_pp_3,how='outer',left_on=['player_id'],right_on=['player_id'],suffixes=("_1","_3")) | |
time.sleep(2) | |
team_report_1 = pd.read_html(f'https://www.naturalstattrick.com/teamtable.php?fromseason={season}&thruseason={season}&stype={seasontype}&sit=pp&score=all&rate=n&team=all&loc=B&gpf=c&gp='+str(n_1)+'&fd=&td=')[0] | |
time.sleep(2) | |
team_report_2 = pd.read_html(f'https://www.naturalstattrick.com/teamtable.php?fromseason={season}&thruseason={season}&stype={seasontype}&sit=pp&score=all&rate=n&team=all&loc=B&gpf=c&gp='+str(n_2)+'&fd=&td=')[0] | |
time.sleep(2) | |
team_report_3 = pd.read_html(f'https://www.naturalstattrick.com/teamtable.php?fromseason=fromseason={season}&thruseason={season}&stype={seasontype}&sit=pp&score=all&rate=n&team=all&loc=B&gpf=c&gp='+str(n_3)+'&fd=&td=')[0] | |
team_report_1 = team_report_1.merge(team_abv,left_on=['Team'],right_on=['team_name'],how='left') | |
team_report_2 = team_report_2.merge(team_abv,left_on=['Team'],right_on=['team_name'],how='left') | |
team_report_3 = team_report_3.merge(team_abv,left_on=['Team'],right_on=['team_name'],how='left') | |
test = df_final[['player_id','Player_1','Team_1','Position_1','TOI_1','TOI_2','TOI_3',]] | |
test.columns = ['player_id','Player','Team','Position','TOI_1','TOI_2','TOI_3',] | |
test = test.merge(team_report_1[['TOI','team_abv']],how='left',left_on=['Team'],right_on=['team_abv'], suffixes=('','_1_team')) | |
test = test.merge(team_report_2[['TOI','team_abv']],how='left',left_on=['Team'],right_on=['team_abv'], suffixes=('','_2_team')) | |
test = test.merge(team_report_3[['TOI','team_abv']],how='left',left_on=['Team'],right_on=['team_abv'], suffixes=('','_3_team')) | |
test = test.fillna('0:00') | |
test['TOI'] = [int(x[:-3])+int(x[-2:])*100/60/100 for x in test['TOI'] ] | |
test['TOI_1'] = [x if x!='0:00' else 0 for x in test['TOI_1']] | |
test['TOI_2'] = [x if x!='0:00' else 0 for x in test['TOI_2']] | |
test['TOI_3'] = [x if x!='0:00' else 0 for x in test['TOI_3']] | |
test['TOI_2_team'] = [int(x[:-3])+int(x[-2:])*100/60/100 for x in test['TOI_2_team'] ] | |
test['TOI_3_team'] = [int(x[:-3])+int(x[-2:])*100/60/100 for x in test['TOI_3_team']] | |
test['TOI'] = test['TOI'].astype(float) | |
test['PP%_1'] = test['TOI_1'].astype(float)/ test['TOI'].astype(float) | |
test['PP%_2'] = test['TOI_2'].astype(float)/ test['TOI_2_team'].astype(float) | |
test['PP%_3'] = test['TOI_3'].astype(float)/ test['TOI_3_team'].astype(float) | |
test = test.fillna(0) | |
test['TOI_1'] = ["%d:%02d" % (int(x),(x*60)%60) for x in test['TOI_1'].astype(float)] | |
test['TOI_2'] = ["%d:%02d" % (int(x),(x*60)%60) for x in test['TOI_2'].astype(float)] | |
test['TOI_3'] = ["%d:%02d" % (int(x),(x*60)%60) for x in test['TOI_3'].astype(float)] | |
test = test.drop(['team_abv','team_abv_2_team','team_abv_3_team','TOI','TOI_2_team','TOI_3_team'],axis=1) | |
test.columns = ['player_id','Player','Team','Position','L'+str(n_1)+' PP TOI','L'+str(n_2)+' PP TOI','L'+str(n_3)+' PP TOI','L'+str(n_1)+' PP%','L'+str(n_2)+' PP%','L'+str(n_3)+' PP%'] | |
yahoo_df = yahoo_df_2.merge(yahoo_nhl_df,left_on = 'player_id',right_on='player_id_yahoo',suffixes=['','_y']) | |
yahoo_df.nhl_id = yahoo_df.nhl_id.astype(float) | |
test.player_id = test.player_id.astype(float) | |
test = test.merge(right=yahoo_df,left_on='player_id',right_on='nhl_id',suffixes=['','_y'],how='left') | |
test.loc[test.display_position.isna(),'display_position'] = test.loc[test.display_position.isna(),'Position'] | |
test.display_position = test.display_position.replace({'L':'LW','R':'RW'}) | |
test.percent_owned = test.percent_owned.fillna(0) | |
print('Column List') | |
print(test.columns) | |
print(list_of_columns) | |
test = test[list_of_columns] | |
test = test.rename(columns={'percent_owned':'Roster%'}) | |
test = test.rename(columns={'display_position':'Position'}) | |
top_d_score = test[(test.Team==input.team_id())].sort_values(by=['L'+str(n_1)+' PP%'],ascending=False).reset_index(drop=True) | |
if input.x(): | |
top_d_score = top_d_score.dropna(axis='columns') | |
top_d_score = top_d_score.head(min(len(top_d_score),top_n)) | |
#top_d_score.columns = list_of_columns_name | |
top_d_score['Deployment'] = "PP2" | |
top_d_score['Deployment'][0:5] = "PP1" | |
cols = top_d_score.columns.tolist(); | |
print('we made it here',cols) | |
# for i in list(input.ignore_id()): | |
# print('we made it here') | |
# print(i) | |
# cols.remove(i) | |
# df_style_bang = top_d_score.head(10).style.background_gradient(cmap=co, subset=['L'+str(n_1)+' PP%','L'+str(n_2)+' PP%','L'+str(n_3)+' PP%','Roster%']).hide_index().set_properties(**{'Height': '12px'},**{'text-align': 'center'}).set_table_styles([{ | |
# 'selector': 'caption', | |
# 'props': [ | |
# ('color', ''), | |
# ('fontname', 'Century Gothic'), | |
# ('font-size', '20px'), | |
# ('font-style', 'italic'), | |
# ('font-weight', ''), | |
# ('text-align', 'centre'), | |
# ] | |
# },{'selector' :'th', 'props':[('text-align', 'center'),('Height','5px')]},{'selector' :'td', 'props':[('text-align', 'center'),('font-size', '13px'),('fontname', 'Century Gothic')]}]).format( | |
# {'L'+str(n_1)+' PP%': '{:.0%}', | |
# 'L'+str(n_2)+' PP%': '{:.0%}', | |
# 'L'+str(n_3)+' PP%': '{:.0%}', | |
# 'Roster%': '{:.0%}', | |
# },) | |
df_style_bang = top_d_score[cols].head(input.top_n()).style.background_gradient(cmap=co,vmin=0,vmax=1, subset=[x for x in cols if x.endswith('PP%')]).set_properties(**{'border': '3 px'},overwrite=False).set_table_styles([{ | |
'selector': 'caption', | |
'props': [ | |
('color', ''), | |
('fontname', 'Century Gothic'), | |
('font-size', '20px'), | |
('font-style', 'italic'), | |
('font-weight', ''), | |
('text-align', 'centre'), | |
] | |
},{'selector' :'th', 'props':[('text-align', 'center'),('Height','px'),('color','black'),( | |
'border', '1px black solid !important')]},{'selector' :'td', 'props':[('text-align', 'center'),('font-size', '18px'),('color','black')]}],overwrite=False).set_properties( | |
**{'background-color':'White','index':'White','min-width':'100px'},overwrite=False).set_properties( | |
**{'background-color':'White','index':'White','min-width':'200px'},overwrite=False,subset=cols[0]).set_table_styles( | |
[{'selector': 'th:first-child', 'props': [('background-color', 'white')]}],overwrite=False).set_table_styles( | |
[{'selector': 'tr:first-child', 'props': [('background-color', 'white')]}],overwrite=False).set_table_styles( | |
[{'selector': 'tr', 'props': [('line-height', '35px')]}],overwrite=False).set_properties( | |
**{'Height': '35px'},**{'text-align': 'center'},overwrite=False).set_properties(**{'border': '3 px','color':'black'},overwrite=False).set_properties(**{'border': '3 px','color':'black'},overwrite=False).set_properties( | |
**{'border': '1px black solid !important'},subset = ((list(top_d_score.index[:]),top_d_score.columns[:]))).set_properties(**{ | |
'color': 'black'},overwrite=False).set_properties( | |
**{'border': '1px black solid !important'},subset = ((list(top_d_score.index[:]),top_d_score.columns[:]))).format( | |
{'L'+str(n_1)+' PP%': '{:.0%}', | |
'L'+str(n_2)+' PP%': '{:.0%}', | |
'L'+str(n_3)+' PP%': '{:.0%}', | |
'Roster%': '{:.0%}', | |
},).background_gradient(cmap=co, subset=[x for x in cols if x.endswith('PP%')]).hide_index() | |
return df_style_bang | |
# test = test.fillna(0) | |
#test['PP TOI'] = ["%d:%02d" % (int(x),(x*60)%60) if x>0 else '0:00' for x in test['PP TOI']] | |
app = App(app_ui, server) | |
time.sleep(60) |