diff --git a/my_project/tab_data_explorer/app_data_explorer.py b/my_project/tab_data_explorer/app_data_explorer.py index 58e19c1..e0a5c2f 100644 --- a/my_project/tab_data_explorer/app_data_explorer.py +++ b/my_project/tab_data_explorer/app_data_explorer.py @@ -13,6 +13,7 @@ code_timer, title_with_link, determine_month_and_hour_filter, + dropdown, ) from my_project.global_scheme import ( @@ -56,14 +57,10 @@ def section_one_inputs(): className="container-row full-width row-center", children=[ html.H4(className="text-next-to-input", children=["Select a variable: "]), - dcc.Dropdown( + dropdown( id="sec1-var-dropdown", - options=[ - {"label": i, "value": explore_dropdown_names[i]} - for i in explore_dropdown_names - ], + options=explore_dropdown_names, value="DBT", - clearable=False, ), ], ) @@ -225,18 +222,11 @@ def section_two_inputs(): children=["Variable:"], style={"flex": "30%"}, ), - dcc.Dropdown( + dropdown( id="sec2-var-dropdown", - options=[ - { - "label": i, - "value": explore_dropdown_names[i], - } - for i in explore_dropdown_names - ], + options=explore_dropdown_names, value="RH", style={"flex": "70%"}, - clearable=False, ), ], ), @@ -333,18 +323,11 @@ def section_two_inputs(): children=["Filter Variable:"], style={"flex": "30%"}, ), - dcc.Dropdown( + dropdown( id="sec2-data-filter-var", - options=[ - { - "label": i, - "value": explore_dropdown_names[i], - } - for i in explore_dropdown_names - ], + options=explore_dropdown_names, value="RH", style={"flex": "70%"}, - clearable=False, ), ], ), @@ -430,15 +413,11 @@ def section_three_inputs(): className=container_row_center_full, children=[ html.H6(style={"flex": "30%"}, children=["X Variable:"]), - dcc.Dropdown( + dropdown( id="tab6-sec3-var-x-dropdown", - options=[ - {"label": i, "value": explore_dropdown_names[i]} - for i in explore_dropdown_names - ], + options=explore_dropdown_names, value="DBT", style={"flex": "70%"}, - clearable=False, ), ], ), @@ -446,15 +425,11 @@ def section_three_inputs(): className=container_row_center_full, children=[ html.H6(style={"flex": "30%"}, children=["Y Variable:"]), - dcc.Dropdown( + dropdown( id="tab6-sec3-var-y-dropdown", - options=[ - {"label": i, "value": explore_dropdown_names[i]} - for i in explore_dropdown_names - ], + options=explore_dropdown_names, value="RH", style={"flex": "70%"}, - clearable=False, ), ], ), @@ -462,15 +437,11 @@ def section_three_inputs(): className=container_row_center_full, children=[ html.H6(style={"flex": "30%"}, children=["Color By:"]), - dcc.Dropdown( + dropdown( id="tab6-sec3-colorby-dropdown", - options=[ - {"label": i, "value": explore_dropdown_names[i]} - for i in explore_dropdown_names - ], + options=explore_dropdown_names, value="glob_hor_rad", style={"flex": "70%"}, - clearable=False, ), ], ), @@ -564,15 +535,11 @@ def section_three_inputs(): html.H6( children=["Filter Variable:"], style={"flex": "30%"} ), - dcc.Dropdown( + dropdown( id="tab6-sec3-filter-var-dropdown", - options=[ - {"label": i, "value": explore_dropdown_names[i]} - for i in explore_dropdown_names - ], + options=explore_dropdown_names, value="RH", style={"flex": "70%"}, - clearable=False, ), ], ), diff --git a/my_project/tab_outdoor_comfort/app_outdoor_comfort.py b/my_project/tab_outdoor_comfort/app_outdoor_comfort.py index 11048cd..aab6edf 100644 --- a/my_project/tab_outdoor_comfort/app_outdoor_comfort.py +++ b/my_project/tab_outdoor_comfort/app_outdoor_comfort.py @@ -8,6 +8,7 @@ container_row_center_full, container_col_center_one_of_three, ) +from my_project.utils import dropdown from dash.dependencies import Input, Output, State from my_project.template_graphs import ( @@ -42,20 +43,11 @@ def inputs_outdoor_comfort(): children=["Select a scenario:"], style={"flex": "30%"}, ), - dcc.Dropdown( + dropdown( id="tab7-dropdown", - style={ - "flex": "60%", - }, - options=[ - { - "label": i, - "value": outdoor_dropdown_names[i], - } - for i in outdoor_dropdown_names - ], + style={"flex": "60%"}, + options=outdoor_dropdown_names, value="utci_Sun_Wind", - clearable=False, ), html.Div(id="image-selection", style={"flex": "10%"}), ], diff --git a/my_project/tab_psy_chart/app_psy_chart.py b/my_project/tab_psy_chart/app_psy_chart.py index 453df83..f3a5b56 100644 --- a/my_project/tab_psy_chart/app_psy_chart.py +++ b/my_project/tab_psy_chart/app_psy_chart.py @@ -18,6 +18,7 @@ generate_custom_inputs_psy, determine_month_and_hour_filter, title_with_link, + dropdown, ) from my_project.global_scheme import ( dropdown_names, @@ -64,17 +65,13 @@ def inputs(): children=["Color By:"], style={"flex": "30%"}, ), - dcc.Dropdown( + dropdown( id="psy-color-by-dropdown", - options=[ - {"label": i, "value": psy_dropdown_names[i]} - for i in psy_dropdown_names - ], + options=psy_dropdown_names, value="Frequency", style={"flex": "70%"}, persistence_type="session", persistence=True, - clearable=False, ), ], ), @@ -168,15 +165,11 @@ def inputs(): html.H6( children=["Filter Variable:"], style={"flex": "30%"} ), - dcc.Dropdown( + dropdown( id="psy-var-dropdown", - options=[ - {"label": i, "value": dropdown_names[i]} - for i in dropdown_names - ], + options=dropdown_names, value="RH", style={"flex": "70%"}, - clearable=False, ), ], ), diff --git a/my_project/tab_sun/app_sun.py b/my_project/tab_sun/app_sun.py index c320f3f..cc63955 100644 --- a/my_project/tab_sun/app_sun.py +++ b/my_project/tab_sun/app_sun.py @@ -12,6 +12,7 @@ month_lst, mapping_dictionary, ) +from my_project.utils import dropdown from dash.dependencies import Input, Output, State from my_project.tab_sun.charts_sun import ( @@ -68,15 +69,14 @@ def sun_path(): children=["View: "], style={"width": "10rem"}, ), - dcc.Dropdown( + dropdown( id="custom-sun-view-dropdown", - options=[ - {"label": "Spherical", "value": "polar"}, - {"label": "Cartesian", "value": "cartesian"}, - ], + options={ + "Spherical": "polar", + "Cartesian": "cartesian", + }, value="polar", style={"width": "10rem"}, - clearable=False, ), ], ), @@ -89,16 +89,11 @@ def sun_path(): children=["Select variable: "], style={"width": "10rem"}, ), - dcc.Dropdown( + dropdown( id="custom-sun-var-dropdown", - options=[ - {"label": i, "value": sc_dropdown_names[i]} - for i in sc_dropdown_names - if i != "Frequency" - ], + options=sc_dropdown_names, value="None", style={"width": "20rem"}, - clearable=False, ), ], ), @@ -132,18 +127,11 @@ def explore_daily_heatmap(): children=["Select variable: "], style={"width": "10rem"}, ), - dcc.Dropdown( + dropdown( id="tab4-explore-dropdown", - options=[ - { - "label": i, - "value": sun_cloud_tab_explore_dropdown_names[i], - } - for i in sun_cloud_tab_explore_dropdown_names - ], + options=sun_cloud_tab_explore_dropdown_names, value="glob_hor_rad", style={"width": "20rem"}, - clearable=False, ), ], ), diff --git a/my_project/tab_t_rh/app_t_rh.py b/my_project/tab_t_rh/app_t_rh.py index 477941b..cf68c5d 100644 --- a/my_project/tab_t_rh/app_t_rh.py +++ b/my_project/tab_t_rh/app_t_rh.py @@ -7,6 +7,7 @@ title_with_tooltip, summary_table_tmp_rh_tab, title_with_link, + dropdown, ) from my_project.template_graphs import heatmap, yearly_profile, daily_profile from my_project.global_scheme import dropdown_names @@ -28,18 +29,11 @@ def layout_t_rh(): html.H4( className="text-next-to-input", children=["Select a variable: "] ), - dcc.Dropdown( + dropdown( id="dropdown", className="dropdown-t-rh", - options=[ - { - "label": var, - "value": dropdown_names[var], - } - for var in var_to_plot - ], + options={var: dropdown_names[var] for var in var_to_plot}, value=dropdown_names[var_to_plot[0]], - clearable=False, ), ], ), diff --git a/my_project/tab_wind/app_wind.py b/my_project/tab_wind/app_wind.py index f81d47b..be93710 100644 --- a/my_project/tab_wind/app_wind.py +++ b/my_project/tab_wind/app_wind.py @@ -8,6 +8,7 @@ generate_units, generate_custom_inputs_time, title_with_link, + dropdown, ) from my_project.utils import code_timer @@ -233,15 +234,13 @@ def custom_wind_rose(): style={"width": "8rem"}, children=["Start Month:"], ), - dcc.Dropdown( + dropdown( id="tab5-custom-start-month", - options=[ - {"label": j, "value": i + 1} - for i, j in enumerate(month_lst) - ], + options={ + j: i + 1 for i, j in enumerate(month_lst) + }, value=1, style={"width": "6rem"}, - clearable=False, ), ], ), @@ -252,15 +251,13 @@ def custom_wind_rose(): style={"width": "8rem"}, children=["Start Hour:"], ), - dcc.Dropdown( + dropdown( id="tab5-custom-start-hour", - options=[ - {"label": str(i) + ":00", "value": i} - for i in range(1, 25) - ], + options={ + str(i) + ":00": i for i in range(1, 25) + }, value=1, style={"width": "6rem"}, - clearable=False, ), ], ), @@ -276,15 +273,13 @@ def custom_wind_rose(): style={"width": "8rem"}, children=["End Month:"], ), - dcc.Dropdown( + dropdown( id="tab5-custom-end-month", - options=[ - {"label": j, "value": i + 1} - for i, j in enumerate(month_lst) - ], + options={ + j: i + 1 for i, j in enumerate(month_lst) + }, value=12, style={"width": "6rem"}, - clearable=False, ), ], ), @@ -295,15 +290,13 @@ def custom_wind_rose(): style={"width": "8rem"}, children=["End Hour:"], ), - dcc.Dropdown( + dropdown( id="tab5-custom-end-hour", - options=[ - {"label": str(i) + ":00", "value": i} - for i in range(1, 25) - ], + options={ + str(i) + ":00": i for i in range(1, 25) + }, value=24, style={"width": "6rem"}, - clearable=False, ), ], ), diff --git a/my_project/utils.py b/my_project/utils.py index a839203..03f8909 100644 --- a/my_project/utils.py +++ b/my_project/utils.py @@ -4,7 +4,7 @@ import pandas as pd import json from pandas import json_normalize -from dash import html, dash_table +from dash import html, dash_table, dcc import dash_bootstrap_components as dbc import plotly.express as px import copy @@ -373,3 +373,16 @@ def determine_month_and_hour_filter(month, hour, invert_month, invert_hour): end_hour, start_hour = hour return start_month, end_month, start_hour, end_hour + + +def dropdown(options={}, **kwargs): + """ + Wrapper for dcc.Dropdown which + - makes "clearable=False" the default, so we don't need to handle None + - accepts dicts, and preserves order. + """ + return dcc.Dropdown( + options=[{"label": k, "value": v} for k, v in options.items()], + clearable=False, + **kwargs, + )