Update app.py
Browse files
app.py
CHANGED
|
@@ -439,48 +439,90 @@ def server(input, output, session):
|
|
| 439 |
@output
|
| 440 |
@render.data_frame
|
| 441 |
def season_stats():
|
| 442 |
-
season = int(str(input.date_input())[:4])
|
| 443 |
-
url_season = url = f"""
|
| 444 |
-
https://www.fangraphs.com/api/leaders/major-league/data?age=&pos=all&stats=pit&lg=all&season={season}&season1={season}
|
| 445 |
-
&startdate={season}-01-01&enddate={season}-12-01&ind=0&qual=0&type=8&month=33&pageitems=500000
|
| 446 |
-
"""
|
| 447 |
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 455 |
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
if data_day['dateRange'][:10] == str(input.date_input()):
|
| 464 |
-
df_day = pl.DataFrame(data=data_day['data'], infer_schema_length=1000)
|
| 465 |
-
df_day = df_day.with_columns(pl.lit('Today').alias('Time'))
|
| 466 |
-
|
| 467 |
-
df_all = pl.DataFrame(pd.concat([df_day.to_pandas(),df_season.to_pandas()]))
|
| 468 |
-
|
| 469 |
-
df_player = df_all.filter(pl.col('xMLBAMID')==int(input.pitcher_id()))
|
| 470 |
-
|
| 471 |
else:
|
| 472 |
|
| 473 |
-
|
| 474 |
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
|
| 485 |
@output
|
| 486 |
@render.plot
|
|
|
|
| 439 |
@output
|
| 440 |
@render.data_frame
|
| 441 |
def season_stats():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
|
| 443 |
+
if int(input.sport_id()) == 1:
|
| 444 |
+
season = int(str(input.date_input())[:4])
|
| 445 |
+
url_season = url = f"""
|
| 446 |
+
https://www.fangraphs.com/api/leaders/major-league/data?age=&pos=all&stats=pit&lg=all&season={season}&season1={season}
|
| 447 |
+
&startdate={season}-01-01&enddate={season}-12-01&ind=0&qual=0&type=8&month=33&pageitems=500000
|
| 448 |
+
"""
|
| 449 |
+
|
| 450 |
+
url_day = f"""
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
https://www.fangraphs.com/api/leaders/major-league/data?age=&pos=all&stats=pit&lg=all&qual=y&season={season}&season1={season}
|
| 454 |
+
&startdate={season}-03-01&enddate={season}-11-01
|
| 455 |
+
&month=34&hand=&team=0&pageitems=500000&ind=0&rost=0&players=&type=8
|
| 456 |
+
&sortstat=WAR
|
| 457 |
+
|
| 458 |
+
"""
|
| 459 |
+
data_season = requests.get(url_season).json()
|
| 460 |
+
df_season = pl.DataFrame(data=data_season['data'], infer_schema_length=1000)
|
| 461 |
+
df_season = df_season.with_columns(pl.lit('Season').alias('Time'))
|
| 462 |
+
|
| 463 |
+
data_day = requests.get(url_day).json()
|
| 464 |
+
|
| 465 |
+
if data_day['dateRange'][:10] == str(input.date_input()):
|
| 466 |
+
df_day = pl.DataFrame(data=data_day['data'], infer_schema_length=1000)
|
| 467 |
+
df_day = df_day.with_columns(pl.lit('Today').alias('Time'))
|
| 468 |
+
|
| 469 |
+
df_all = pl.DataFrame(pd.concat([df_day.to_pandas(),df_season.to_pandas()]))
|
| 470 |
+
|
| 471 |
+
df_player = df_all.filter(pl.col('xMLBAMID')==int(input.pitcher_id()))
|
| 472 |
+
|
| 473 |
+
else:
|
| 474 |
+
|
| 475 |
+
df_player = df_season.filter(pl.col('xMLBAMID')==int(input.pitcher_id()))
|
| 476 |
|
| 477 |
+
|
| 478 |
+
return render.DataGrid(
|
| 479 |
+
df_player.select(['Time','IP','TBF','R','ER','SO','BB','ERA','FIP','WHIP']).to_pandas().round(2),
|
| 480 |
+
row_selection_mode='multiple',
|
| 481 |
+
height='700px',
|
| 482 |
+
width='fit-content',
|
| 483 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
else:
|
| 485 |
|
| 486 |
+
d = scrape.get_data([int(input.game_id())])
|
| 487 |
|
| 488 |
+
player_id = f'ID{int(input.pitcher_id())}'
|
| 489 |
+
|
| 490 |
+
home_players = d[0]['liveData']['boxscore']['teams']['home']['players']
|
| 491 |
+
away_players = d[0]['liveData']['boxscore']['teams']['away']['players']
|
| 492 |
+
|
| 493 |
+
if player_id in home_players:
|
| 494 |
+
batters_faced = home_players[player_id]['stats']['pitching']['battersFaced']
|
| 495 |
+
team_side = 'home'
|
| 496 |
+
elif player_id in away_players:
|
| 497 |
+
batters_faced = away_players[player_id]['stats']['pitching']['battersFaced']
|
| 498 |
+
team_side = 'away'
|
| 499 |
+
else:
|
| 500 |
+
batters_faced = None
|
| 501 |
+
team_side = None
|
| 502 |
+
|
| 503 |
+
|
| 504 |
+
pitches = d[0]['liveData']['boxscore']['teams'][team_side]['players'][player_id]['stats']['pitching']['pitchesThrown']
|
| 505 |
+
battersFaced = d[0]['liveData']['boxscore']['teams'][team_side]['players'][player_id]['stats']['pitching']['battersFaced']
|
| 506 |
+
runs = d[0]['liveData']['boxscore']['teams'][team_side]['players'][player_id]['stats']['pitching']['runs']
|
| 507 |
+
earnedRuns = d[0]['liveData']['boxscore']['teams'][team_side]['players'][player_id]['stats']['pitching']['earnedRuns']
|
| 508 |
+
strikeOuts = d[0]['liveData']['boxscore']['teams'][team_side]['players'][player_id]['stats']['pitching']['strikeOuts']
|
| 509 |
+
baseOnBalls = d[0]['liveData']['boxscore']['teams'][team_side]['players'][player_id]['stats']['pitching']['baseOnBalls']
|
| 510 |
+
|
| 511 |
+
df_player = pl.DataFrame({
|
| 512 |
+
'pitches': int(pitches),
|
| 513 |
+
'battersFaced': int(battersFaced),
|
| 514 |
+
'runs': int(runs),
|
| 515 |
+
'earnedRuns': int(earnedRuns),
|
| 516 |
+
'strikeOuts': int(strikeOuts),
|
| 517 |
+
'baseOnBalls': int(baseOnBalls)
|
| 518 |
+
})
|
| 519 |
+
|
| 520 |
+
return render.DataGrid(
|
| 521 |
+
df_player.to_pandas().round(2),
|
| 522 |
+
row_selection_mode='multiple',
|
| 523 |
+
height='700px',
|
| 524 |
+
width='fit-content',
|
| 525 |
+
)
|
| 526 |
|
| 527 |
@output
|
| 528 |
@render.plot
|