Spaces:
Build error
Build error
import gradio as gr | |
from gradio_calendar import Calendar | |
from data import df, game_df | |
from gradio_function import * | |
from css import css | |
import datetime | |
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(date, top_players, strict, ignore_zero_whiffs, show_rank, debug): | |
_df = df.filter(pl.col('game_date') == date) | |
other_cols = ['Name'] | |
if debug: | |
other_cols = ['game_date'] + other_cols | |
whiffs = ( | |
_df | |
.group_by(['pitcher']) | |
.agg( | |
pl.col('whiff').sum().alias('Whiffs'), | |
*[pl.col(col).first() for col in other_cols] | |
) | |
.select(*other_cols, 'Whiffs') | |
.sort('Whiffs', descending=True) | |
) | |
if ignore_zero_whiffs: | |
whiffs = whiffs.filter(pl.col('Whiffs') > 0) | |
if len(whiffs) >top_players: | |
whiffs = ( | |
whiffs | |
.filter(pl.col('Whiffs') >= whiffs['Whiffs'][top_players]) | |
) | |
if strict: | |
whiffs = whiffs[:top_players] | |
if show_rank: | |
whiffs = ( | |
whiffs | |
.with_row_index(offset=1) | |
.rename({'index': 'Rank'}) | |
) | |
velos = ( | |
_df | |
.select(*other_cols, 'Velocity') | |
.drop_nulls() | |
.sort(['Velocity', 'Name'], descending=[True, False]) | |
) | |
if len(velos) > top_players: | |
velos = velos.filter(pl.col('Velocity') >= velos['Velocity'][top_players]) | |
if strict: | |
velos = velos[:top_players] | |
if show_rank: | |
velos = ( | |
velos | |
.with_row_index(offset=1) | |
.rename({'index': 'Rank'}) | |
) | |
return ( | |
f'<center><h1>Daily Leaderboard<h1><h2>{date.strftime("%B %d, %Y")}</h2><h3>{date.strftime("%A")}</h3></center>', | |
whiffs, | |
velos, | |
gr.update(interactive=True), | |
gr.update(interactive=True) | |
) | |
def go_back_day(date): | |
return date - datetime.timedelta(days=1) | |
def go_forward_day(date): | |
return date + datetime.timedelta(days=1) | |
def create_daily_pitcher_leaderboard(): | |
with gr.Blocks( | |
css=css | |
) as demo: | |
with gr.Row(): | |
# date_picker = gr.DateTime( | |
# value=df['game_date'].max().strftime('%Y-%m-%d'), | |
# include_time=False, | |
# type='datetime', | |
# label='Date', | |
# scale=4 | |
# ) | |
date_picker = Calendar( | |
value=df['game_date'].max().strftime('%Y-%m-%d'), | |
type='datetime', | |
label='Date', | |
scale=2, | |
min_width=50 | |
) | |
top_players = gr.Number(10, label='# Top players', scale=1, min_width=100) | |
strict = gr.Checkbox(False, label='Strict', info='Ignore ties and restrict to # top players', scale=2, min_width=100) | |
ignore_zero_whiffs = gr.Checkbox(False, label='Ignore zero whiffs', info='Ignore zero whiff players if in top ranked', scale=2, min_width=100) | |
show_rank = gr.Checkbox(False, label='Show rank', scale=1, min_width=100) | |
debug = gr.Checkbox(False, label='Debug', info='Show dates', scale=1, min_width=100) | |
search_btn = gr.Button('Search', scale=1, min_width=100) | |
with gr.Row(): | |
prev_btn = gr.Button('Previous', interactive=False) | |
next_btn = gr.Button('Next', interactive=False) | |
header = gr.HTML('<center><h1>Daily Leaderboard<h1><h2 style="display: none;"></h2><h3 style="display: none;"></h3></center>') | |
with gr.Row(): | |
whiffs = gr.Dataframe(pl.DataFrame({'Name': [], 'Whiffs': []}), label='Whiffs', interactive=False) | |
velos = gr.Dataframe(pl.DataFrame({'Name': [], 'Velocity': []}), label='Velocity', interactive=False) | |
search_kwargs = dict( | |
fn=get_pitcher_leaderboards, | |
inputs=[date_picker, top_players, strict, ignore_zero_whiffs, show_rank, debug], | |
outputs=[header, whiffs, velos, prev_btn, next_btn] | |
) | |
search_btn.click(**search_kwargs) | |
( | |
prev_btn | |
.click(go_back_day, date_picker, date_picker) | |
.then(**search_kwargs) | |
) | |
( | |
next_btn | |
.click(go_forward_day, date_picker, date_picker) | |
.then(**search_kwargs) | |
) | |
return demo | |
demo = create_daily_pitcher_leaderboard() | |
if __name__ == '__main__': | |
# demo = create_daily_pitcher_leaderboard() | |
demo.launch() | |