Spaces:
Runtime error
Runtime error
File size: 2,736 Bytes
b8df987 7db7475 b8df987 bcf020e bb00581 c46cec1 5a53c9e 84b4aa4 c46cec1 fb11f75 17976c1 fb11f75 b8df987 485d878 bdf1ad8 b8df987 fb11f75 84b4aa4 33f3b6b 84b4aa4 bb00581 84b4aa4 bb00581 c46cec1 bb00581 bcf020e e5d2e0d 0054bfa c46cec1 0054bfa bb00581 84b4aa4 c46cec1 bb00581 c46cec1 bb00581 84b4aa4 c46cec1 bcf020e b3344b3 87aae5e 84b4aa4 87aae5e bb00581 c46cec1 bb00581 c46cec1 87aae5e 0054bfa 87aae5e c46cec1 bb00581 bcf020e e1e02bd ed4ba7d 84b4aa4 ed4ba7d 0d0ff81 |
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import os
#os.environ['MPLCONFIGDIR'] = os.getcwd() + "/configs"
from shiny import App, ui, render, reactive
from shinywidgets import output_widget, render_widget
import fastf1 as ff1
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib import cm
import numpy as np
from pathlib import Path
import shiny.experimental as x
import shinyswatch
# Create a Path object for the folder
cache_path = os.getcwd() + "/cache"
ff1.Cache.enable_cache(cache_path)
ff1.Cache.offline_mode(enabled=True)
print(f"Cache path: {cache_path}")
app_ui = x.ui.page_fillable(
shinyswatch.theme.minty(),
x.ui.layout_sidebar(
x.ui.sidebar(
ui.input_select(
"track", label="Select track:",
choices=["Austria", "Hungary", "Spain", "Bahrain"],
selected = "Austria"),
),
ui.h3("Gear usage in fastest lap"),
x.ui.output_plot("gear", fill = True),
ui.output_text("fastest_driver"),
fill=True,
fillable=True,
),
)
def server(input, output, session):
@reactive.Calc
def get_data():
f1_session = ff1.get_session(2023, input.track(), 'R')
f1_session.load()
lap = f1_session.laps.pick_fastest()
tel = lap.get_telemetry()
driver = lap['Driver']
#converting data to numpy data tables
x = np.array(tel['X'].values)
y = np.array(tel['Y'].values)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
gear = tel['nGear'].to_numpy().astype(float)
return segments, gear, driver
@output
@render.plot
def gear():
segments, gear, driver = get_data()
cmap = cm.get_cmap('Paired')
lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)
lc_comp.set_array(gear)
lc_comp.set_linewidth(4)
plt.gca().add_collection(lc_comp)
plt.axis('equal')
plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)
##title = plt.suptitle(
## f"Fastest Lap Gear Shift Visualization\n"
## f"{lap['Driver']} - {f1_session.event['EventName']} {f1_session.event.year}"
## )
cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
cbar.set_ticks(np.arange(1.5, 9.5))
cbar.set_ticklabels(np.arange(1, 9))
plt
@output
@render.text
def fastest_driver():
segments, gear, driver = get_data()
print(driver)
return f"The driver of the fastest lap is: {driver}"
app = App(app_ui, server) |