-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash_app.py
1262 lines (1083 loc) · 67.5 KB
/
dash_app.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Import necessary libraries
import dash
from dash import html, dcc, dash_table, ctx, callback_context
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
from dash import dash_table
import pandas as pd
from sqlalchemy import create_engine
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from urllib.request import urlopen
import json
from scipy.ndimage import gaussian_filter1d, uniform_filter
import requests
import utils
import sys
import warnings
warnings.filterwarnings("ignore")
#Session and circuit information
track_config = pd.read_csv('config/track_config.csv')
pit_limit = 80
location = sys.argv[1]
year = int(sys.argv[2])
needed_session = sys.argv[3]
track = track_config.query(f''' circuit_location == '{location}' ''')
TOTAL_LAPS = int(track.total_laps.iloc[0])
circuit_length = int(track.circuit_length.iloc[0])
corners = eval(track.corners.iloc[0])
start_line = eval(track.start_line.iloc[0])
before_start_line = eval(track.before_start_line.iloc[0])
after_start_line = eval(track.after_start_line.iloc[0])
start_line_theta = np.arctan((after_start_line[1] - before_start_line[1])/(after_start_line[0] - before_start_line[0]))
start_line_points = pd.DataFrame([(start_line[0] - r * np.sin(start_line_theta), start_line[1] + r * np.cos(start_line_theta)) for r in np.arange(-500, 500, 5)], columns = ['x', 'y'])
session = utils.get_session(location, year)
session_key = session.query(f" session_name == '{needed_session}'").session_key.iloc[0]
race_start_time = pd.to_datetime(session.query(f" session_name == '{needed_session}'").date_start.iloc[0])
GRID = utils.get_starting_grid(session_key, race_start_time)
GRID_REVERSE = {v: k for k, v in GRID.items()}
# Connect to your SQL database
engine = create_engine(f"sqlite:///data/{session_key}.db")
print(f"Loading from data/{session_key}.db")
driver_data = pd.read_csv(f'config/driver_config_{year}.csv')
team_groups = {k: v['name_acronym'].tolist() for k, v in driver_data[['team_acronym', 'name_acronym']].groupby('team_acronym')}
driver_config = {}
driver_config['driver_code'] = {}
driver_config['driver_number'] = {}
driver_config['team_name'] = {}
driver_config['team_colour'] = {}
driver_config['team_order'] = {}
driver_config['driver_order'] = {}
# driver_config['team_code'] = {}
for ind, driver in driver_data.sort_values(by = ['team_order', 'driver_number']).iterrows():
driver_config['driver_number'][driver['name_acronym']] = driver['driver_number']
driver_config['driver_code'][driver['driver_number']] = driver['name_acronym']
driver_config['team_name'][driver['name_acronym']]= f'''{driver['team_name']}'''
driver_config['team_colour'][driver['name_acronym']]= f'''#{driver['team_colour']}'''
# driver_config['team_code'][driver['name_acronym']]= f'''{driver['team_acronym']}'''
driver_config['team_order'][driver['name_acronym']] = driver['team_order']
driver_config['driver_order'][driver['name_acronym']] = driver['driver_order']
drs_map = {k:50 if k in [10,12,14] else 0 for k in range(15)}
lines = {}
for name, color in driver_config['team_colour'].items():
lines[driver_config['driver_number'][name]] = dict(color=f"{color}")
if driver_config['driver_number'][name] in [11, 18, 22, 23, 27, 31, 55, 63, 77, 81]:
lines[driver_config['driver_number'][name]]['dash'] = 'dot'
# driver_config_reverse = {v: k for k, v in driver_config.items()}
def get_lap_dur(driv,lap):
response = urlopen(f'https://api.openf1.org/v1/laps?session_key={session_key}&driver_number={driv}&lap_number={lap}')
return json.loads(response.read().decode('utf-8'))[0]["lap_duration"]
columns = ['driver_code']
# columns += [str(lap) for lap in range(1, 11)]
# Define your Dash app
# app = dash.Dash(__name__)
app = dash.Dash(__name__,external_stylesheets=[dbc.themes.BOOTSTRAP])
driver1_button_group = html.Div(
[
dbc.RadioItems(
id="driver1-radiobuttons",
className="btn-group",
inputClassName="btn-check",
labelClassName="btn btn-outline-secondary",
labelCheckedClassName="active",
options=[{'label': html.Div([key], style={'color': 'Black', 'font-size': 20, 'text-align': 'center'}), 'value':key} for key, value in driver_config['driver_number'].items()],
value='VER',
# labelStyle= {"margin":"0.001rem"}
),
html.Div(id="output"),
],
className="radio-group",
)
driver2_button_group = html.Div(
[
dbc.RadioItems(
id="driver2-radiobuttons",
className="btn-group",
inputClassName="btn-check",
labelClassName="btn btn-outline-secondary",
labelCheckedClassName="active",
options=[{'label': html.Div([key], style={'color': 'Black', 'font-size': 20, 'text-align': 'center'}), 'value':key} for key, value in driver_config['driver_number'].items()],
value='LEC',
# labelStyle= {"margin":"0.001rem"}
),
html.Div(id="output"),
],
className="radio-group",
)
group1_buttons = html.Div(
[
dbc.Checklist(
id="group1-buttons",
className="btn-group",
inputClassName="btn-check",
labelClassName="btn btn-outline-secondary",
labelCheckedClassName="active",
options=[{'label': html.Div([key], style={'color': 'Black', 'font-size': 20, 'text-align': 'center'}), 'value':key} for key, value in driver_config['driver_number'].items()],
value='VER',
# labelStyle= {"margin":"0.001rem"}
),
html.Div(id="output"),
],
className="radio-group",
)
group2_buttons = html.Div(
[
dbc.Checklist(
id="group2-buttons",
className="btn-group",
inputClassName="btn-check",
labelClassName="btn btn-outline-secondary",
labelCheckedClassName="active",
options=[{'label': html.Div([key], style={'color': 'Black', 'font-size': 20, 'text-align': 'center'}), 'value':key} for key, value in driver_config['driver_number'].items()],
value='VER',
# labelStyle= {"margin":"0.001rem"}
),
html.Div(id="output"),
],
className="radio-group",
)
group3_buttons = html.Div(
[
dbc.Checklist(
id="group3-buttons",
className="btn-group",
inputClassName="btn-check",
labelClassName="btn btn-outline-secondary",
labelCheckedClassName="active",
options=[{'label': html.Div([key], style={'color': 'Black', 'font-size': 20, 'text-align': 'center'}), 'value':key} for key, value in driver_config['driver_number'].items()],
value='VER',
# labelStyle= {"margin":"0.001rem"}
),
html.Div(id="output"),
],
className="radio-group",
)
lap1_button_group = html.Div(
[
dbc.RadioItems(
id="lap1-radiobuttons",
className="btn-group",
inputClassName="btn-check",
labelClassName="btn btn-outline-secondary",
labelCheckedClassName="active",
options=[{'label': html.Div([lap_number], style={'color': 'Black', 'font-size': 16, 'text-align': 'center'}), 'value':lap_number} for lap_number in range(0, 1 + TOTAL_LAPS)],
# size="sm",
value=2, # Default value
# labelStyle= {"margin":"0.001rem"}
),
html.Div(id="output"),
],
className="radio-group",
)
lap2_button_group = html.Div(
[
dbc.RadioItems(
id="lap2-radiobuttons",
className="btn-group",
inputClassName="btn-check",
labelClassName="btn btn-outline-secondary",
labelCheckedClassName="active",
options=[{'label': html.Div([lap_number], style={'color': 'Black', 'font-size': 16, 'text-align': 'center'}), 'value':lap_number} for lap_number in range(0, 1 + TOTAL_LAPS)],
value=2, # Default value
# labelStyle= {"margin":"0.001rem"}
),
html.Div(id="output"),
],
className="radio-group",
)
group_button_group = html.Div(
[
dbc.RadioItems(
id="group-radiobuttons",
className="btn-group",
inputClassName="btn-check",
labelClassName="btn btn-outline-secondary",
labelCheckedClassName="active",
options=[{'label': html.Div([group], style={'color':'Black', 'font-size':16, 'text-align':'center'}), 'value': group} for group in list(team_groups.keys()) + ['G1', 'G2', 'G3', 'ALL']],
value='ALL',
),
html.Div(id='output'),
],
className='radio-group',
)
fuel_toggle_group = html.Div(
[
dbc.RadioItems(
id="fuel-toggle-radiobuttons",
className="btn-group",
inputClassName="btn-check",
labelClassName="btn btn-outline-secondary",
labelCheckedClassName="active",
options=[{'label': html.Div([group[0]], style={'color':'Black', 'font-size':16, 'text-align':'center'}), 'value': group[1]} for group in [('Regular', 0), ('Corrected', 1)]],
value=0,
),
html.Div(id='output'),
],
className='radio-group',
)
latest_tele_toggle_group = html.Div(
[
dbc.RadioItems(
id="latest-tele-toggle-radiobuttons",
className="btn-group",
inputClassName="btn-check",
labelClassName="btn btn-outline-secondary",
labelCheckedClassName="active",
options=[{'label': html.Div([group[0]], style={'color':'Black', 'font-size':16, 'text-align':'center'}), 'value': group[1]} for group in [('Selection', 0), ('Latest', 1)]],
value=0,
),
html.Div(id='output'),
],
className='radio-group',
)
# Define the layout of your app
app.layout = html.Div([
html.H1(f"Laptime Comparison for Race {location}, {year}"),
dbc.Col([
dbc.Row([driver1_button_group]),
dbc.Row([lap1_button_group]),
dbc.Row([driver2_button_group]),
dbc.Row([lap2_button_group]),
], align = 'left',
),
html.Button('Submit', id='telemetry-submit-value', n_clicks=0),
html.Button('Previous', id='telemetry-prev-value', n_clicks=0),
html.Button('Next', id='telemetry-next-value', n_clicks=0),
dbc.Col([
dbc.Row([latest_tele_toggle_group]),
]),
#html.Button('Latest', id='telemetry-latest-value', n_clicks=0),
dcc.Interval(id='telemetry-updater-component', interval=5000, n_intervals=0),
dcc.Interval(id='weather-updater-component', interval=30000, n_intervals=0),
dcc.Interval(id='laptime-updater-component', interval=10000, n_intervals=0),
dcc.Interval(id='race-control-updater-component', interval=10000, n_intervals=0),
dcc.Interval(id='maxspeed-updater-component', interval=15000, n_intervals=0),
dcc.Interval(id='position-updater-component', interval=15000, n_intervals=0),
dcc.Interval(id='track-location-updater-component', interval=5000, n_intervals=0),
dcc.Interval(id='pitstop-updater-component', interval=10000, n_intervals=0),
dcc.Interval(id='pitstop-formatting-updater-component', interval=10000, n_intervals=0),
dcc.Interval(id='lap-position-updater-component', interval=10000, n_intervals=0),
dcc.Graph(id='scatter-plot'),
# dash_table.DataTable(
# id='corner-minspeed-table',
# style_data={ 'border': '1px solid black' },
# style_header={ 'border': '2px solid black', 'textAlign' : 'center'},
# columns=[{'name': str(col), 'id': str(col)} for col in ['driver_code'] + [*range(1, 1+len(corners))]],
# fill_width=False,
# ),
dbc.Col([
dbc.Row([html.H5("Group 1"), group1_buttons]),
dbc.Row([html.H5("Group 2"), group2_buttons]),
dbc.Row([html.H5("Group 3"), group3_buttons]),
], align = 'left',
),
html.Br(),
dbc.Row([
dbc.Col([group_button_group], align='left', ),
dbc.Col(["Threshold ", dcc.Input(id="laptime-threshold-input", type="number", placeholder="", size='5px', step=1, value = 200, style={'width':'20%'}),], align='right', ),
dbc.Col([fuel_toggle_group], align='right', ),
]),
dcc.Graph(id='laptime-plot'),
html.Div([
html.Div([
html.H2("Positions"),
dash_table.DataTable(
id='position-table',
style_data={'border': '1px solid black',},
style_header={ 'border': '2px solid black', 'textAlign' : 'center', 'backgroundColor' : '#aaaaaa',},
style_cell_conditional=[{'if': {'column_id': 'driver_code'}, 'textAlign': 'center',},
{'if': {'column_id': 'position'}, 'textAlign': 'center',},
{'if': {'column_id': 'lap_number'}, 'textAlign': 'center',},
{'if': {'column_id': 'fastest'}, 'textAlign': 'center',},
{'if': {'column_id': 'f_lap'}, 'textAlign': 'center',},
{'if': {'column_id': 'tyre_code'}, 'textAlign': 'left',},
],
columns=[{'name': col, 'id': col} for col in ['date', 'driver_code', 'position', 'gap', 'interval', 'lap_number', 'fastest', 'f_lap', 'n', 'n-1', 'n-2', 'tyre_code']],
fill_width=False,
),],
style={'width': '49%', 'display': 'inline-block'},
),
html.Div([dcc.Graph(id='track-location-plot')], style={'width': '49%', 'display': 'inline-block'}),
], style={'display': 'flex'},
),
dcc.Graph(id='lap-position-plot'),
html.Div([
html.Div([
html.H2("Pitstops"),
dash_table.DataTable(
id='pitstop-table',
style_data={ 'border': '1px solid black' },
style_header={ 'border': '2px solid black', 'textAlign' : 'center', 'backgroundColor' : '#aaaaaa'},
style_cell_conditional=[{'if': {'column_id': 'driver_code'}, 'textAlign': 'center'}] +
[{'if': {'column_id': f'lap{i}'}, 'textAlign': 'center', 'backgroundColor' : '#cccccc'} for i in range(5)] +
[{'if': {'column_id': f'pit{i}'}, 'textAlign': 'right',} for i in range(5)] +
[{'if': {'column_id': f'rest{i}'}, 'textAlign': 'right',} for i in range(5)],
columns=[{'name': str(col), 'id': str(col)} for col in ['driver_code', 'out_lap_number', 'duration_pit', 'duration_rest']],
fill_width=False,
),
], style={'width': '49%', 'display': 'inline-block'}),
html.Div([
html.H2("Race Control"),
dash_table.DataTable(
id='race-control-table',
# filter_action="native",
style_data={ 'border': '1px solid black' },
style_header={ 'border': '2px solid black', 'textAlign' : 'center', 'backgroundColor' : '#aaaaaa'},
style_cell={'whiteSpace': 'normal'},
style_cell_conditional=[{'if': {'column_id': 'lap_number'}, 'textAlign': 'center'},
{'if': {'column_id': 'flag'}, 'textAlign': 'center'},
{'if': {'column_id': 'message'}, 'textAlign': 'left'}, ],
style_table={'maxHeight': '600px', 'maxWidth': '900px', 'overflowY': 'scroll'},
columns=[{'name': col, 'id': col} for col in ['date', 'lap_number', 'flag', 'message']],
fill_width=False,
),
], style={'width': '49%', 'display': 'inline-block'}),
]),
html.H2("Max Speed"),
dash_table.DataTable(
id='maxspeed-table',
style_data={ 'border': '1px solid black'},
style_header={ 'border': '2px solid black', 'textAlign' : 'center', 'backgroundColor' : '#aaaaaa'},
style_cell_conditional=[{'if': {'column_id': 'driver_code'}, 'textAlign': 'center'}],
columns=[{'name': col, 'id': col} for col in columns],
fill_width=False,
),
dcc.Graph(id='weather-plot'),
html.H2("Samples"),
dash_table.DataTable(
id='samples-table',
style_data={ 'border': '1px solid black' },
style_header={ 'border': '2px solid black', 'textAlign' : 'center'},
columns=[{'name': col, 'id': col} for col in columns],
fill_width=False,
),
])
# Define callback to update the displayed scatter plot based on the selected table and columns
@app.callback(
Output('scatter-plot', 'figure'),
[State('driver1-radiobuttons', 'value'),
State('lap1-radiobuttons', 'value'),
State('driver2-radiobuttons', 'value'),
State('lap2-radiobuttons', 'value'),
Input('telemetry-submit-value', 'n_clicks'),
Input('telemetry-updater-component', 'n_intervals'),
Input('telemetry-prev-value', 'n_clicks'),
Input('telemetry-next-value', 'n_clicks'),
Input('latest-tele-toggle-radiobuttons', 'value'),
]
)
def update_scatter_plot(driver1, lap1_number, driver2, lap2_number, n_clicks, n_intervals, prev, next, latest):
driver1_number = driver_config['driver_number'][driver1.upper()]
driver2_number = driver_config['driver_number'][driver2.upper()]
query = f"SELECT * FROM telemetry WHERE ((driver_number = '{driver1_number}' and lap_number = '{lap1_number}') or (driver_number = '{driver2_number}' and lap_number = '{lap2_number}'));"
df = pd.read_sql_query(query, engine)
df['date'] = pd.to_datetime(df.date, format='ISO8601')
df[['rpm', 'speed','n_gear', 'throttle', 'drs', 'brake', 'driver_number', 'lap_number']] = df[['rpm', 'speed', 'n_gear', 'throttle', 'drs', 'brake', 'driver_number', 'lap_number']].astype(int)
df['drs'] = df['drs'].map(drs_map)
df1 = df[(df.lap_number == lap1_number) & (df.driver_number == driver1_number)]
df2 = df[(df.lap_number == lap2_number) & (df.driver_number == driver2_number)]
# df1 = pd.read_sql_query(query, engine)
# df1['date'] = pd.to_datetime(df1.date, format='ISO8601')
# df1[['rpm', 'speed','n_gear', 'throttle', 'drs', 'brake']] = df1[['rpm', 'speed', 'n_gear', 'throttle', 'drs', 'brake']].astype(int)
# query = f"SELECT * FROM telemetry WHERE driver_number = '{driver2_number}' and lap_number = '{lap2_number}';"
# df2 = pd.read_sql_query(query, engine)
# df2['date'] = pd.to_datetime(df2.date, format='ISO8601')
# df2[['rpm', 'speed','n_gear', 'throttle', 'drs', 'brake']] = df2[['rpm', 'speed', 'n_gear', 'throttle', 'drs', 'brake']].astype(int)
df1['time'] = df1['date'] - df1['date'].iloc[0]
df2['time'] = df2['date'] - df2['date'].iloc[0]
df1['actual_distance_smoothed'] = gaussian_filter1d(df1.actual_distance, sigma = 10)
df2['actual_distance_smoothed'] = gaussian_filter1d(df2.actual_distance, sigma = 10)
delta = utils.delta_time(df1, df2)
smoothed_delta = uniform_filter(delta, size = 30)
line_driver2 = dict(color=f"{driver_config['team_colour'][driver2.upper()]}")
if driver_config['team_colour'][driver1.upper()] == driver_config['team_colour'][driver2.upper()]:
line_driver2['dash'] = "dot"
# time1 = df1['date'] - df1['date'].iloc[0]
# time2 = df2['date'] - df2['date'].iloc[0]
speeds = [go.Scatter(x=df1.actual_distance_smoothed, y=df1['speed'], mode='lines', name=f'{driver1.upper()}, Lap {lap1_number}', line=dict(color=f"{driver_config['team_colour'][driver1.upper()]}"), legendgroup='group1'),
go.Scatter(x=df2.actual_distance_smoothed, y=df2['speed'], mode='lines', name=f'{driver2.upper()}, Lap {lap2_number}', line=line_driver2, legendgroup='group2')]
drss = [go.Scatter(x=df1.actual_distance_smoothed, y=df1['drs'], mode='lines', name=f'{driver1.upper()}', line=dict(color=f"{driver_config['team_colour'][driver1.upper()]}"), legendgroup='group1', showlegend=False),
go.Scatter(x=df2.actual_distance_smoothed, y=df2['drs'], mode='lines', name=f'{driver2.upper()}', line=line_driver2, legendgroup='group2', showlegend=False)]
for corner in corners:
speeds.append(go.Scatter(x=[corner,corner], y=[0,320], mode='lines', line=dict(color="#404040", dash="dot"), showlegend=False))
deltas = [go.Scatter(x=df1.actual_distance_smoothed, y=smoothed_delta, mode='lines', showlegend= False, line=dict(color="#404040"))]
deltas.append(go.Scatter(x=[0, circuit_length], y=[0,0], mode='lines', line=dict(color="#606060", dash = "dash"), showlegend=False))
for corner in corners:
deltas.append(go.Scatter(x=[corner,corner], y=[min(smoothed_delta), max(smoothed_delta)], mode='lines', line=dict(color="#404040", dash="dot"), showlegend=False))
throttles = [go.Scatter(x=df1.actual_distance_smoothed, y=df1['throttle'], mode='lines', name=f'{driver1.upper()}', line=dict(color=f"{driver_config['team_colour'][driver1.upper()]}"), legendgroup='group1',showlegend=False),
go.Scatter(x=df2.actual_distance_smoothed, y=df2['throttle'], mode='lines', name=f'{driver2.upper()}', line=line_driver2, legendgroup='group2',showlegend=False),]
for corner in corners:
throttles.append(go.Scatter(x=[corner,corner], y=[0,100], mode='lines', line=dict(color="#404040", dash="dot"), showlegend=False))
brakes = [go.Scatter(x=df1.actual_distance_smoothed, y=df1['brake'], mode='lines', name=f'{driver1.upper()}', line=dict(color=f"{driver_config['team_colour'][driver1.upper()]}"), legendgroup='group1',showlegend=False),
go.Scatter(x=df2.actual_distance_smoothed, y=df2['brake'], mode='lines', name=f'{driver2.upper()}', line=line_driver2, legendgroup='group2',showlegend=False)]
for corner in corners:
brakes.append(go.Scatter(x=[corner,corner], y=[0,100], mode='lines', line=dict(color="#404040", dash="dot"), showlegend=False))
rpms = [go.Scatter(x=df1.actual_distance_smoothed, y=df1['rpm'], mode='lines', name=f'{driver1.upper()}', line=dict(color=f"{driver_config['team_colour'][driver1.upper()]}"), legendgroup='group1',showlegend=False),
go.Scatter(x=df2.actual_distance_smoothed, y=df2['rpm'], mode='lines', name=f'{driver2.upper()}', line=line_driver2, legendgroup='group2',showlegend=False)]
for corner in corners:
rpms.append(go.Scatter(x=[corner,corner], y=[0,12000], mode='lines', line=dict(color="#404040", dash="dot"), showlegend=False))
gears = [go.Scatter(x=df1.actual_distance_smoothed, y=df1['n_gear'], mode='lines', name=f'{driver1.upper()}', line=dict(color=f"{driver_config['team_colour'][driver1.upper()]}"), legendgroup='group1',showlegend=False),
go.Scatter(x=df2.actual_distance_smoothed, y=df2['n_gear'], mode='lines', name=f'{driver2.upper()}', line=line_driver2, legendgroup='group2',showlegend=False)]
for corner in corners:
gears.append(go.Scatter(x=[corner,corner], y=[0,8], mode='lines', line=dict(color="#404040", dash="dot"), showlegend=False))
# fig = make_subplots(rows=5, cols=1, vertical_spacing = 0.01, row_width = [0.4, 0.15, 0.15, 0.15, 0.15])
fig = make_subplots(rows=6, cols=1, vertical_spacing = 0.005,
row_width = [0.12, 0.12, 0.12, 0.12, 0.22, 0.30]) # don't ask me how this works, the reverse of this row_width list is what I expected to work - it made the last plot the tallest
for trace in speeds:
fig.add_trace(trace, row=1, col=1)
for trace in deltas:
fig.add_trace(trace, row=2, col=1)
for trace in throttles:
fig.add_trace(trace, row=3, col=1)
for trace in brakes:
fig.add_trace(trace, row=4, col=1)
for trace in rpms:
fig.add_trace(trace, row=5, col=1)
for trace in gears:
fig.add_trace(trace, row=6, col=1)
for trace in drss:
fig.add_trace(trace, row=1, col=1)
plot_list = ["Speed/DRS", f"Live Delta<br><-- {driver2.upper()} {lap2_number} faster | {driver1.upper()} {lap1_number} faster -->", "Throttle", "Brake", "RPM", "Gear"]
for i in range(6):
if i == 0:
fig['layout']['yaxis']['title']= plot_list[i]
fig['layout']['xaxis']['range']= [0, circuit_length]
else:
fig['layout'][f'yaxis{i+1}']['title']= plot_list[i]
fig['layout'][f'xaxis{i+1}']['range']= [0, circuit_length]
# fig['layout']['yaxis']['title']="Speed/DRS"
# fig['layout']['yaxis2']['title']="Delta"
# fig['layout']['yaxis3']['title']="Throttle"
# fig['layout']['yaxis4']['title']="Brake"
# fig['layout']['yaxis5']['title']="RPM"
# fig['layout']['yaxis6']['title']="Gear"
fig.update_layout(uirevision=8, height=1400, width=1800, title_text=f'''{driver1.upper()} ({lap1_number}) : {get_lap_dur(driver1_number, lap1_number)}s, {driver2.upper()} ({lap2_number}): {get_lap_dur(driver2_number,lap2_number)}s''')
fig.update_xaxes(showticklabels=False)
fig.update_yaxes(minor=dict(tickvals=np.arange(0,350,10), tickmode='array', showgrid=True, ticks="inside"))
return fig
@app.callback([
Output('lap1-radiobuttons', 'value'),
Output('lap2-radiobuttons', 'value'),],
[
State('driver1-radiobuttons', 'value'),
State('driver2-radiobuttons', 'value'),
State('lap1-radiobuttons', 'value'),
State('lap2-radiobuttons', 'value'),
Input('telemetry-prev-value', 'n_clicks'),
Input('telemetry-next-value', 'n_clicks'),
Input('latest-tele-toggle-radiobuttons', 'value'),
Input('telemetry-updater-component', 'n_intervals')]
)
def update_lap_number(driver1, driver2, lap1, lap2, prev, next, latest, n_intervals):
ctx = callback_context
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
if latest==1:
query = f"select driver_number, max(lap_number) as lap_number from telemetry group by driver_number"
df_laps = pd.read_sql_query(query, engine)
return df_laps[df_laps['driver_number']==driver_config['driver_number'][driver1.upper()]].iloc[0,1], df_laps[df_laps['driver_number']==driver_config['driver_number'][driver2.upper()]].iloc[0,1]
elif latest==0:
if button_id == 'telemetry-prev-value':
if lap1 == 1:
lap1 = TOTAL_LAPS + 1
if lap2 == 1:
lap2 = TOTAL_LAPS + 1
return lap1 - 1, lap2 - 1
elif button_id == 'telemetry-next-value':
return (lap1 % TOTAL_LAPS) + 1, (lap2 % TOTAL_LAPS) + 1
else:
return lap1, lap2
# # Define callback to update the displayed scatter plot based on the selected table and columns
# @app.callback(
# Output('corner-minspeed-table', 'data'),
# [State('driver1-radiobuttons', 'value'),
# State('lap1-radiobuttons', 'value'),
# State('driver2-radiobuttons', 'value'),
# State('lap2-radiobuttons', 'value'),
# Input('telemetry-submit-value', 'n_clicks'),
# Input('telemetry-updater-component', 'n_intervals')]
# )
# def update_corner_minspeed_table(driver1, lap1_number, driver2, lap2_number, n_clicks, n_intervals):
# driver1_number = driver_config['driver_number'][driver1.upper()]
# driver2_number = driver_config['driver_number'][driver2.upper()]
# dist_ranges = [(corner - 30, corner + 30) for corner in corners]
# query = f" SELECT driver_number, lap_number, actual_distance, speed FROM telemetry WHERE ((driver_number = {driver1_number} and lap_number = {lap1_number}) OR (driver_number = {driver2_number} and lap_number = {lap2_number})) AND ("
# subquery = ' OR '.join([f'(actual_distance > {corner[0]} AND actual_distance < {corner[1]})' for corner in dist_ranges])
# query += subquery + ')'
# # print(query)
# df = pd.read_sql_query(query, engine)
# # print(len(df))
# # df['date'] = pd.to_datetime(df.date, format='ISO8601')
# df[['actual_distance', 'speed']] = df[['actual_distance', 'speed']].astype(float)
# # print(df)
# groups = df.groupby(['driver_number', 'lap_number'])
# df1 = groups.get_group((driver1_number, lap1_number)).sort_values(by = 'actual_distance')
# df2 = groups.get_group((driver2_number, lap2_number)).sort_values(by = 'actual_distance')
# dist1 = gaussian_filter1d(df1.actual_distance, sigma = 10)
# dist2 = gaussian_filter1d(df2.actual_distance, sigma = 10)
# data1 = [driver_config['driver_code'][driver1_number]] + [df1[(df1.actual_distance < corner[1]) & (df1.actual_distance > corner[0])].speed.round(0).min() for corner in dist_ranges]
# data2 = [driver_config['driver_code'][driver2_number]] + [df2[(df2.actual_distance < corner[1]) & (df2.actual_distance > corner[0])].speed.round(0).min() for corner in dist_ranges]
# # print(data1, data2)
# columns = ['driver_code'] + [str(x) for x in range(1, 1+ len(corners))]
# data = pd.concat([pd.DataFrame(data1).T, pd.DataFrame(data2).T])
# data.columns = columns
# return data.to_dict('records')
@app.callback(
Output('laptime-plot', 'figure'),
[
Input('group-radiobuttons', 'value'),
Input('laptime-threshold-input', 'value'),
Input('fuel-toggle-radiobuttons', 'value'),
Input('group1-buttons', 'value'),
Input('group2-buttons', 'value'),
Input('group3-buttons', 'value'),
Input('laptime-updater-component', 'n_intervals'),
]
)
def update_laptime_plot(group_name, laptime_threshold, is_corrected, group1, group2, group3, n_intervals):
# Replace this with your data update logic
def fuel_corrected_laptime(lap_duration, lap_number, TOTAL_LAPS, TOTAL_FUEL = 110, TIME_LOST_PER_KG = 0.035):
return lap_duration - (1 - lap_number/TOTAL_LAPS) * TOTAL_FUEL * TIME_LOST_PER_KG
team_groups.update({'G1':group1})
team_groups.update({'G2':group2})
team_groups.update({'G3':group3})
team_groups['ALL'] = driver_config['driver_number'].keys()
query = f"SELECT driver_number, lap_number, lap_duration FROM laptimes"
df = pd.read_sql_query(query, engine).dropna().astype(float).query(f"lap_duration < {laptime_threshold}")
df['lap_duration_fuel_corrected'] = df.apply(lambda x: fuel_corrected_laptime(x.lap_duration, x.lap_number, TOTAL_LAPS), axis = 1)
df['driver_order'] = df.driver_number.map(driver_config['driver_code']).map(driver_config['driver_order'])
#df = get_data(f'https://api.openf1.org/v1/laps?session_key={session_key}')
#df = df[['driver_number', 'lap_number', 'lap_duration']].dropna().astype(float).query(f"lap_duration < {laptime_threshold}")
#df = pd.read_sql_query(query, engine).dropna().astype(float).query(f"lap_duration < {laptime_threshold}")
# df_ = df.pivot(columns = 'lap_number', index = 'driver_number', values = 'lap_duration').round(3)
# df_.columns = [int(x) for x in df_.columns]
# df_ = df_[sorted(df_.columns.tolist())].reset_index()
traces = []
visibility = {driver_no: True if driver_code in team_groups[group_name] else "legendonly" for driver_code, driver_no in driver_config['driver_number'].items()}
for k, v in df.sort_values(by = ['driver_order', 'lap_number']).groupby('driver_order'):
v[['driver_number','lap_number']] = v[['driver_number','lap_number']].astype(int)
v.set_index('lap_number', inplace=True)
x = np.arange(1,max(v.index)+1)
y = [v['lap_duration_fuel_corrected' if is_corrected == 1 else 'lap_duration'].loc[i] if float(i) in v.index.values else None for i in range(1,max(v.index)+1) ]
traces.append(go.Scatter(x=x, y=y, mode='markers+lines', name=f'{driver_config["driver_code"][v.driver_number.iloc[0]]}', line=lines[v.driver_number.iloc[0]], visible=visibility[v.driver_number.iloc[0]]))
layout = go.Layout(title = 'Fuel Corrected Laptimes' if is_corrected == 1 else 'Regular Laptimes', xaxis=dict(title='Lap Number'), yaxis=dict(title='Time'), uirevision = 8, modebar_add=["v1hovermode","toggleSpikelines",])
figure = go.Figure(data=traces, layout=layout)
#figure.update_traces(visible='legendonly', selector=dict(name=team_groups[groups][0]))
return figure
@app.callback(
Output('lap-position-plot', 'figure'),
[
Input('group-radiobuttons', 'value'),
Input('lap-position-updater-component', 'n_intervals'),
]
)
def update_lap_position_plot(group_name, n_intervals):
query = 'select * from position'
df = pd.read_sql_query(query, engine)
query = f"select driver_number, max(lap_number) as lap_number from telemetry group by driver_number"
df_laps = pd.read_sql_query(query, engine).set_index('driver_number', drop=True)
df=df.astype({'lap_number':int, 'driver_number':int, 'position':int})
df['driver_order'] = df.driver_number.map(driver_config['driver_code']).map(driver_config['driver_order'])
traces = []
visibility = {driver_no: True if driver_code in team_groups[group_name] else "legendonly" for driver_code, driver_no in driver_config['driver_number'].items()}
for k, v in df.sort_values(by = ['driver_order', 'lap_number']).groupby('driver_order'):
v=v.groupby('lap_number').agg('last')
x=np.arange(df_laps.loc[v.driver_number.iloc[0],'lap_number']+1)
y=[GRID[v.driver_number.iloc[0]]]
for i in range(1,len(x)-1):
if i in v.index:
y.append(v.loc[i].position)
else:
y.append(y[-1])
traces.append(go.Scatter(x=x,y=y,mode='markers+lines',name=f'{driver_config["driver_code"][v.driver_number.iloc[0]]}', line=lines[v.driver_number.iloc[0]], visible=visibility[v.driver_number.iloc[0]]))
layout = go.Layout(title = 'Positions', xaxis=dict(title='Lap Number'), yaxis=dict(title='Position'), uirevision = 8, modebar_add=["v1hovermode","toggleSpikelines",], )
fig = go.Figure(data=traces, layout=layout)
fig.update_yaxes(autorange='reversed')
fig.update_xaxes(tickmode='array', tickvals=np.arange(0,TOTAL_LAPS+1))
return fig
@app.callback(
Output('maxspeed-table', 'columns'),
[Input('maxspeed-updater-component', 'n_intervals')]
)
def update_maxspeed_columns(n_intervals):
query = f"select max(lap_number) as max_lap_number from telemetry"
df = pd.read_sql_query(query, engine)
columns = ['driver_code'] + [str(lap) for lap in range(0, 1 + df.iloc[0].max_lap_number)]
columns = [{'name': col, 'id': col} for col in columns]
# print(columns)
return columns
@app.callback(
Output('samples-table', 'columns'),
[Input('maxspeed-updater-component', 'n_intervals')]
)
def update_samples_columns(n_intervals):
query = f"select max(lap_number) as max_lap_number from telemetry"
df = pd.read_sql_query(query, engine)
columns = ['driver_code'] + [str(lap) for lap in range(0, 1 + df.iloc[0].max_lap_number)]
columns = [{'name': col, 'id': col} for col in columns]
# print(columns)
return columns
@app.callback(
Output('maxspeed-table', 'data'),
[Input('maxspeed-updater-component', 'n_intervals')]
)
def update_maxspeed_table(n_intervals):
# Replace this with your data update logic
query = f"select driver_number, lap_number, max(speed) as max_speed from telemetry group by driver_number, lap_number"
df = pd.read_sql_query(query, engine).astype(int)
df_ = df.pivot(columns = 'lap_number', index = 'driver_number', values = 'max_speed').round(0)
df_.columns = [int(x) for x in df_.columns]
df_ = df_[sorted(df_.columns.tolist())].reset_index()
df_['driver_code'] = df_.driver_number.map(driver_config['driver_code'])
df_.columns = [str(x) for x in df_.columns]
df_['driver_order'] = df_.driver_code.map(driver_config['driver_order'])
# print(df_.head(2))
# traces = []
# for k, v in df.groupby('driver_number'):
# traces.append(go.Scatter(x=v['lap_number'], y=v['lap_duration'], mode='markers+lines', name=f'{driver_config['driver_code'][int(k)]}'))
# layout = go.Layout(title = f'''Laptime Data''', xaxis=dict(title='Lap Number'), yaxis=dict(title='Time'), uirevision = 8)
# figure = go.Figure(data=traces, layout=layout)
return df_.sort_values(by = 'driver_order').to_dict('records')
@app.callback(
Output('race-control-table', 'data'),
[Input('race-control-updater-component', 'n_intervals')]
)
def update_race_control_table(n_intervals):
# Replace this with your data update logic
query = f"select * from race_control"
df = pd.read_sql_query(query, engine)[['date', 'lap_number', 'flag', 'message']]
df['date'] = pd.to_datetime(df['date'], format='mixed').dt.time
return df.sort_values(by = 'date', ascending = False).to_dict('records')
@app.callback(
Output('race-control-table', 'style_data_conditional'),
[Input('race-control-table','data'),
Input('race-control-updater-component', 'n_intervals')]
)
def update_race_control_style_data_conditional(data, n_intervals):
mapping = {'GREEN':'green', 'YELLOW':'yellow', 'RED':'red', 'DOUBLE YELLOW':"orange", 'CLEAR':'green', 'BLUE':'cyan', 'CHEQUERED':'gray'}
df = pd.DataFrame(data).fillna(0)
conditional_styles = []
for i in range(len(df)):
flag = df.flag.iloc[i]
message = df.message.iloc[i]
if any(x in message for x in ['INVESTIGATION', 'NOTED', 'PENALTY']):
conditional_styles.append({'if': {'row_index': i}, 'backgroundColor': '#3366ff', 'color': 'white'})
if flag in mapping.keys():
conditional_styles.append({'if': {'row_index': i}, 'backgroundColor': mapping[flag], 'color': 'black'})
return conditional_styles
@app.callback(
Output('maxspeed-table', 'style_data_conditional'),
[Input('maxspeed-table','data'),
Input('maxspeed-updater-component', 'n_intervals')]
)
def update_maxspeed_style_data_conditional(data, n_intervals):
def get_color(x):
if x is None:
return 0
else:
return np.clip((64 + 172 * (int(x) - 250)/100), 0, 224)
df = pd.DataFrame(data).fillna(0)
query = f"select driver_number, lap_number, max(drs) as max_drs from telemetry group by driver_number, lap_number"
df_ = pd.read_sql_query(query, engine).astype(int)
df_ = df_.pivot(columns = 'lap_number', index = 'driver_number', values = 'max_drs').round(0).fillna(0) >= 10
df_ = df_.reset_index()
df_['driver_code'] = df_.driver_number.map(driver_config['driver_code'])
df_['driver_order'] = df_.driver_code.map(driver_config['driver_order'])
df['driver_code'] = df.driver_number.map(driver_config['driver_code'])
df['driver_order'] = df.driver_code.map(driver_config['driver_order'])
df_ = df_.sort_values(by = 'driver_order')
df = df.sort_values(by = 'driver_order')
conditional_styles = []
for i in range(len(data)):
conditional_styles.append({'if': {'column_id': 'driver_code', 'row_index': i}, 'backgroundColor': driver_config['team_colour'][df.driver_code[i]], 'color': 'white'})
conditional_styles += [{'if': {'column_id': str(column), 'row_index': i}, 'border': '3px solid yellow', 'color' : 'white', 'backgroundColor': f'rgb({get_color(df[str(column)].iloc[i])}, 0, 0, 1)' }if df_[column].iloc[i] == 1 else {'if': {'column_id': str(column), 'row_index': i}, 'color' : 'white', 'backgroundColor': f'rgb({get_color(df[str(column)].iloc[i])}, 0, 0, 1'} for column in df_.columns[1:-2]]
# print(int(x) for x in df.columns[:-2])
# conditional_styles += [{'if': {'filter_query': '{%s} > 330' % column, 'column_id': column}, 'border': '5px solid red'} for column in df.columns[:-2]]
return conditional_styles
@app.callback(
Output('pitstop-table', 'columns'),
[Input('pitstop-updater-component', 'n_intervals')]
)
def update_pitstop_columns(n_intervals):
query = f"select * from laptimes"
df_laps = pd.read_sql_query(query, engine).query("is_pit_out_lap == 'True'")[['driver_number', 'lap_number']].astype(int)
merged_windows = pd.DataFrame({
'pit_stops': df_laps.groupby(['driver_number'])['lap_number'].agg(lambda x: len(x.unique())),
})
max_pits = 1 if len(merged_windows) == 0 else max(merged_windows.pit_stops)
columns = ['driver_code'] + [item for sublist in [[f'lap{i+1}', f'pit{i+1}', f'rest{i+1}'] for i in range(max_pits)] for item in sublist]
columns = [{'name': col, 'id': col} for col in columns]
return columns
@app.callback(
Output('position-table', 'style_data_conditional'),
[Input('position-table','data'),
Input('pitstop-formatting-updater-component', 'n_intervals')]
)
def update_position_style_data_conditional(data, n_intervals):
def get_color(x):
# return np.clip(127 + int(128 * x/30), 127, 255)
return np.clip((255 - 128 * x/30), 0, 255)
def get_color_2(x):
return np.clip((255 - 32 * x), 128, 255)
df = pd.DataFrame(data)
fastest_lap = df.fastest.min()
current_lap = df.lap_number.max()
conditional_styles = []
for i in range(len(data)):
if df['n'][i] > df['fastest'][i]:
conditional_styles.append({'if': {'column_id': 'n', 'row_index': i}, 'backgroundColor': '#ffcc00', 'color': 'black',})
else:
conditional_styles.append({'if': {'column_id': 'n', 'row_index': i}, 'backgroundColor': '#00cc44', 'color': 'black',})
if df['n-1'][i] > df['fastest'][i]:
conditional_styles.append({'if': {'column_id': 'n-1', 'row_index': i}, 'backgroundColor': '#ffcc00', 'color': 'black',})
else:
conditional_styles.append({'if': {'column_id': 'n-1', 'row_index': i}, 'backgroundColor': '#00cc44', 'color': 'black',})
if df['n-2'][i] > df['fastest'][i]:
conditional_styles.append({'if': {'column_id': 'n-2', 'row_index': i}, 'backgroundColor': '#ffcc00', 'color': 'black',})
else:
conditional_styles.append({'if': {'column_id': 'n-2', 'row_index': i}, 'backgroundColor': '#00cc44', 'color': 'black',})
if float(df['position'][i]) <= 3:
conditional_styles.append({'if': {'column_id': 'position', 'row_index':i}, 'backgroundColor':'rgba(128, 128, 128, 1)', 'color': 'black'})
elif float(df['position'][i]) <= 10:
conditional_styles.append({'if': {'column_id': 'position', 'row_index':i}, 'backgroundColor':'rgba(176, 176, 176, 1)', 'color': 'black'})
elif float(df['position'][i]) <= 15:
conditional_styles.append({'if': {'column_id': 'position', 'row_index':i}, 'backgroundColor':'rgba(224, 224, 224, 1)', 'color': 'black'})
color = get_color(df['lap_number'][i] - df['f_lap'][i])
color_2 = get_color_2(current_lap - df['lap_number'][i])
conditional_styles.append({'if': {'column_id': 'f_lap', 'row_index': i}, 'backgroundColor': f'''rgba({color}, {color}, {color}, 1)''', 'color': 'black'})
conditional_styles.append({'if': {'column_id': 'driver_code', 'row_index': i}, 'backgroundColor': driver_config['team_colour'][df.driver_code[i]], 'color': 'white'})
conditional_styles.append({'if': {'column_id': 'date', 'row_index': i}, 'backgroundColor': driver_config['team_colour'][df.driver_code[i]], 'color': 'white'})
conditional_styles.append({'if': {'column_id': 'lap_number', 'row_index': i}, 'backgroundColor': f'''rgba({color_2}, {color_2}, {color_2}, 1)''', 'color': 'black'})
# conditional_styles.append({'if': {'column_id': 'position', 'row_index': i}, 'backgroundColor': driver_config['team_colour'][df.driver_code[i]], 'color': 'white'})
if float(df['interval'][i]) < 1.0:
conditional_styles.append({'if': {'column_id': 'interval', 'row_index':i}, 'backgroundColor':'#00cc44', 'color': 'black'})
elif float(df['interval'][i]) > 1.0 and float(df['interval'][i]) < 2.0:
conditional_styles.append({'if': {'column_id': 'interval', 'row_index':i}, 'backgroundColor':'#C7F6C7', 'color': 'black'})
# style_data_conditional = [{'if': {'column_id': x, 'row_index': y},
# 'backgroundColor': '#3D9970','color': 'white'} for x,y in zip((df.idxmax(axis=1)), df.index)]
# if df['n-1'][i] > df['fastest'][i]:
# conditional_styles.append({'if': {'row_index': i}, 'backgroundColor': '#ffcc00', 'color': 'white',})
# else:
# conditional_styles.append({'if': {'row_index': i}, 'backgroundColor': '#00cc44', 'color': 'white',})
# if df['n-2'][i] > df['fastest'][i]:
# conditional_styles.append({'if': {'row_index': i}, 'backgroundColor': '#ffcc00', 'color': 'white',})
# else:
# conditional_styles.append({'if': {'row_index': i}, 'backgroundColor': '#00cc44', 'color': 'white',})
conditional_styles.append({'if': { 'filter_query': '{{fastest}} = {}'.format(fastest_lap), 'column_id': 'fastest'}, 'backgroundColor': '#ff66cc', 'color': 'black'})
conditional_styles.append({'if': { 'filter_query': '{{n}} = {}'.format(fastest_lap), 'column_id': 'n'}, 'backgroundColor': '#ff66cc', 'color': 'black',})
conditional_styles.append({'if': { 'filter_query': '{{n-1}} = {}'.format(fastest_lap), 'column_id': 'n-1'}, 'backgroundColor': '#ff66cc', 'color': 'black',})
conditional_styles.append({'if': { 'filter_query': '{{n-2}} = {}'.format(fastest_lap), 'column_id': 'n-2'}, 'backgroundColor': '#ff66cc', 'color': 'black',})
return conditional_styles
# df = pd.DataFrame(data)
# fastest_lap = df.fastest.min()
# return [{
# 'if' : {'filter_query': '{{fastest}} = {}'.format(fastest_lap), 'column_id': 'fastest'},
# 'backgroundColor': '#ff66cc', 'color': 'white'
# }] + [{
# 'if': { 'filter_query': '{{n}} > {{fastest}}'},
# 'backgroundColor': '#ffcc00', 'color': 'black',
# }]
# # + [{
# # 'if': { 'filter_query': '{{n}} <= {{fastest}}', 'column_id': 'n'},
# # 'backgroundColor': '#00cc44', 'color': 'black',
# # }]
# # + [{
# # 'if': { 'filter_query': '{{n-1}} > {}'.format(fastest), 'column_id': 'n-1'},
# # 'backgroundColor': '#ffcc00', 'color': 'black',
# # } for fastest in df['fastest']
# # ] + [{
# # 'if': { 'filter_query': '{{n-1}} <= {}'.format(fastest), 'column_id': 'n-1'},
# # 'backgroundColor': '#00cc44', 'color': 'black',
# # } for fastest in df['fastest']
# # ] + [{
# # 'if': { 'filter_query': '{{n-2}} > {}'.format(fastest), 'column_id': 'n-2'},
# # 'backgroundColor': '#ffcc00', 'color': 'black',
# # } for fastest in df['fastest']
# # ] + [{
# # 'if': { 'filter_query': '{{n-2}} <= {}'.format(fastest), 'column_id': 'n-2'},
# # 'backgroundColor': '#00cc44', 'color': 'black',
# # } for fastest in df['fastest']
# # ] + [{
# # 'if': { 'filter_query': '{{n}} = {}'.format(fastest_lap), 'column_id': 'n'},
# # 'backgroundColor': '#ff66cc', 'color': 'white',
# # }] + [{
# # 'if': { 'filter_query': '{{n-1}} = {}'.format(fastest_lap), 'column_id': 'n-1'},
# # 'backgroundColor': '#ff66cc', 'color': 'white',
# # }] + [{
# # 'if': { 'filter_query': '{{n-2}} = {}'.format(fastest_lap), 'column_id': 'n-2'},
# # 'backgroundColor': '#ff66cc', 'color': 'white',
# # }]
# # }] + [{
# # 'if': { 'filter_query': '{{n}} > {}'.format(fastest), 'column_id': 'n'},
# # 'backgroundColor': '#ffcc00', 'color': 'black',
# # } for fastest in df['fastest']
# # ] + [{
# # 'if': { 'filter_query': '{{n}} < {}'.format(fastest), 'column_id': 'n'},
# # 'backgroundColor': '#00cc44', 'color': 'black',
# # } for fastest in df['fastest']
# # ]
@app.callback(
Output('pitstop-table', 'style_data_conditional'),
[Input('pitstop-table','data'),
Input('pitstop-updater-component', 'n_intervals')]
)
def update_pitstop_style_data_conditional(data, n_intervals):
# def get_color(x):
# # return np.clip(127 + int(128 * x/30), 127, 255)
# return np.clip((255 - 128 * x/30), 0, 255)
df = pd.DataFrame(data)
conditional_styles = []
for i in range(len(data)):
conditional_styles.append({'if': {'column_id': 'driver_code', 'row_index': i}, 'backgroundColor': driver_config['team_colour'][df.driver_code[i]], 'color': 'white'})
# conditional_styles.append({'if': { 'filter_query': '{{fastest}} = {}'.format(fastest_lap), 'column_id': 'fastest'}, 'backgroundColor': '#ff66cc', 'color': 'black'})
# conditional_styles.append({'if': { 'filter_query': '{{n}} = {}'.format(fastest_lap), 'column_id': 'n'}, 'backgroundColor': '#ff66cc', 'color': 'black',})
# conditional_styles.append({'if': { 'filter_query': '{{n-1}} = {}'.format(fastest_lap), 'column_id': 'n-1'}, 'backgroundColor': '#ff66cc', 'color': 'black',})
# conditional_styles.append({'if': { 'filter_query': '{{n-2}} = {}'.format(fastest_lap), 'column_id': 'n-2'}, 'backgroundColor': '#ff66cc', 'color': 'black',})
return conditional_styles
# return None
@app.callback(
Output('pitstop-table', 'data'),
[Input('pitstop-updater-component', 'n_intervals')]
)
def update_pitstop_table(n_intervals):
# Replace this with your data update logic
query = f"select * from laptimes"
df_laps = pd.read_sql_query(query, engine).query("is_pit_out_lap == 'True'")[['driver_number', 'lap_number']].astype(int)
df_laps = df_laps.sort_values(by = ['driver_number', 'lap_number'])
pit_out_laps = {(v.driver_number, v.lap_number): int(v.lap_number) for k, v in df_laps.iterrows()}
pit_laps = {(v.driver_number, v.lap_number - 1): int(v.lap_number) for k, v in df_laps.iterrows()}
pit_laps.update(pit_out_laps)
if len(pit_out_laps) == 0:
return pd.DataFrame(columns = ['driver_code', 'out_lap_number', 'duration_pit', 'duration_rest']).to_dict('records')
query = f"select * from telemetry where ("
# subquery1 = ''.join([f'''(driver_number = '{k[0]}' and lap_number = '{k[1]}') or ''' for k in pit_laps.keys()] )
subquery1 = ''.join([f'''(driver_number = '{k[0]}' and lap_number in ({k[1]}, {k[1]-1})) or ''' for k in pit_out_laps.keys()])
query = query+ subquery1[:-3] + ')'
df = pd.read_sql_query(query, engine)