kbberendsen commited on
Commit
6706c02
1 Parent(s): e662b95

add all drivers and error message

Browse files
Files changed (2) hide show
  1. __pycache__/app.cpython-311.pyc +0 -0
  2. app.py +67 -38
__pycache__/app.cpython-311.pyc CHANGED
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
 
app.py CHANGED
@@ -16,6 +16,26 @@ ff1.Cache.enable_cache(cache_path)
16
  # Offline mode to prevent F1 API crashes on Hugging Face
17
  ff1.Cache.offline_mode(enabled=True)
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  app_ui = ui.page_fluid(
20
  shinyswatch.theme.minty(),
21
  ui.panel_title("Gear usage in fastest lap"),
@@ -23,7 +43,7 @@ app_ui = ui.page_fluid(
23
  ui.panel_sidebar(
24
  ui.input_select(
25
  "track_select", "Select track:",
26
- choices = ["Austria", "Hungary", "Spain", "Bahrain"],
27
  selected = "Austria"
28
  ),
29
  ui.input_select(
@@ -55,9 +75,9 @@ def server(input, output, session):
55
  @reactive.Effect()
56
  def _():
57
  if input.year() == "2023":
58
- driver_options = ['Fastest driver', 'RUS', 'VER', 'HAM']
59
  elif input.year() == "2022":
60
- driver_options = ['Fastest driver', 'LEC', 'NOR', 'GAS', 'VER']
61
 
62
  ui.update_select("driver_select",
63
  label="Select driver:",
@@ -67,27 +87,32 @@ def server(input, output, session):
67
 
68
  @reactive.Calc
69
  def get_data():
70
- f1_session = ff1.get_session(int(input.year()), input.track_select(), input.session_type())
71
- f1_session.load()
72
-
73
- # Check if user input == fastest driver
74
- if input.driver_select() == "Fastest driver":
75
- lap = f1_session.laps.pick_fastest()
76
- else:
77
- laps_driver = f1_session.laps.pick_driver(input.driver_select())
78
- lap = laps_driver.pick_fastest()
79
-
80
- tel = lap.get_telemetry()
81
- driver = lap['Driver']
82
 
83
- #converting data to numpy data tables
84
- x = np.array(tel['X'].values)
85
- y = np.array(tel['Y'].values)
 
 
 
 
 
 
86
 
87
- points = np.array([x, y]).T.reshape(-1, 1, 2)
88
- segments = np.concatenate([points[:-1], points[1:]], axis=1)
89
- gear = tel['nGear'].to_numpy().astype(float)
90
- return segments, gear, driver
 
 
 
 
 
 
 
 
91
 
92
  @output
93
  @render.text
@@ -99,22 +124,26 @@ def server(input, output, session):
99
  @output
100
  @render.plot
101
  def gear():
102
- segments, gear, driver = get_data()
103
-
104
- cmap = cm.get_cmap('Paired')
105
- lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)
106
- lc_comp.set_array(gear)
107
- lc_comp.set_linewidth(4)
108
-
109
- plt.gca().add_collection(lc_comp)
110
- plt.axis('equal')
111
- plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)
112
-
113
- cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
114
- cbar.set_ticks(np.arange(1.5, 9.5))
115
- cbar.set_ticklabels(np.arange(1, 9))
116
-
117
- plt
 
 
 
 
118
 
119
 
120
  app = App(app_ui, server)
 
16
  # Offline mode to prevent F1 API crashes on Hugging Face
17
  ff1.Cache.offline_mode(enabled=True)
18
 
19
+ # Define drivers
20
+ drivers_2023 = {'Fastest driver': 'Fastest driver', 'VER': 'Max Verstappen',
21
+ 'NOR': 'Lando Norris', 'GAS': 'Pierre Gasly', 'PER': 'Sergio Perez',
22
+ 'ALO': 'Fernando Alonso', 'LEC': 'Charles Leclerc', 'STR': 'Lance Stroll',
23
+ 'MAG': 'Kevin Magnussen', 'TSU': 'Yuki Tsunoda', 'ALB': 'Alexander Albon',
24
+ 'ZHO': 'Guanyu Zhou', 'HUL': 'Nico Hülkenberg', 'OCO': 'Esteban Ocon',
25
+ 'HAM': 'Lewis Hamilton', 'SAI': 'Carlos Sainz', 'RUS': 'George Russel',
26
+ 'BOT': 'Valteri Bottas', 'PIA': 'Oscar Piastri', 'VRI': 'Nyck de Vries',
27
+ 'SAR': 'Logan Sargeant', 'RIC': 'Daniel Ricciardo'}
28
+
29
+ drivers_2022 = {'Fastest driver': 'Fastest driver', 'VER': 'Max Verstappen',
30
+ 'NOR': 'Lando Norris', 'GAS': 'Pierre Gasly', 'PER': 'Sergio Perez',
31
+ 'ALO': 'Fernando Alonso', 'LEC': 'Charles Leclerc', 'STR': 'Lance Stroll',
32
+ 'MAG': 'Kevin Magnussen', 'TSU': 'Yuki Tsunoda', 'ALB': 'Alexander Albon',
33
+ 'ZHO': 'Guanyu Zhou', 'HUL': 'Nico Hülkenberg', 'OCO': 'Esteban Ocon',
34
+ 'HAM': 'Lewis Hamilton', 'SAI': 'Carlos Sainz', 'RUS': 'George Russel',
35
+ 'BOT': 'Valteri Bottas', 'VRI': 'Nyck de Vries', 'VET': 'Sebastian Vettel',
36
+ 'RIC': 'Daniel Ricciardo', 'MSC': 'Mick Schumacher',
37
+ 'LAT': 'Nicolas Latifi'}
38
+
39
  app_ui = ui.page_fluid(
40
  shinyswatch.theme.minty(),
41
  ui.panel_title("Gear usage in fastest lap"),
 
43
  ui.panel_sidebar(
44
  ui.input_select(
45
  "track_select", "Select track:",
46
+ choices = ["Austria", "Hungary", "Spain", "Bahrain", "Great-Brittain"],
47
  selected = "Austria"
48
  ),
49
  ui.input_select(
 
75
  @reactive.Effect()
76
  def _():
77
  if input.year() == "2023":
78
+ driver_options = drivers_2023
79
  elif input.year() == "2022":
80
+ driver_options = drivers_2022
81
 
82
  ui.update_select("driver_select",
83
  label="Select driver:",
 
87
 
88
  @reactive.Calc
89
  def get_data():
90
+ try:
91
+ f1_session = ff1.get_session(int(input.year()), input.track_select(), input.session_type())
92
+ f1_session.load()
 
 
 
 
 
 
 
 
 
93
 
94
+ # Check if user input == fastest driver
95
+ if input.driver_select() == "Fastest driver":
96
+ lap = f1_session.laps.pick_fastest()
97
+ else:
98
+ laps_driver = f1_session.laps.pick_driver(input.driver_select())
99
+ lap = laps_driver.pick_fastest()
100
+
101
+ tel = lap.get_telemetry()
102
+ driver = lap['Driver']
103
 
104
+ #converting data to numpy data tables
105
+ x = np.array(tel['X'].values)
106
+ y = np.array(tel['Y'].values)
107
+
108
+ points = np.array([x, y]).T.reshape(-1, 1, 2)
109
+ segments = np.concatenate([points[:-1], points[1:]], axis=1)
110
+ gear = tel['nGear'].to_numpy().astype(float)
111
+ return segments, gear, driver
112
+
113
+ except Exception:
114
+ pass
115
+
116
 
117
  @output
118
  @render.text
 
124
  @output
125
  @render.plot
126
  def gear():
127
+ try:
128
+ segments, gear, driver = get_data()
129
+
130
+ cmap = cm.get_cmap('Paired')
131
+ lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)
132
+ lc_comp.set_array(gear)
133
+ lc_comp.set_linewidth(4)
134
+
135
+ plt.gca().add_collection(lc_comp)
136
+ plt.axis('equal')
137
+ plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)
138
+
139
+ cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
140
+ cbar.set_ticks(np.arange(1.5, 9.5))
141
+ cbar.set_ticklabels(np.arange(1, 9))
142
+
143
+ plt
144
+
145
+ except Exception:
146
+ ui.notification_show("Data not available. Select another track or driver.", duration=10, type = 'error')
147
 
148
 
149
  app = App(app_ui, server)