Skip to content

Commit

Permalink
Implement callback to set up compound dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper-Guo committed Aug 20, 2024
1 parent 5ca9941 commit 36e5d2c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
50 changes: 33 additions & 17 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions f1_visualization/plotly_dash/visualization_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 36e5d2c

Please sign in to comment.