Skip to content

Commit

Permalink
fix(dashboard): unselecting all example chips resulted in the first c…
Browse files Browse the repository at this point in the history
…hip being selected
  • Loading branch information
Elijas committed Dec 22, 2023
1 parent 6eacb19 commit 52f2d03
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
3 changes: 1 addition & 2 deletions dev_utils/dashboard_app/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import contextlib
from enum import Enum, auto
from urllib.parse import urlencode

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions dev_utils/dashboard_app/constants.py
Original file line number Diff line number Diff line change
@@ -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"),
(
Expand Down
54 changes: 40 additions & 14 deletions dev_utils/dashboard_app/select_reports.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from urllib.parse import urlencode

import streamlit as st
Expand All @@ -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
Expand Down
24 changes: 18 additions & 6 deletions dev_utils/dashboard_app/streamlit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 52f2d03

Please sign in to comment.