kbberendsen commited on
Commit
ec47323
1 Parent(s): 2ef1c28

driver module only contains dicts

Browse files
Files changed (1) hide show
  1. app.py +75 -31
app.py CHANGED
@@ -1,13 +1,11 @@
1
  import os
2
- from shiny import module, App, ui, render, reactive
3
  import fastf1 as ff1
4
  import matplotlib.pyplot as plt
5
  from matplotlib.collections import LineCollection
6
  from matplotlib import cm
7
  import numpy as np
8
  import shinyswatch
9
- from modules import plot
10
- from modules import test_module
11
  from modules import drivers
12
 
13
  # Define cache folder path
@@ -20,24 +18,9 @@ ff1.Cache.enable_cache(cache_path)
20
  ff1.Cache.offline_mode(enabled=True)
21
 
22
  # Define drivers
23
- drivers_2023 = {'Fastest driver': 'Fastest driver', 'VER': 'Max Verstappen',
24
- 'NOR': 'Lando Norris', 'GAS': 'Pierre Gasly', 'PER': 'Sergio Perez',
25
- 'ALO': 'Fernando Alonso', 'LEC': 'Charles Leclerc', 'STR': 'Lance Stroll',
26
- 'MAG': 'Kevin Magnussen', 'TSU': 'Yuki Tsunoda', 'ALB': 'Alexander Albon',
27
- 'ZHO': 'Guanyu Zhou', 'HUL': 'Nico Hülkenberg', 'OCO': 'Esteban Ocon',
28
- 'HAM': 'Lewis Hamilton', 'SAI': 'Carlos Sainz', 'RUS': 'George Russel',
29
- 'BOT': 'Valteri Bottas', 'PIA': 'Oscar Piastri', 'VRI': 'Nyck de Vries',
30
- 'SAR': 'Logan Sargeant', 'RIC': 'Daniel Ricciardo'}
31
-
32
- drivers_2022 = {'Fastest driver': 'Fastest driver', 'VER': 'Max Verstappen',
33
- 'NOR': 'Lando Norris', 'GAS': 'Pierre Gasly', 'PER': 'Sergio Perez',
34
- 'ALO': 'Fernando Alonso', 'LEC': 'Charles Leclerc', 'STR': 'Lance Stroll',
35
- 'MAG': 'Kevin Magnussen', 'TSU': 'Yuki Tsunoda', 'ALB': 'Alexander Albon',
36
- 'ZHO': 'Guanyu Zhou', 'HUL': 'Nico Hülkenberg', 'OCO': 'Esteban Ocon',
37
- 'HAM': 'Lewis Hamilton', 'SAI': 'Carlos Sainz', 'RUS': 'George Russel',
38
- 'BOT': 'Valteri Bottas', 'VRI': 'Nyck de Vries', 'VET': 'Sebastian Vettel',
39
- 'RIC': 'Daniel Ricciardo', 'MSC': 'Mick Schumacher',
40
- 'LAT': 'Nicolas Latifi'}
41
 
