From 36e5d2ce5fe9b494f80c877df00c0ec72efecf36 Mon Sep 17 00:00:00 2001 From: Casper Guo Date: Tue, 20 Aug 2024 13:32:34 -0400 Subject: [PATCH] Implement callback to set up compound dropdown --- app.py | 50 ++++++++++++------- .../plotly_dash/visualization_config.toml | 1 + 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/app.py b/app.py index cf9c981..44c9811 100644 --- a/app.py +++ b/app.py @@ -79,6 +79,26 @@ def configure_lap_numbers_slider(data: dict) -> tuple[int, list[int], dict[int, return num_laps, [1, num_laps], marks +def style_compound_options(compounds: Iterable[str]) -> list[dict]: + """Create compound dropdown options with styling.""" + compound_order = ["SOFT", "MEDIUM", "HARD", "INTERMEDIATE", "WET"] + # discard unknown compounds + compounds = [compound for compound in compounds if compound in compound_order] + + # sort the compounds + compound_index = [compound_order.index(compound) for compound in compounds] + sorted_compounds = sorted(zip(compounds, compound_index), key=lambda x: x[1]) + compounds = [compound for compound, _ in sorted_compounds] + + return [ + { + "label": html.Span(compound, style={"color": COMPOUND_PALETTE[compound]}), + "value": compound, + } + for compound in compounds + ] + + app = Dash( __name__, external_stylesheets=[dbc.themes.SANDSTONE], @@ -156,23 +176,6 @@ def enable_load_session(season: int | None, event: str | None, session: str | No return not (season is not None and event is not None and session is not None) -def create_compound_dropdown_options(compounds: Iterable[str]) -> list[dict]: - """Create compound dropdown options with styling.""" - # sort the compounds - compound_order = ["SOFT", "MEDIUM", "HARD", "INTERMEDIATE", "WET"] - compound_index = [compound_order.index(compound) for compound in compounds] - sorted_compounds = sorted(zip(compounds, compound_index), key=lambda x: x[1]) - compounds = [compound for compound, _ in sorted_compounds] - - return [ - { - "label": html.Span(compound, style={"color": COMPOUND_PALETTE[compound]}), - "value": compound, - } - for compound in compounds - ] - - @callback( Output("session-info", "data"), Input("load-session", "n_clicks"), @@ -266,6 +269,19 @@ def readable_gap_col_name(col: str) -> str: ) +@callback( + Output("compounds", "options"), + Output("compounds", "disabled"), + Input("laps", "data"), + prevent_initial_call=True, +) +def set_compounds_dropdown(data: dict) -> tuple[list[dict], bool]: + """Update compound plot dropdown options based on the laps dataframe.""" + # exploit how Pandas dataframes are converted to dictionaries + # avoid having to construct a new dataframe + return style_compound_options(set(data["Compound"].values())), False + + @callback( Output("laps", "data", allow_duplicate=True), Input("add-gap", "n_clicks"), diff --git a/f1_visualization/plotly_dash/visualization_config.toml b/f1_visualization/plotly_dash/visualization_config.toml index 9af0be8..ecac150 100644 --- a/f1_visualization/plotly_dash/visualization_config.toml +++ b/f1_visualization/plotly_dash/visualization_config.toml @@ -35,6 +35,7 @@ UNKNOWN = "#00ffff" # For display on a white background # the customary palette is kept unchanged where the contrast is sufficiently high +# TODO: improve this palette [relative.high_contrast_palette] SOFT = "#da291c" MEDIUM = "8a6d00"