File size: 1,866 Bytes
3d0c0d1
 
 
 
eaf2663
 
3d0c0d1
 
 
eaf2663
3d0c0d1
7dac96a
 
 
 
 
248dee2
7dac96a
3d0c0d1
 
7dac96a
 
 
 
eaf2663
7dac96a
 
 
 
 
248dee2
7dac96a
 
 
 
 
 
 
 
 
 
 
 
 
 
3d0c0d1
 
7dac96a
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr
import polars as pl


from data import df, game_df, compute_pitch_stats
from seasons import SEASONS, LATEST_SEASON
from gradio_function import *
from css import css

# SEASONS = [int(season) for season in SEASONS]

def filter_pitch_leaderboard(season, min_pitches):
  return (
    compute_pitch_stats(df.filter(pl.col('game_year') == season))
    .filter(pl.col('Count') >= min_pitches)
    .sort('CSW%', descending=True)
    .rename({'name': 'Player', 'pitch_name': 'Pitch'})
  )

def create_pitch_leaderboard():
    with gr.Blocks(
        css=css
    ) as demo:
        init_min_pitches = 100
        init_season = LATEST_SEASON
        init_pitch_stats = filter_pitch_leaderboard(init_season, init_min_pitches)
        init_pitch_stats.write_csv('pitch_leaderboard.csv')
        pitch_leaderboard_df = gr.State(init_pitch_stats)

        with gr.Row():
            season = gr.Dropdown(choices=SEASONS, value=init_season, label='Season')
            min_pitches = gr.Number(init_min_pitches, precision=0, label='Min. Pitches')
        pitch_leaderboard_download_file = gr.DownloadButton(value='pitch_leaderboard.csv', label='Download leaderboard')
        pitch_leaderboard = gr.Dataframe(value=pitch_leaderboard_df.value)

        for component in (season, min_pitches):
            component.change(filter_pitch_leaderboard, inputs=[season, min_pitches], outputs=pitch_leaderboard_df)
        (
            pitch_leaderboard_df
            .change(create_set_download_file_fn('files/pitch_leaderboard.csv'), inputs=pitch_leaderboard_df, outputs=pitch_leaderboard_download_file)
            .then(lambda df: df, inputs=pitch_leaderboard_df, outputs=pitch_leaderboard)
        )
    return demo

demo = create_pitch_leaderboard()

if __name__ == '__main__':
    create_pitch_leaderboard().launch(
        share=True,
        debug=True
    )