kbberendsen commited on
Commit
6166cea
1 Parent(s): 42e25ea

start sidebar module

Browse files
Files changed (4) hide show
  1. __pycache__/app.cpython-311.pyc +0 -0
  2. app.py +4 -21
  3. modules/plot.py +0 -74
  4. modules/sidebar.py +29 -0
__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,12 +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
- from modules import drivers
10
 
11
  # Define cache folder path
12
  cache_path = os.getcwd() + "/cache"
@@ -19,7 +19,6 @@ 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(
@@ -29,24 +28,8 @@ app_ui = ui.page_fluid(
29
  ui.panel_title("Gear usage in fastest lap"),
30
  ui.layout_sidebar(
31
  ui.panel_sidebar(
32
- ui.input_select(
33
- "track_select", "Select track:",
34
- choices = ["Austria", "Hungary", "Spain", "Bahrain", "United-Kingdom"],
35
- selected = "Austria"
36
- ),
37
- ui.input_radio_buttons(
38
- "session_type", "Session type:",
39
- choices = {"R": "Race", "Q": "Qualification"},
40
- selected = "R"
41
- ),
42
- ui.input_radio_buttons(
43
- "year", "Year:",
44
- choices = ["2023", "2022"],
45
- selected = "2023"
46
- ),
47
- width=2
48
  ),
49
-
50
  ui.panel_main(
51
  ui.row(
52
  ui.column(
@@ -89,7 +72,7 @@ def server(input, output, session):
89
  # Updating driver selection list
90
  @reactive.Effect()
91
  def _():
92
- if input.year() == "2023":
93
  driver_options = drivers_2023
94
  elif input.year() == "2022":
95
  driver_options = drivers_2022
 
1
  import os
2
+ from shiny import App, ui, render, reactive, module
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, sidebar
10
 
11
  # Define cache folder path
12
  cache_path = os.getcwd() + "/cache"
 
19
 
20
  # Define drivers
21
  drivers_2023 = drivers.drivers_2023
 
22
  drivers_2022 = drivers.drivers_2022
23
 
24
  app_ui = ui.page_fluid(
 
28
  ui.panel_title("Gear usage in fastest lap"),
29
  ui.layout_sidebar(
30
  ui.panel_sidebar(
31
+ sidebar.sidebar_content("sidebar_content")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  ),
 
33
  ui.panel_main(
34
  ui.row(
35
  ui.column(
 
72
  # Updating driver selection list
73
  @reactive.Effect()
74
  def _():
75
+ if sidebar.sidebar_content("year") == "2023":
76
  driver_options = drivers_2023
77
  elif input.year() == "2022":
78
  driver_options = drivers_2022
modules/plot.py DELETED
@@ -1,74 +0,0 @@
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'):
13
- return ui.div(
14
- ui.output_plot('plot1')
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():
23
- try:
24
- ui.notification_show("Data takes a couple seconds to load.", duration=3, type = 'default')
25
-
26
- f1_session = ff1.get_session(int(input.year()), input.track_select(), input.session_type())
27
- f1_session.load()
28
-
29
- # Check if user input == fastest driver
30
- if input.driver1_select() == "Fastest driver":
31
- lap = f1_session.laps.pick_fastest()
32
- else:
33
- laps_driver = f1_session.laps.pick_driver(input.driver1_select())
34
- lap = laps_driver.pick_fastest()
35
-
36
- tel = lap.get_telemetry()
37
- driver = lap['Driver']
38
-
39
- #converting data to numpy data tables
40
- x = np.array(tel['X'].values)
41
- y = np.array(tel['Y'].values)
42
-
43
- points = np.array([x, y]).T.reshape(-1, 1, 2)
44
- segments = np.concatenate([points[:-1], points[1:]], axis=1)
45
- gear = tel['nGear'].to_numpy().astype(float)
46
- lap_time = lap['LapTime']
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
modules/sidebar.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @module.ui
11
+ def sidebar_content(id):
12
+ return ui.div(
13
+ ui.input_select(
14
+ "track_select", "Select track:",
15
+ choices = ["Austria", "Hungary", "Spain", "Bahrain", "United-Kingdom"],
16
+ selected = "Austria"
17
+ ),
18
+ ui.input_radio_buttons(
19
+ "session_type", "Session type:",
20
+ choices = {"R": "Race", "Q": "Qualification"},
21
+ selected = "R"
22
+ ),
23
+ ui.input_radio_buttons(
24
+ "year", "Year:",
25
+ choices = ["2023", "2022"],
26
+ selected = "2023"
27
+ ),
28
+ width=2
29
+ )