F1_API / main.py
tracinginsights's picture
Update main.py
a2d3266
raw
history blame
1.05 kB
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
import fastf1
import pandas as pd
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World!"}
@app.get("/{year}/{race}/{session}")
async def get_data(year: int, race: int, session: str):
f1session = fastf1.get_session(year, race, session)
f1session.load(telemetry=False, weather=False, messages=False)
# Load all laps with telemetry
laps = f1session.laps
laps['Sector1Time'] = laps['Sector1Time'].dt.total_seconds()
laps['Sector2Time'] = laps['Sector2Time'].dt.total_seconds()
laps['Sector3Time'] = laps['Sector3Time'].dt.total_seconds()
laps['LapTime_in_seconds'] = laps['LapTime'].dt.total_seconds()
laps['laptime_sum_sectortimes'] = laps.Sector1Time + \
laps.Sector2Time + laps.Sector3Time
# convert laps to csv
laps.to_csv('laps.csv', index=False)
# return laps
# return FileResponse(path='laps.csv', filename='laps.csv')
return HTMLResponse(content=laps.to_html(), status_code=200)