From 52f2d03cd1b13535c146d1639db92d57cdb54848 Mon Sep 17 00:00:00 2001 From: Elijas <4084885+Elijas@users.noreply.github.com> Date: Fri, 22 Dec 2023 13:58:43 +0200 Subject: [PATCH] fix(dashboard): unselecting all example chips resulted in the first chip being selected --- dev_utils/dashboard_app/app.py | 3 +- dev_utils/dashboard_app/constants.py | 1 + dev_utils/dashboard_app/select_reports.py | 54 ++++++++++++++++------ dev_utils/dashboard_app/streamlit_utils.py | 24 +++++++--- 4 files changed, 60 insertions(+), 22 deletions(-) diff --git a/dev_utils/dashboard_app/app.py b/dev_utils/dashboard_app/app.py index 8ed2704..6958822 100644 --- a/dev_utils/dashboard_app/app.py +++ b/dev_utils/dashboard_app/app.py @@ -1,4 +1,3 @@ -import contextlib from enum import Enum, auto from urllib.parse import urlencode @@ -64,7 +63,7 @@ st.session_state["select_reports__queries"] = default if "select_reports__example_queries" not in st.session_state: - default = [0, 2] + default = [1, 3] if url_params_queries: default = [ i for i, k in enumerate(example_queries_items) if k[1] in url_params_queries diff --git a/dev_utils/dashboard_app/constants.py b/dev_utils/dashboard_app/constants.py index 0b2e2de..ae08d0d 100644 --- a/dev_utils/dashboard_app/constants.py +++ b/dev_utils/dashboard_app/constants.py @@ -1,6 +1,7 @@ import streamlit_antd_components as sac example_queries_items = [ + (sac.ChipItem(label="Unselect All"), None), (sac.ChipItem(icon="apple", label="Latest apple 10-Q"), "AAPL"), (sac.ChipItem(icon="google", label="Two Latest alphabet 10-Q"), "2/GOOG"), ( diff --git a/dev_utils/dashboard_app/select_reports.py b/dev_utils/dashboard_app/select_reports.py index 4738b1c..af23ee6 100644 --- a/dev_utils/dashboard_app/select_reports.py +++ b/dev_utils/dashboard_app/select_reports.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from urllib.parse import urlencode import streamlit as st @@ -20,27 +22,51 @@ def render_select_reports(): """, unsafe_allow_html=True, ) + + class OnChangeHandler: + def __init__(self, key): + self.key = key + + def __call__(self, value): + if isinstance(value, list): + if len(value) >= 2 and 0 in value and value[0] != 0: + return [0] # [v for v in value if v != 0] + if len(value) >= 2 and value[0] == 0: + return [v for v in value if v != 0] + elif len(value) == 0: + return [0] + return value + + handler = OnChangeHandler("actual") st_utils.st_unkeep("select_reports__example_queries") - selected_example_queries = sac.chip( - items=[k[0] for k in example_queries_items], - label="Get started with examples:", - size="xs", - format_func="title", - align="start", - index=st.session_state.select_reports__example_queries, - variant="outline", - multiple=True, - return_index=True, - key="_select_reports__example_queries", - on_change=lambda: st_utils.st_keep("select_reports__example_queries"), + selected_example_queries = handler( + sac.chip( + items=[k[0] for k in example_queries_items], + label="Get started with examples:", + size="xs", + format_func="title", + align="start", + index=st.session_state.select_reports__example_queries, + variant="outline", + multiple=True, + return_index=True, + key="_select_reports__example_queries", + on_change=lambda: st_utils.st_keep( + "select_reports__example_queries", + handler, + ), + ), ) - avoidlist = [ k[1] for i, k in enumerate(example_queries_items) if i not in selected_example_queries ] - addlist = [example_queries_items[i][1] for i in selected_example_queries] + addlist = [ + example_queries_items[i][1] + for i in selected_example_queries + if example_queries_items[i][1] + ] current = [ k diff --git a/dev_utils/dashboard_app/streamlit_utils.py b/dev_utils/dashboard_app/streamlit_utils.py index 424c4ae..53fa86b 100644 --- a/dev_utils/dashboard_app/streamlit_utils.py +++ b/dev_utils/dashboard_app/streamlit_utils.py @@ -135,13 +135,25 @@ def get_style_change_top_page_margin(): return "div[class^='block-container'] { padding-top: 2rem; }".strip() -def st_keep(key): - "https://stackoverflow.com/questions/74968179/session-state-is-reset-in-streamlit-multipage-app" +def st_keep(key, value_func=None) -> None: + """ + Keep a widget's value in session state. + https://stackoverflow.com/questions/74968179/session-state-is-reset-in-streamlit-multipage-app + """ # Copy from temporary widget key to permanent key - st.session_state[key] = st.session_state["_" + key] + new_value = st.session_state["_" + key] + if value_func: + new_value = value_func(new_value) + st.session_state[key] = new_value -def st_unkeep(key): - "https://stackoverflow.com/questions/74968179/session-state-is-reset-in-streamlit-multipage-app" +def st_unkeep(key, value_func=None) -> None: + """ + Recall a widget's value from session state. + https://stackoverflow.com/questions/74968179/session-state-is-reset-in-streamlit-multipage-app + """ # Copy from permanent key to temporary widget key - st.session_state["_" + key] = st.session_state[key] + new_value = st.session_state[key] + if value_func: + new_value = value_func(new_value) + st.session_state["_" + key] = new_value