Spaces:
Build error
Build error
Commit
·
c00ad6e
1
Parent(s):
79797b0
Add daily pitcher leaderboard
Browse files- app.py +4 -0
- daily_pitcher_leaderboard.py +60 -0
- data.py +3 -1
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
|
3 |
from pitcher_dashboard import create_pitcher_dashboard
|
4 |
from pitch_leaderboard import create_pitch_leaderboard
|
|
|
5 |
from css import css
|
6 |
|
7 |
|
@@ -19,6 +20,9 @@ with gr.Blocks(
|
|
19 |
with gr.Tab('Pitch Leaderboard'):
|
20 |
pitch_leaderboard_app = create_pitch_leaderboard()
|
21 |
|
|
|
|
|
|
|
22 |
demo.launch(
|
23 |
share=True,
|
24 |
debug=True
|
|
|
2 |
|
3 |
from pitcher_dashboard import create_pitcher_dashboard
|
4 |
from pitch_leaderboard import create_pitch_leaderboard
|
5 |
+
from daily_pitcher_leaderboard import create_daily_pitcher_leaderboard
|
6 |
from css import css
|
7 |
|
8 |
|
|
|
20 |
with gr.Tab('Pitch Leaderboard'):
|
21 |
pitch_leaderboard_app = create_pitch_leaderboard()
|
22 |
|
23 |
+
with gr.Tab('Daily Leaderboard'):
|
24 |
+
daily_pitcher_leaderboard_app = create_daily_pitcher_leaderboard()
|
25 |
+
|
26 |
demo.launch(
|
27 |
share=True,
|
28 |
debug=True
|
daily_pitcher_leaderboard.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
10 |
+
.rename({
|
11 |
+
'name': 'Name',
|
12 |
+
'release_speed': 'Velocity',
|
13 |
+
})
|
14 |
+
)
|
15 |
+
|
16 |
+
|
17 |
+
def get_pitcher_leaderboards(datetime):
|
18 |
+
_df = df.filter(pl.col('game_date') == datetime)
|
19 |
+
whiffs = (
|
20 |
+
_df
|
21 |
+
.group_by(['pitcher'])
|
22 |
+
.agg(
|
23 |
+
pl.col('whiff').sum().alias('Whiffs'),
|
24 |
+
pl.col('Name').first()
|
25 |
+
)
|
26 |
+
.select('Name', 'Whiffs')
|
27 |
+
.sort('Whiffs', descending=True)
|
28 |
+
[:10]
|
29 |
+
)
|
30 |
+
velos = (
|
31 |
+
_df
|
32 |
+
.select('Name', 'Velocity')
|
33 |
+
.drop_nulls()
|
34 |
+
.sort(['Velocity', 'Name'], descending=[True, False])
|
35 |
+
[:10]
|
36 |
+
)
|
37 |
+
|
38 |
+
return f'<center><h1>Daily Leaderboard<h1><h2>{datetime.strftime("%B %d, %Y")}</h2></center>', whiffs, velos
|
39 |
+
|
40 |
+
def create_daily_pitcher_leaderboard():
|
41 |
+
with gr.Blocks(
|
42 |
+
css=css
|
43 |
+
) as demo:
|
44 |
+
date_picker = gr.DateTime(value=df['game_date'].max().strftime('%Y-%m-%d'), include_time=False, type='datetime', label='Date')
|
45 |
+
search_btn = gr.Button('Search')
|
46 |
+
|
47 |
+
header = gr.HTML('<center><h1>Daily Leaderboard<h2><br></center>')
|
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 |
+
search_btn.click(get_pitcher_leaderboards, date_picker, [header, whiffs, velos])
|
53 |
+
|
54 |
+
return demo
|
55 |
+
|
56 |
+
demo = create_daily_pitcher_leaderboard()
|
57 |
+
|
58 |
+
if __name__ == '__main__':
|
59 |
+
# demo = create_daily_pitcher_leaderboard()
|
60 |
+
demo.launch()
|
data.py
CHANGED
@@ -42,7 +42,7 @@ def identify_bb_type(hit_type):
|
|
42 |
return 'line_drive'
|
43 |
elif hit_type in list(range(28, 31)) + list(range(55, 58)) + list(range(107, 110)) + list(range(247, 251)):
|
44 |
return 'fly_ball'
|
45 |
-
elif hit_type in list(range(49, 55)) + list(range(
|
46 |
return 'pop_up'
|
47 |
elif hit_type in [31, 32]:
|
48 |
return None
|
@@ -101,7 +101,9 @@ pitch_df = (
|
|
101 |
)
|
102 |
.with_columns(
|
103 |
pl.col('jp_pitch_name').map_elements(lambda pitch_name: jp_pitch_to_en_pitch[pitch_name], return_dtype=str).alias('pitch_name'),
|
|
|
104 |
pl.col('jp_pitch_name').map_elements(lambda pitch_name: jp_pitch_to_pitch_code[pitch_name], return_dtype=str).alias('pitch_type'),
|
|
|
105 |
pl.col('description').str.split(' ').list.first().map_elements(translate_pitch_outcome, return_dtype=str),
|
106 |
pl.when(
|
107 |
pl.col('release_speed') != '-'
|
|
|
42 |
return 'line_drive'
|
43 |
elif hit_type in list(range(28, 31)) + list(range(55, 58)) + list(range(107, 110)) + list(range(247, 251)):
|
44 |
return 'fly_ball'
|
45 |
+
elif hit_type in list(range(49, 55)) + list(range(101, 107)) + list(range(242, 248)):
|
46 |
return 'pop_up'
|
47 |
elif hit_type in [31, 32]:
|
48 |
return None
|
|
|
101 |
)
|
102 |
.with_columns(
|
103 |
pl.col('jp_pitch_name').map_elements(lambda pitch_name: jp_pitch_to_en_pitch[pitch_name], return_dtype=str).alias('pitch_name'),
|
104 |
+
# pl.col('jp_pitch_name').replace_strict(jp_pitch_to_en_pitch).alias('pitch_name'),
|
105 |
pl.col('jp_pitch_name').map_elements(lambda pitch_name: jp_pitch_to_pitch_code[pitch_name], return_dtype=str).alias('pitch_type'),
|
106 |
+
# pl.col('jp_pitch_name').map_elements(jp_pitch_to_pitch_code).alias('pitch_type'),
|
107 |
pl.col('description').str.split(' ').list.first().map_elements(translate_pitch_outcome, return_dtype=str),
|
108 |
pl.when(
|
109 |
pl.col('release_speed') != '-'
|