Spaces:
Runtime error
Runtime error
Commit
·
7eaac64
1
Parent(s):
f5980e9
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,89 +1,20 @@
|
|
| 1 |
import fastf1
|
| 2 |
import pandas as pd
|
| 3 |
from fastapi import FastAPI
|
| 4 |
-
from fastapi.responses import FileResponse, HTMLResponse
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
|
| 7 |
-
app = FastAPI()
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# class Laps(BaseModel):
|
| 11 |
-
# Time: pd.Timedelta
|
| 12 |
-
# Driver: str
|
| 13 |
-
# DriverNumber: str
|
| 14 |
-
# LapTime: pd.Timedelta
|
| 15 |
-
# LapNumber: int
|
| 16 |
-
# Stint: int
|
| 17 |
-
# PitOutTime: pd.Timedelta
|
| 18 |
-
# PitInTime: pd.Timedelta
|
| 19 |
-
# Sector1Time: pd.Timedelta
|
| 20 |
-
# Sector2Time: pd.Timedelta
|
| 21 |
-
# Sector3Time: pd.Timedelta
|
| 22 |
-
# Sector1SessionTime: pd.Timedelta
|
| 23 |
-
# Sector2SessionTime: pd.Timedelta
|
| 24 |
-
# Sector3SessionTime: pd.Timedelta
|
| 25 |
-
# SpeedI1: float
|
| 26 |
-
# SpeedI2: float
|
| 27 |
-
# SpeedFL: float
|
| 28 |
-
# SpeedST: float
|
| 29 |
-
# IsPersonalBest: bool
|
| 30 |
-
# Compound: str
|
| 31 |
-
# TyreLife: float
|
| 32 |
-
# FreshTyre: bool
|
| 33 |
-
# Team: str
|
| 34 |
-
# LapStartTime: pd.Timedelta
|
| 35 |
-
# LapStartDate: pd.Timestamp
|
| 36 |
-
# TrackStatus: str
|
| 37 |
-
# IsAccurate: bool
|
| 38 |
-
# # https://stackoverflow.com/questions/65412984/using-pandas-data-frame-as-a-type-in-pydantic
|
| 39 |
|
| 40 |
-
# class Config:
|
| 41 |
-
# arbitrary_types_allowed = True
|
| 42 |
|
| 43 |
-
|
| 44 |
-
class Laps(BaseModel):
|
| 45 |
-
Time: pd.core.frame.DataFrame
|
| 46 |
-
Driver: pd.core.frame.DataFrame
|
| 47 |
-
DriverNumber: pd.core.frame.DataFrame
|
| 48 |
-
LapTime: pd.core.frame.DataFrame
|
| 49 |
-
LapNumber: pd.core.frame.DataFrame
|
| 50 |
-
Stint: pd.core.frame.DataFrame
|
| 51 |
-
PitOutTime: pd.core.frame.DataFrame
|
| 52 |
-
PitInTime: pd.core.frame.DataFrame
|
| 53 |
-
Sector1Time: pd.core.frame.DataFrame
|
| 54 |
-
Sector2Time: pd.core.frame.DataFrame
|
| 55 |
-
Sector3Time: pd.core.frame.DataFrame
|
| 56 |
-
Sector1SessionTime: pd.core.frame.DataFrame
|
| 57 |
-
Sector2SessionTime: pd.core.frame.DataFrame
|
| 58 |
-
Sector3SessionTime: pd.core.frame.DataFrame
|
| 59 |
-
SpeedI1: pd.core.frame.DataFrame
|
| 60 |
-
SpeedI2: pd.core.frame.DataFrame
|
| 61 |
-
SpeedFL: pd.core.frame.DataFrame
|
| 62 |
-
SpeedST: pd.core.frame.DataFrame
|
| 63 |
-
IsPersonalBest: pd.core.frame.DataFrame
|
| 64 |
-
Compound: pd.core.frame.DataFrame
|
| 65 |
-
TyreLife: pd.core.frame.DataFrame
|
| 66 |
-
FreshTyre: pd.core.frame.DataFrame
|
| 67 |
-
Team: pd.core.frame.DataFrame
|
| 68 |
-
LapStartTime: pd.core.frame.DataFrame
|
| 69 |
-
LapStartDate: pd.core.frame.DataFrame
|
| 70 |
-
TrackStatus: pd.core.frame.DataFrame
|
| 71 |
-
IsAccurate: pd.core.frame.DataFrame
|
| 72 |
-
# https://stackoverflow.com/questions/65412984/using-pandas-data-frame-as-a-type-in-pydantic
|
| 73 |
-
|
| 74 |
-
class Config:
|
| 75 |
-
arbitrary_types_allowed = True
|
| 76 |
|
| 77 |
|
| 78 |
@app.get("/")
|
| 79 |
async def root():
|
| 80 |
return {"message": "Hello World"}
|
| 81 |
|
| 82 |
-
|
| 83 |
-
# create a function that returns laps dataframe given year, race ,session from fastf1
|
| 84 |
-
|
| 85 |
@app.get("/{year}/{race}/{session}", response_model=None)
|
| 86 |
async def get_data(year: int, race: int, session: str) -> any:
|
|
|
|
|
|
|
| 87 |
|
| 88 |
f1session = fastf1.get_session(year, race, session)
|
| 89 |
f1session.load(telemetry=False, weather=False, messages=False)
|
|
@@ -93,16 +24,6 @@ async def get_data(year: int, race: int, session: str) -> any:
|
|
| 93 |
laps['Sector2Time'] = laps['Sector2Time'].dt.total_seconds()
|
| 94 |
laps['Sector3Time'] = laps['Sector3Time'].dt.total_seconds()
|
| 95 |
laps['LapTime_in_seconds'] = laps['LapTime'].dt.total_seconds()
|
| 96 |
-
laps['laptime_sum_sectortimes'] = laps.Sector1Time + laps.Sector2Time + laps.Sector3Time
|
| 97 |
-
|
| 98 |
-
# # convert laps to csv
|
| 99 |
-
# laps.to_csv('laps.csv', index=False)
|
| 100 |
-
# return laps
|
| 101 |
-
|
| 102 |
-
# in laps replace NaN with ""
|
| 103 |
-
laps = laps.fillna(" ")
|
| 104 |
|
| 105 |
-
return {"laps": laps.
|
| 106 |
-
|
| 107 |
-
# return FileResponse(path='laps.csv', filename='laps.csv')
|
| 108 |
-
# return HTMLResponse(content=laps.to_html(), status_code=200)
|
|
|
|
| 1 |
import fastf1
|
| 2 |
import pandas as pd
|
| 3 |
from fastapi import FastAPI
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
@app.get("/")
|
| 11 |
async def root():
|
| 12 |
return {"message": "Hello World"}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.get("/{year}/{race}/{session}", response_model=None)
|
| 15 |
async def get_data(year: int, race: int, session: str) -> any:
|
| 16 |
+
|
| 17 |
+
fastf1.Cache.enable_cache('/Cache')
|
| 18 |
|
| 19 |
f1session = fastf1.get_session(year, race, session)
|
| 20 |
f1session.load(telemetry=False, weather=False, messages=False)
|
|
|
|
| 24 |
laps['Sector2Time'] = laps['Sector2Time'].dt.total_seconds()
|
| 25 |
laps['Sector3Time'] = laps['Sector3Time'].dt.total_seconds()
|
| 26 |
laps['LapTime_in_seconds'] = laps['LapTime'].dt.total_seconds()
|
| 27 |
+
laps['laptime_sum_sectortimes'] = laps.Sector1Time + laps.Sector2Time + laps.Sector3Time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
return {"laps": laps.to_json(orient='records')}
|
|
|
|
|
|
|
|
|