import gradio as gr
from data import df, game_df
from gradio_function import *
from css import css
df = (
df
.join(game_df, on='game_pk').with_columns(pl.col('game_date').str.to_datetime())
.rename({
'name': 'Name',
'release_speed': 'Velocity',
})
)
def get_pitcher_leaderboards(datetime):
_df = df.filter(pl.col('game_date') == datetime)
whiffs = (
_df
.group_by(['pitcher'])
.agg(
pl.col('whiff').sum().alias('Whiffs'),
pl.col('Name').first()
)
.select('Name', 'Whiffs')
.sort('Whiffs', descending=True)
[:10]
)
velos = (
_df
.select('Name', 'Velocity')
.drop_nulls()
.sort(['Velocity', 'Name'], descending=[True, False])
[:10]
)
return f'
Daily Leaderboard{datetime.strftime("%B %d, %Y")}
', whiffs, velos
def create_daily_pitcher_leaderboard():
with gr.Blocks(
css=css
) as demo:
date_picker = gr.DateTime(value=df['game_date'].max().strftime('%Y-%m-%d'), include_time=False, type='datetime', label='Date')
search_btn = gr.Button('Search')
header = gr.HTML('Daily Leaderboard
')
with gr.Row():
whiffs = gr.Dataframe(pl.DataFrame({'Name': [], 'Whiffs': []}), label='Whiffs')
velos = gr.Dataframe(pl.DataFrame({'Name': [], 'Velocity': []}), label='Velocity')
search_btn.click(get_pitcher_leaderboards, date_picker, [header, whiffs, velos])
return demo
demo = create_daily_pitcher_leaderboard()
if __name__ == '__main__':
# demo = create_daily_pitcher_leaderboard()
demo.launch()