kbberendsen commited on
Commit
1fe0802
1 Parent(s): b543474

first attempt implementing module - not working

Browse files
__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
@@ -1,11 +1,12 @@
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
 
10
  # Define cache folder path
11
  cache_path = os.getcwd() + "/cache"
@@ -83,7 +84,7 @@ app_ui = ui.page_fluid(
83
  ui.row(
84
  ui.column(
85
  6,
86
- ui.output_plot("gear_1"),
87
  ui.output_text("fastest_driver_1"),
88
  ui.output_text("laptime_1")
89
  ),
@@ -119,38 +120,6 @@ def server(input, output, session):
119
  choices=driver_options,
120
  selected=input.driver2_select()
121
  )
122
-
123
- # Get required data for driver 1 based on selection
124
- @reactive.Calc
125
- def get_data_1():
126
- try:
127
- ui.notification_show("Data takes a couple seconds to load.", duration=3, type = 'default')
128
-
129
- f1_session = ff1.get_session(int(input.year()), input.track_select(), input.session_type())
130
- f1_session.load()
131
-
132
- # Check if user input == fastest driver
133
- if input.driver1_select() == "Fastest driver":
134
- lap = f1_session.laps.pick_fastest()
135
- else:
136
- laps_driver = f1_session.laps.pick_driver(input.driver1_select())
137
- lap = laps_driver.pick_fastest()
138
-
139
- tel = lap.get_telemetry()
140
- driver = lap['Driver']
141
-
142
- #converting data to numpy data tables
143
- x = np.array(tel['X'].values)
144
- y = np.array(tel['Y'].values)
145
-
146
- points = np.array([x, y]).T.reshape(-1, 1, 2)
147
- segments = np.concatenate([points[:-1], points[1:]], axis=1)
148
- gear = tel['nGear'].to_numpy().astype(float)
149
- lap_time = lap['LapTime']
150
- return segments, gear, driver, lap_time
151
-
152
- except Exception:
153
- ui.notification_show("Data not available. Select another track or driver.", duration=10, type = 'error')
154
 
155
  # Get required data for driver 2 based on selection
156
  @reactive.Calc
@@ -186,7 +155,7 @@ def server(input, output, session):
186
  @output
187
  @render.text
188
  def fastest_driver_1():
189
- segments, gear, driver, lap_time = get_data_1()
190
  #print(f"The driver of the fastest lap this session is: {driver}")
191
  return f"Graph shows the fastest lap of: {driver}"
192
 
@@ -200,7 +169,7 @@ def server(input, output, session):
200
  @output
201
  @render.text
202
  def laptime_1():
203
- segments, gear, driver, lap_time = get_data_1()
204
  delta_str= str(lap_time)
205
  # Split the time delta string to extract hours, minutes, and seconds
206
  time_parts = delta_str.split(" ")[-1].split(":")
@@ -225,29 +194,7 @@ def server(input, output, session):
225
 
226
  return f"The lap time is: {formatted_time}"
227
 
228
- @output
229
- @render.plot
230
- def gear_1():
231
- try:
232
- segments, gear, driver, lap_time = get_data_1()
233
-
234
- cmap = cm.get_cmap('Paired')
235
- lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)
236
- lc_comp.set_array(gear)
237
- lc_comp.set_linewidth(4)
238
-
239
- plt.gca().add_collection(lc_comp)
240
- plt.axis('equal')
241
- plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)
242
-
243
- cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
244
- cbar.set_ticks(np.arange(1.5, 9.5))
245
- cbar.set_ticklabels(np.arange(1, 9))
246
-
247
- plt
248
-
249
- except Exception:
250
- pass
251
 
252
  @output
253
  @render.plot
 
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
 
11
  # Define cache folder path
12
  cache_path = os.getcwd() + "/cache"
 
84
  ui.row(
85
  ui.column(
86
  6,
87
+ plot.plot1_ui("gear_1"),
88
  ui.output_text("fastest_driver_1"),
89
  ui.output_text("laptime_1")
90
  ),
 
120
  choices=driver_options,
121
  selected=input.driver2_select()
122
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
  # Get required data for driver 2 based on selection
125
  @reactive.Calc
 
155
  @output
156
  @render.text
157
  def fastest_driver_1():
158
+ segments, gear, driver, lap_time = plot.plot1_server("get_data_1")
159
  #print(f"The driver of the fastest lap this session is: {driver}")
160
  return f"Graph shows the fastest lap of: {driver}"
161
 
 
169
  @output
170
  @render.text
171
  def laptime_1():
172
+ segments, gear, driver, lap_time = plot.plot1_server("get_data_1")
173
  delta_str= str(lap_time)
174
  # Split the time delta string to extract hours, minutes, and seconds
175
  time_parts = delta_str.split(" ")[-1].split(":")
 
194
 
195
  return f"The lap time is: {formatted_time}"
196
 
197
+ plot.plot1_server("gear_1")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
  @output
200
  @render.plot
modules/__pycache__/plot.cpython-311.pyc ADDED
Binary file (5.17 kB). View file
 
modules/plot.py CHANGED
@@ -1,16 +1,22 @@
1
- from shiny import module, ui, render, reactive, event, App
 
2
  import fastf1 as ff1
 
 
 
3
  import numpy as np
 
4
 
5
  # UI
6
  @module.ui
7
- def plot1_ui():
8
- return ui.ouput_plot("gear_1")
9
-
 
10
 
11
  # Server
12
  @module.server
13
- def plot1_server():
14
  @reactive.Calc
15
  # Get required data for driver 1 based on selection
16
  def get_data_1():
@@ -41,4 +47,28 @@ def plot1_server():
41
  return segments, gear, driver, lap_time
42
 
43
  except Exception:
44
- ui.notification_show("Data not available. Select another track or driver.", duration=10, type = 'error')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
10
  # UI
11
  @module.ui
12
+ def plot1_ui(label: str = 'plot1_ui'):
13
+ return ui.div(
14
+ ui.output_plot('gear_1')
15
+ )
16
 
17
  # Server
18
  @module.server
19
+ def plot1_server(input, output, session):
20
  @reactive.Calc
21
  # Get required data for driver 1 based on selection
22
  def get_data_1():
 
47
  return segments, gear, driver, lap_time
48
 
49
  except Exception:
50
+ ui.notification_show("Data not available. Select another track or driver.", duration=10, type = 'error')
51
+
52
+ @output
53
+ @render.plot
54
+ def gear_1():
55
+ try:
56
+ segments, gear, driver, lap_time = get_data_1()
57
+
58
+ cmap = cm.get_cmap('Paired')
59
+ lc_comp = LineCollection(segments, norm=plt.Normalize(1, cmap.N+1), cmap=cmap)
60
+ lc_comp.set_array(gear)
61
+ lc_comp.set_linewidth(4)
62
+
63
+ plt.gca().add_collection(lc_comp)
64
+ plt.axis('equal')
65
+ plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)
66
+
67
+ cbar = plt.colorbar(mappable=lc_comp, label="Gear", boundaries=np.arange(1, 10))
68
+ cbar.set_ticks(np.arange(1.5, 9.5))
69
+ cbar.set_ticklabels(np.arange(1, 9))
70
+
71
+ plt
72
+
73
+ except Exception:
74
+ pass