42
  app_ui = ui.page_fluid(
43
  {"style": "padding: 10px"},
@@ -68,20 +51,27 @@ app_ui = ui.page_fluid(
68
  ui.row(
69
  ui.column(
70
  6,
71
- drivers.driver_ui('driver1_select')
 
 
 
 
72
  ),
73
  ui.column(
74
  6,
75
- drivers.driver_ui('driver2_select')
 
 
 
 
76
  )
77
  ),
78
  ui.row(
79
  ui.column(
80
  6,
81
- #test_module.test_ui("calc_1")
82
- plot.plot1_ui("gear_1")
83
- #ui.output_text("fastest_driver_1"),
84
- #ui.output_text("laptime_1")
85
  ),
86
  ui.column(
87
  6,
@@ -96,8 +86,6 @@ app_ui = ui.page_fluid(
96
 
97
  def server(input, output, session):
98
 
99
- plot.plot1_server("gear_1")
100
-
101
  # Updating driver selection list
102
  @reactive.Effect()
103
  def _():
@@ -117,6 +105,38 @@ def server(input, output, session):
117
  choices=driver_options,
118
  selected=input.driver2_select()
119
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  # Get required data for driver 2 based on selection
122
  @reactive.Calc
@@ -152,7 +172,7 @@ def server(input, output, session):
152
  @output
153
  @render.text
154
  def fastest_driver_1():
155
- segments, gear, driver, lap_time = plot.plot1_server("gear_1")
156
  #print(f"The driver of the fastest lap this session is: {driver}")
157
  return f"Graph shows the fastest lap of: {driver}"
158
 
@@ -166,7 +186,7 @@ def server(input, output, session):
166
  @output
167
  @render.text
168
  def laptime_1():
169
- segments, gear, driver, lap_time = plot.plot1_server("gear_1")
170
  delta_str= str(lap_time)
171
  # Split the time delta string to extract hours, minutes, and seconds
172
  time_parts = delta_str.split(" ")[-1].split(":")
@@ -191,6 +211,30 @@ def server(input, output, session):
191
 
192
  return f"The lap time is: {formatted_time}"
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  @output
195
  @render.plot
196
  def gear_2():
 
1
  import os
2
+ from shiny import App, ui, render, reactive
3
  import fastf1 as ff1
4
  import matplotlib.pyplot as plt
5
  from matplotlib.collections import LineCollection
6
  from matplotlib import cm
7
  import numpy as np
8
  import shinyswatch
 
 
9
  from modules import drivers
10
 
11
  # Define cache folder path
 
18
  ff1.Cache.offline_mode(enabled=True)
19
 
20
  # Define drivers
21
+ drivers_2023 = drivers.drivers_2023
22
+
23
+ drivers_2022 = drivers.drivers_2022
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  app_ui = ui.page_fluid(
26
  {"style": "padding: 10px"},
 
51
  ui.row(
52
  ui.column(
53
  6,
54
+ ui.input_select(
55
+ "driver1_select", label="Select driver 1:",
56
+ choices = ["Fastest driver"],
57
+ selected = "Fastest driver"
58
+ )
59
  ),
60
  ui.column(
61
  6,
62
+ ui.input_select(
63
+ "driver2_select", label="Select driver 2:",
64
+ choices = ["Fastest driver"],
65
+ selected = "Fastest driver"
66
+ )
67
  )
68
  ),
69
  ui.row(
70
  ui.column(
71
  6,
72
+ ui.output_plot("gear_1"),
73
+ ui.output_text("fastest_driver_1"),
74
+ ui.output_text("laptime_1")
 
75
  ),
76
  ui.column(
77
  6,
 
86
 
87
  def server(input, output, session):
88
 
 
 
89
  # Updating driver selection list
90
  @reactive.Effect()
91
  def _():
 
105
  choices=driver_options,
106
  selected=input.driver2_select()
107
  )
108
+
109
+ # Get required data for driver 2 based on selection
110
+ @reactive.Calc
111
+ def get_data_1():
112
+ try:
113
+ ui.notification_show("Data takes a couple seconds to load.", duration=3, type = 'default')
114
+
115
+ f1_session = ff1.get_session(int(input.year()), input.track_select(), input.session_type())
116
+ f1_session.load()
117
+
118
+ # Check if user input == fastest driver
119
+ if input.driver1_select() == "Fastest driver":
120
+ lap = f1_session.laps.pick_fastest()
121
+ else:
122
+ laps_driver = f1_session.laps.pick_driver(input.driver1_select())
123
+ lap = laps_driver.pick_fastest()
124
+
125
+ tel = lap.get_telemetry()
126
+ driver = lap['Driver']
127
+
128
+ #converting data to numpy data tables
129
+ x = np.array(tel['X'].values)
130
+ y = np.array(tel['Y'].values)
131
+
132
+ points = np.array([x, y]).T.reshape(-1, 1, 2)
133
+ segments = np.concatenate([points[:-1], points[1:]], axis=1)
134
+ gear = tel['nGear'].to_numpy().astype(float)
135
+ lap_time = lap['LapTime']
136
+ return segments, gear, driver, lap_time
137
+
138
+ except Exception:
139
+ ui.notification_show("Data not available. Select another track or driver.", duration=10, type = 'error')
140
 
141
  # Get required data for driver 2 based on selection
142
  @reactive.Calc
 
172
  @output
173
  @render.text
174
  def fastest_driver_1():
175
+ segments, gear, driver, lap_time = get_data_1()
176
  #print(f"The driver of the fastest lap this session is: {driver}")
177
  return f"Graph shows the fastest lap of: {driver}"
178
 
 
186
  @output
187
  @render.text
188
  def laptime_1():
189
+ segments, gear, driver, lap_time = get_data_1()
190
  delta_str= str(lap_time)
191
  # Split the time delta string to extract hours, minutes, and seconds
192
  time_parts = delta_str.split(" ")[-1].split(":")
 
211
 
212
  return f"The lap time is: {formatted_time}"
213
 
214
+ @output
215
+ @render.plot
216
+ def gear_1():
217
+ try:
218
+ segments, gear, driver, lap_time = get_data_1()
219
+
220
+ cmap = cm.get_cmap('Paired')
221
+ lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)
222
+ lc_comp.set_array(gear)
223
+ lc_comp.set_linewidth(4)
224
+
225
+ plt.gca().add_collection(lc_comp)
226
+ plt.axis('equal')
227
+ plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)
228
+
229
+ cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
230
+ cbar.set_ticks(np.arange(1.5, 9.5))
231
+ cbar.set_ticklabels(np.arange(1, 9))
232
+
233
+ plt
234
+
235
+ except Exception:
236
+ pass
237
+
238
  @output
239
  @render.plot
240
  def gear_2():