Spaces:
Build error
Build error
Commit
·
248dee2
1
Parent(s):
5dca563
Add weekly pitcher leaderboard
Browse files- daily_pitcher_leaderboard.py +59 -27
- pitch_leaderboard.py +5 -4
daily_pitcher_leaderboard.py
CHANGED
@@ -18,8 +18,25 @@ df = (
|
|
18 |
)
|
19 |
|
20 |
|
21 |
-
def
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
other_cols = ['Name']
|
25 |
|
@@ -68,14 +85,15 @@ def get_pitcher_leaderboards(date, top_players, strict, ignore_zero_whiffs, show
|
|
68 |
.with_row_index(offset=1)
|
69 |
.rename({'index': 'Rank'})
|
70 |
)
|
71 |
-
|
72 |
-
return
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
gr.update(interactive=True)
|
78 |
-
|
|
|
79 |
|
80 |
|
81 |
def go_back_day(date):
|
@@ -83,6 +101,12 @@ def go_back_day(date):
|
|
83 |
|
84 |
def go_forward_day(date):
|
85 |
return date + datetime.timedelta(days=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
def create_daily_pitcher_leaderboard():
|
88 |
with gr.Blocks(
|
@@ -111,31 +135,39 @@ def create_daily_pitcher_leaderboard():
|
|
111 |
search_btn = gr.Button('Search', scale=1, min_width=100)
|
112 |
|
113 |
with gr.Row():
|
114 |
-
|
115 |
-
|
|
|
|
|
116 |
|
117 |
|
118 |
-
|
119 |
with gr.Row():
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
search_kwargs = dict(
|
124 |
-
fn=
|
125 |
inputs=[date_picker, top_players, strict, ignore_zero_whiffs, show_rank, debug],
|
126 |
-
outputs=[
|
127 |
)
|
128 |
search_btn.click(**search_kwargs)
|
129 |
-
(
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
139 |
|
140 |
|
141 |
|
|
|
18 |
)
|
19 |
|
20 |
|
21 |
+
def filter_pitcher_leaderboard_by_date(date, *args, **kwargs):
|
22 |
+
day_df = df.filter(pl.col('game_date') == date)
|
23 |
+
monday = date - datetime.timedelta(days=date.weekday())
|
24 |
+
sunday = date + datetime.timedelta(days=6-date.weekday())
|
25 |
+
week_df = df.filter((pl.col('game_date') >= monday) & (pl.col('game_date') <= sunday))
|
26 |
+
return (
|
27 |
+
f'<center><h1>Daily Leaderboard<h1><h2>{date.strftime("%B %d, %Y")}</h2><h3>{date.strftime("%A")}</h3></center>',
|
28 |
+
f'<center><h1>Weekly Leaderboard<h1><h2>{monday.strftime("%B %d, %Y")} to {sunday.strftime("%B %d, %Y")}</h2><h3>{monday.strftime("%A")} to {sunday.strftime("%A")} </h3></center>',
|
29 |
+
*compute_pitcher_leaderboards(day_df, *args, **kwargs),
|
30 |
+
*compute_pitcher_leaderboards(week_df, *args, **kwargs),
|
31 |
+
gr.update(interactive=True),
|
32 |
+
gr.update(interactive=True),
|
33 |
+
gr.update(interactive=True),
|
34 |
+
gr.update(interactive=True)
|
35 |
+
)
|
36 |
+
|
37 |
+
def compute_pitcher_leaderboards(df, top_players, strict, ignore_zero_whiffs, show_rank, debug):
|
38 |
+
# _df = df.filter(pl.col('game_date') == date)
|
39 |
+
_df = df
|
40 |
|
41 |
other_cols = ['Name']
|
42 |
|
|
|
85 |
.with_row_index(offset=1)
|
86 |
.rename({'index': 'Rank'})
|
87 |
)
|
88 |
+
|
89 |
+
return whiffs, velos
|
90 |
+
# return (
|
91 |
+
# f'<center><h1>Daily Leaderboard<h1><h2>{date.strftime("%B %d, %Y")}</h2><h3>{date.strftime("%A")}</h3></center>',
|
92 |
+
# whiffs,
|
93 |
+
# velos,
|
94 |
+
# gr.update(interactive=True),
|
95 |
+
# gr.update(interactive=True)
|
96 |
+
# )
|
97 |
|
98 |
|
99 |
def go_back_day(date):
|
|
|
101 |
|
102 |
def go_forward_day(date):
|
103 |
return date + datetime.timedelta(days=1)
|
104 |
+
|
105 |
+
def go_back_week(date):
|
106 |
+
return date - datetime.timedelta(days=7)
|
107 |
+
|
108 |
+
def go_forward_week(date):
|
109 |
+
return date + datetime.timedelta(days=7)
|
110 |
|
111 |
def create_daily_pitcher_leaderboard():
|
112 |
with gr.Blocks(
|
|
|
135 |
search_btn = gr.Button('Search', scale=1, min_width=100)
|
136 |
|
137 |
with gr.Row():
|
138 |
+
prev_week_btn = gr.Button('Previous Week', interactive=False)
|
139 |
+
prev_day_btn = gr.Button('Previous Day', interactive=False)
|
140 |
+
next_day_btn = gr.Button('Next Day', interactive=False)
|
141 |
+
next_week_btn = gr.Button('Next Week', interactive=False)
|
142 |
|
143 |
|
144 |
+
daily_header = gr.HTML('<center><h1>Daily Leaderboard<h1><h2 style="display: none;"></h2><h3 style="display: none;"></h3></center>')
|
145 |
with gr.Row():
|
146 |
+
daily_whiffs = gr.Dataframe(pl.DataFrame({'Name': [], 'Whiffs': []}), label='Whiffs', interactive=False)
|
147 |
+
daily_velos = gr.Dataframe(pl.DataFrame({'Name': [], 'Velocity': []}), label='Velocity', interactive=False)
|
148 |
+
|
149 |
+
weekly_header = gr.HTML('<center><h1>Weekly Leaderboard<h1><h2 style="display: none;"></h2><h3 style="display: none;"></h3></center>')
|
150 |
+
with gr.Row():
|
151 |
+
weekly_whiffs = gr.Dataframe(pl.DataFrame({'Name': [], 'Whiffs': []}), label='Whiffs', interactive=False)
|
152 |
+
weekly_velos = gr.Dataframe(pl.DataFrame({'Name': [], 'Velocity': []}), label='Velocity', interactive=False)
|
153 |
|
154 |
search_kwargs = dict(
|
155 |
+
fn=filter_pitcher_leaderboard_by_date,
|
156 |
inputs=[date_picker, top_players, strict, ignore_zero_whiffs, show_rank, debug],
|
157 |
+
outputs=[daily_header, weekly_header, daily_whiffs, daily_velos, weekly_whiffs, weekly_velos, prev_day_btn, next_day_btn, prev_week_btn, next_week_btn]
|
158 |
)
|
159 |
search_btn.click(**search_kwargs)
|
160 |
+
for btn, fn in (
|
161 |
+
(prev_day_btn, go_back_day),
|
162 |
+
(next_day_btn, go_forward_day),
|
163 |
+
(prev_week_btn, go_back_week),
|
164 |
+
(next_week_btn, go_forward_week)
|
165 |
+
):
|
166 |
+
(
|
167 |
+
btn
|
168 |
+
.click(fn, date_picker, date_picker)
|
169 |
+
.then(**search_kwargs)
|
170 |
+
)
|
171 |
|
172 |
|
173 |
|
pitch_leaderboard.py
CHANGED
@@ -2,17 +2,18 @@ import gradio as gr
|
|
2 |
import polars as pl
|
3 |
|
4 |
|
5 |
-
from data import df, game_df, compute_pitch_stats
|
6 |
from gradio_function import *
|
7 |
from css import css
|
8 |
|
9 |
-
|
10 |
|
11 |
def filter_pitch_leaderboard(season, min_pitches):
|
12 |
return (
|
13 |
compute_pitch_stats(df.filter(pl.col('game_year') == season))
|
14 |
.filter(pl.col('Count') >= min_pitches)
|
15 |
.sort('CSW%', descending=True)
|
|
|
16 |
)
|
17 |
|
18 |
def create_pitch_leaderboard():
|
@@ -20,13 +21,13 @@ def create_pitch_leaderboard():
|
|
20 |
css=css
|
21 |
) as demo:
|
22 |
init_min_pitches = 100
|
23 |
-
init_season =
|
24 |
init_pitch_stats = filter_pitch_leaderboard(init_season, init_min_pitches)
|
25 |
init_pitch_stats.write_csv('pitch_leaderboard.csv')
|
26 |
pitch_leaderboard_df = gr.State(init_pitch_stats)
|
27 |
|
28 |
with gr.Row():
|
29 |
-
season = gr.
|
30 |
min_pitches = gr.Number(init_min_pitches, precision=0, label='Min. Pitches')
|
31 |
pitch_leaderboard_download_file = gr.DownloadButton(value='pitch_leaderboard.csv', label='Download leaderboard')
|
32 |
pitch_leaderboard = gr.Dataframe(value=pitch_leaderboard_df.value)
|
|
|
2 |
import polars as pl
|
3 |
|
4 |
|
5 |
+
from data import df, game_df, compute_pitch_stats, SEASONS
|
6 |
from gradio_function import *
|
7 |
from css import css
|
8 |
|
9 |
+
SEASONS = [int(season) for season in SEASONS]
|
10 |
|
11 |
def filter_pitch_leaderboard(season, min_pitches):
|
12 |
return (
|
13 |
compute_pitch_stats(df.filter(pl.col('game_year') == season))
|
14 |
.filter(pl.col('Count') >= min_pitches)
|
15 |
.sort('CSW%', descending=True)
|
16 |
+
.rename({'name': 'Player', 'pitch_name': 'Pitch'})
|
17 |
)
|
18 |
|
19 |
def create_pitch_leaderboard():
|
|
|
21 |
css=css
|
22 |
) as demo:
|
23 |
init_min_pitches = 100
|
24 |
+
init_season = max(SEASONS)
|
25 |
init_pitch_stats = filter_pitch_leaderboard(init_season, init_min_pitches)
|
26 |
init_pitch_stats.write_csv('pitch_leaderboard.csv')
|
27 |
pitch_leaderboard_df = gr.State(init_pitch_stats)
|
28 |
|
29 |
with gr.Row():
|
30 |
+
season = gr.Dropdown(choices=SEASONS, value=init_season, label='Season')
|
31 |
min_pitches = gr.Number(init_min_pitches, precision=0, label='Min. Pitches')
|
32 |
pitch_leaderboard_download_file = gr.DownloadButton(value='pitch_leaderboard.csv', label='Download leaderboard')
|
33 |
pitch_leaderboard = gr.Dataframe(value=pitch_leaderboard_df.value)
|