File size: 4,661 Bytes
c00ad6e
75e5ca4
c00ad6e
 
 
 
 
75e5ca4
 
c00ad6e
 
7dac96a
 
c00ad6e
 
 
 
 
 
 
75e5ca4
 
 
 
 
 
 
 
c00ad6e
 
 
 
 
75e5ca4
c00ad6e
75e5ca4
c00ad6e
 
75e5ca4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00ad6e
 
75e5ca4
c00ad6e
 
 
75e5ca4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00ad6e
 
 
 
 
75e5ca4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1f0527
 
75e5ca4
 
 
 
 
 
 
 
c00ad6e
75e5ca4
c00ad6e
75e5ca4
 
c00ad6e
75e5ca4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00ad6e
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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()