Spaces:
Sleeping
Sleeping
Commit
·
75e5ca4
1
Parent(s):
c00ad6e
Add features to daily pitcher leaderboard
Browse files- daily_pitcher_leaderboard.py +102 -15
daily_pitcher_leaderboard.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
from data import df, game_df
|
| 4 |
from gradio_function import *
|
| 5 |
from css import css
|
| 6 |
|
|
|
|
|
|
|
| 7 |
df = (
|
| 8 |
df
|
| 9 |
.join(game_df, on='game_pk').with_columns(pl.col('game_date').str.to_datetime())
|
|
@@ -14,42 +17,126 @@ df = (
|
|
| 14 |
)
|
| 15 |
|
| 16 |
|
| 17 |
-
def get_pitcher_leaderboards(
|
| 18 |
-
_df = df.filter(pl.col('game_date') ==
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
whiffs = (
|
| 20 |
_df
|
| 21 |
.group_by(['pitcher'])
|
| 22 |
.agg(
|
| 23 |
pl.col('whiff').sum().alias('Whiffs'),
|
| 24 |
-
pl.col(
|
| 25 |
)
|
| 26 |
-
.select(
|
| 27 |
.sort('Whiffs', descending=True)
|
| 28 |
-
[:10]
|
| 29 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
velos = (
|
| 31 |
_df
|
| 32 |
-
.select(
|
| 33 |
.drop_nulls()
|
| 34 |
.sort(['Velocity', 'Name'], descending=[True, False])
|
| 35 |
-
[:10]
|
| 36 |
)
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def create_daily_pitcher_leaderboard():
|
| 41 |
with gr.Blocks(
|
| 42 |
css=css
|
| 43 |
) as demo:
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
header = gr.HTML('<center><h1>Daily Leaderboard<h2><
|
| 48 |
with gr.Row():
|
| 49 |
-
whiffs = gr.Dataframe(pl.DataFrame({'Name': [], 'Whiffs': []}), label='Whiffs')
|
| 50 |
-
velos = gr.Dataframe(pl.DataFrame({'Name': [], 'Velocity': []}), label='Velocity')
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
return demo
|
| 55 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio_calendar import Calendar
|
| 3 |
|
| 4 |
from data import df, game_df
|
| 5 |
from gradio_function import *
|
| 6 |
from css import css
|
| 7 |
|
| 8 |
+
import datetime
|
| 9 |
+
|
| 10 |
df = (
|
| 11 |
df
|
| 12 |
.join(game_df, on='game_pk').with_columns(pl.col('game_date').str.to_datetime())
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
|
| 20 |
+
def get_pitcher_leaderboards(date, top_players, strict, ignore_zero_whiffs, show_rank, debug):
|
| 21 |
+
_df = df.filter(pl.col('game_date') == date)
|
| 22 |
+
|
| 23 |
+
other_cols = ['Name']
|
| 24 |
+
|
| 25 |
+
if debug:
|
| 26 |
+
other_cols = ['game_date'] + other_cols
|
| 27 |
+
|
| 28 |
whiffs = (
|
| 29 |
_df
|
| 30 |
.group_by(['pitcher'])
|
| 31 |
.agg(
|
| 32 |
pl.col('whiff').sum().alias('Whiffs'),
|
| 33 |
+
*[pl.col(col).first() for col in other_cols]
|
| 34 |
)
|
| 35 |
+
.select(*other_cols, 'Whiffs')
|
| 36 |
.sort('Whiffs', descending=True)
|
|
|
|
| 37 |
)
|
| 38 |
+
if ignore_zero_whiffs:
|
| 39 |
+
whiffs = whiffs.filter(pl.col('Whiffs') > 0)
|
| 40 |
+
if len(whiffs) >top_players:
|
| 41 |
+
whiffs = (
|
| 42 |
+
whiffs
|
| 43 |
+
.filter(pl.col('Whiffs') >= whiffs['Whiffs'][top_players])
|
| 44 |
+
)
|
| 45 |
+
if strict:
|
| 46 |
+
whiffs = whiffs[:top_players]
|
| 47 |
+
if show_rank:
|
| 48 |
+
whiffs = (
|
| 49 |
+
whiffs
|
| 50 |
+
.with_row_index(offset=1)
|
| 51 |
+
.rename({'index': 'Rank'})
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
velos = (
|
| 55 |
_df
|
| 56 |
+
.select(*other_cols, 'Velocity')
|
| 57 |
.drop_nulls()
|
| 58 |
.sort(['Velocity', 'Name'], descending=[True, False])
|
|
|
|
| 59 |
)
|
| 60 |
+
if len(velos) > top_players:
|
| 61 |
+
velos = velos.filter(pl.col('Velocity') >= velos['Velocity'][top_players])
|
| 62 |
+
if strict:
|
| 63 |
+
velos = velos[:top_players]
|
| 64 |
+
if show_rank:
|
| 65 |
+
velos = (
|
| 66 |
+
velos
|
| 67 |
+
.with_row_index(offset=1)
|
| 68 |
+
.rename({'index': 'Rank'})
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return (
|
| 72 |
+
f'<center><h1>Daily Leaderboard<h1><h2>{date.strftime("%B %d, %Y")}</h2><h3>{date.strftime("%A")}</h3></center>',
|
| 73 |
+
whiffs,
|
| 74 |
+
velos,
|
| 75 |
+
gr.update(interactive=True),
|
| 76 |
+
gr.update(interactive=True)
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def go_back_day(date):
|
| 81 |
+
return date - datetime.timedelta(days=1)
|
| 82 |
+
|
| 83 |
+
def go_forward_day(date):
|
| 84 |
+
return date + datetime.timedelta(days=1)
|
| 85 |
|
| 86 |
def create_daily_pitcher_leaderboard():
|
| 87 |
with gr.Blocks(
|
| 88 |
css=css
|
| 89 |
) as demo:
|
| 90 |
+
with gr.Row():
|
| 91 |
+
# date_picker = gr.DateTime(
|
| 92 |
+
# value=df['game_date'].max().strftime('%Y-%m-%d'),
|
| 93 |
+
# include_time=False,
|
| 94 |
+
# type='datetime',
|
| 95 |
+
# label='Date',
|
| 96 |
+
# scale=4
|
| 97 |
+
# )
|
| 98 |
+
date_picker = Calendar(
|
| 99 |
+
value=df['game_date'].max().strftime('%Y-%m-%d'),
|
| 100 |
+
type='datetime',
|
| 101 |
+
label='Date',
|
| 102 |
+
scale=2,
|
| 103 |
+
min_width=50
|
| 104 |
+
)
|
| 105 |
+
top_players = gr.Number(10, label='# Top players', scale=1, min_width=100)
|
| 106 |
+
strict = gr.Checkbox(False, label='Strict', info='Ignore ties and restrict to # top players', scale=1.5, min_width=100)
|
| 107 |
+
ignore_zero_whiffs = gr.Checkbox(False, label='Ignore zero whiffs', info='Ignore zero whiff players if in top ranked', scale=1.5, min_width=100)
|
| 108 |
+
show_rank = gr.Checkbox(False, label='Show rank', scale=1, min_width=100)
|
| 109 |
+
debug = gr.Checkbox(False, label='Debug', info='Show dates', scale=1, min_width=100)
|
| 110 |
+
search_btn = gr.Button('Search', scale=1, min_width=100)
|
| 111 |
+
|
| 112 |
+
with gr.Row():
|
| 113 |
+
prev_btn = gr.Button('Previous', interactive=False)
|
| 114 |
+
next_btn = gr.Button('Next', interactive=False)
|
| 115 |
+
|
| 116 |
|
| 117 |
+
header = gr.HTML('<center><h1>Daily Leaderboard<h1><h2 style="display: none;"></h2><h3 style="display: none;"></h3></center>')
|
| 118 |
with gr.Row():
|
| 119 |
+
whiffs = gr.Dataframe(pl.DataFrame({'Name': [], 'Whiffs': []}), label='Whiffs', interactive=False)
|
| 120 |
+
velos = gr.Dataframe(pl.DataFrame({'Name': [], 'Velocity': []}), label='Velocity', interactive=False)
|
| 121 |
|
| 122 |
+
search_kwargs = dict(
|
| 123 |
+
fn=get_pitcher_leaderboards,
|
| 124 |
+
inputs=[date_picker, top_players, strict, ignore_zero_whiffs, show_rank, debug],
|
| 125 |
+
outputs=[header, whiffs, velos, prev_btn, next_btn]
|
| 126 |
+
)
|
| 127 |
+
search_btn.click(**search_kwargs)
|
| 128 |
+
(
|
| 129 |
+
prev_btn
|
| 130 |
+
.click(go_back_day, date_picker, date_picker)
|
| 131 |
+
.then(**search_kwargs)
|
| 132 |
+
)
|
| 133 |
+
(
|
| 134 |
+
next_btn
|
| 135 |
+
.click(go_forward_day, date_picker, date_picker)
|
| 136 |
+
.then(**search_kwargs)
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
|
| 141 |
return demo
|
| 142 |
|