Spaces:
Build error
Build error
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 | |
) | |