What I’m trying to do is to update the table whenever users change category or count. (data from mongodb)
so for multiselect, I can choose categories. for slider, I can select amount of something.
the problem is I’m not sure how to use the values of these widgets on on_change callback.
and the weird thing is, multiselect updates its value on call while slider never update its value in this way.
seems like the codes are running again from top to bottom whenever I change the values like more than one?
and sometimes callback function updates by last value (default=10, change to 5, the value is still 10, after changing the value again, it’s now 5)
st.title("title")
my_table = st.empty()
def update_notice():
global my_table
global categories
my_table.empty()
filter = {}
try: # whenever I change category, it is changed too
if categories:
filter = {"$or": [{"category": category} for category in categories]}
except NameError:
pass
sort = list({"id": -1}.items())
# I tried same method like categories for count, but the count value never changes
result = tuple(
client["db"]["collection"].find(filter=filter, sort=sort, limit=count)
)
# print(result)
data = {
"title": tuple(article["title"] for article in result),
}
df = pd.DataFrame(data)
df.index = [f"{i}th" for i in range(1, len(result) + 1)]
my_table.table(df)
categories = st.multiselect(
"label",
categories, # like ["a", "b"...]
[],
on_change=update_notice(), # I should pass the function not return value, but the table disappears when I use lambda: f() or f...
help="help",
)
count = st.slider(
"label",
min_value=1,
max_value=100,
value=10,
on_change=update_notice(), # I should pass the function not return value, but the table disappears...
help="help",
)
# print(count), if I print it in here, it prints updated value..
Any better idea to load global widgets in update callback fuctions and get the current values?
I used “st.session_state.my_key” to get the value but wanna know better approach as it updates several times when I click something