File size: 2,586 Bytes
a8b6a3f
b421bc5
 
a8b6a3f
b421bc5
 
 
a8b6a3f
bb5e4ba
 
 
 
 
 
 
 
 
 
 
a8b6a3f
 
 
b421bc5
 
 
 
 
 
 
 
 
 
 
 
a8b6a3f
b421bc5
 
 
 
 
a8b6a3f
 
 
 
bb5e4ba
a8b6a3f
b421bc5
 
 
 
 
 
 
 
 
 
bb5e4ba
a8b6a3f
b421bc5
 
 
 
 
 
 
a8b6a3f
 
 
 
b421bc5
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
import gradio as gr
import matplotlib.pyplot as plt
import polars as pl

from data import SEASONS, data_df

from plotting import create_pitcher_overview_card

notes = '''**Limitations**
- Foreign players names are in Hebpurn romanization. Contact me if you need a card for a foreign player.

**To-do**
- Fix names of foreign playeres
- Add teams insignias
- Measure percentiles per pitcher handedness
- Allow for arbitrary date ranges
- Improve readability of pitch velocities
'''

def dummy(*inputs):
  return inputs

def gr_create_pitcher_overview_card(name, season):
  # pit_id = name.split(' | ')[-1]
  pit_id = data_df.filter(pl.col('pitcher_name') == name)['pitId'].unique()
  if len(pit_id) == 0:
    raise gr.Error(f"No data found for {name}. If the name looks strangely spelled or formatted there's a possibility that's what causing the error.")
  elif len(pit_id) > 1:
    raise gr.Error(f'Multiple IDs for {name}')
  else:
    pit_id = pit_id.item()
  create_pitcher_overview_card(pit_id, season=season, dpi=300)
  plt.savefig('tmp.png', bbox_inches='tight')
  return 'tmp.png'

# def adjust_season_end_based_on_season_start(season_start, season_end):
  # return max(season_start, season_end)
# 
# def adjust_season_start_based_on_season_end(season_end, season_start):
  # return min(season_start, season_end)
    
  
def create_pitcher_overview(data_df):
  with gr.Blocks() as app:
    gr.Markdown('# Pitcher Overview')

    with gr.Row():
      with gr.Column():
        # names = [f'{pit_name} | {pit_id}' for pit_name, pit_id in data_df[['pitcher_name', 'pitId']].unique().sort('pitId').iter_rows()]
        names = data_df['pitcher_name'].unique().sort().to_list()
        name = gr.Dropdown(names, label='Name')
        season = gr.Dropdown(SEASONS, label='Season')
        # season_start = gr.Dropdown(SEASONS, label='Season start')
        # season_end = gr.Dropdown(SEASONS, label='Season end')
        # game_type = gr.Dropdown(['Spring Training', 'Regular Season', 'Postseason'], label='Game Type'])
        view = gr.Button('View')
        gr.Markdown(notes)

      with gr.Column():
        overview_card = gr.Image(label='Overview')

    # season_start.input(adjust_season_end_based_on_season_start, inputs=[season_start, season_end], outputs=season_end)
    # season_end.input(adjust_season_start_based_on_season_end, inputs=[season_end, season_start], outputs=season_start)
    view.click(gr_create_pitcher_overview_card, inputs=[name, season], outputs=overview_card)
    

  return app

if __name__ == '__main__':
  create_pitcher_overview(data_df).launch()