Multiselect only updating for every second input

When choosing from the multiselectbox below, I find that I have to click the options twice in order for it to register in the streamlit application.

By default the select shows the years 2017-2019.
If I then click ‘2016’ it shows 2016-2019, correctly.
If I then click ‘2015’ it show 2016-2019 once again.
If I then click ‘2015’ once again, it shows 2015-2019, which is correct.

I want the multiselect to default to the latest y1-input always, since the application has multiple pages, which the user navigates in between.

code:
session_state = get(y1=['2019','2018','2017'])

session_state.y1 = st.multiselect("Select year(s)['2013','2014','2015','2016','2017','2018','2019'],session_state.y1)

Can anyone help me figure a solution out?

Hey @Pbennekou, welcome to Streamlit!

I assume you’re using the SessionState gist?

This is some bugginess around the SessionState implementation, and how it interacts with Streamlit’s rerun logic. As a workaround, you can do something like this:

import streamlit as st
from streamlit.ScriptRequestQueue import RerunData
from streamlit.ScriptRunner import RerunException

last_y1 = session_state.y1
session_state.y1 = st.multiselect(
    "Select year(s)",
    ['2013', '2014', '2015', '2016', '2017', '2018', '2019'],
    session_state.y1)

if last_y1 != session_state.y1:
    raise RerunException(RerunData(widget_state=None))

That RerunException will force Streamlit to re-execute your script whenever the session_state is updated.

(This is a temporary solution; we intend to “officially” implement SessionState in an upcoming release.)

2 Likes

Thank you Tim, that worked out exactly as intended!
I will look forward to the “official” implementation of SessionState in the future, but this will do for now :slight_smile:

I seem to have the same problem with a text area. The solution proposed here does not work for me, I guess as I am doing other things with “session” variables here as well.

My current solution is that I present users the message that their next edit will be ignored, which works for a while but is not a really viable route for the future.

Just FYI to make clear that it does not only affect Multiselect widgets.

Keep on the fantastic work :slight_smile:

1 Like