-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
55 lines (44 loc) · 1.6 KB
/
Copy pathapp.py
File metadata and controls
55 lines (44 loc) · 1.6 KB
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
# app.py
from shiny import App, render, ui
import pandas as pd
import traceback
from prediction import load_data, train_model, project_next_season
INIT_ERROR = "" # will show in UI if init fails
try:
batting = load_data()
model = train_model(batting)
player_choices = sorted(pd.Series(batting["Name"]).dropna().unique().tolist())
except Exception:
INIT_ERROR = traceback.format_exc()
batting = pd.DataFrame()
model = None
player_choices = []
app_ui = ui.page_fluid(
ui.h2("StatForecast"),
ui.markdown("Select a player to see **projected next-season stats**."),
# show init errors (missing columns, missing sklearn, etc.)
ui.output_text_verbatim("init_error"),
ui.input_selectize("player", "Choose a player:", choices=player_choices, multiple=False),
ui.output_table("projection")
)
def server(input, output, session):
@output
@render.text
def init_error():
return INIT_ERROR # empty string if ok
@output
@render.table
def projection():
if INIT_ERROR:
return pd.DataFrame({"error": ["App failed during startup. See error above."]})
if not input.player():
return pd.DataFrame({"info": ["Pick a player"]})
try:
preds = project_next_season(input.player(), batting, model)
if preds is None:
return pd.DataFrame({"error": [f"No rows for '{input.player()}'"]})
return pd.DataFrame(preds, index=[0])
except Exception:
tb = traceback.format_exc()
return pd.DataFrame({"exception": [tb]})
app = App(app_ui, server)