question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

selectbox does not play well with session state persistence

See original GitHub issue

Summary

When using selectbox together with session state persistence code found at https://gist.github.com/tvst/036da038ab3e999a64497f42de966a92, I need to select the same item two times in row for it to be actually selected. The first time I select it, the selectbox automatically restores the previous value.

Steps to reproduce

Code snippet:

import SessionState
import streamlit as st


options = ["option 1", "option 2"]
session_state = SessionState.get(option='-')
try:
    index = options.index(session_state.option)
except ValueError:
    index = 0
session_state.option = st.sidebar.selectbox('Options', options, index=index)
  1. At startup, the select box displays “option 1”.
  2. Select “option 2”, it works, the first selection after startup always works.
  3. Select “option 1”, the select box displays “option 1” during a very short period of time and then restores the select box content to “option 2”.
  4. Select “option 1” again, it works.

Expected behavior:

I expect step 3 to provide “option 1”

Actual behavior:

Step 3 provides “option 2”.

Is this a regression?

That is, did this use to work the way you expected in the past? I don’t know. I observed the same behavior in Streamlit version 0.81.1.

Debug info

  • Streamlit version: version 0.82.0
  • Python version: Python 3.8.9
  • pyenv 1.2.27
  • OS version: macOS Big Sur 11.3.1
  • Browser version: Firefox 88.0.1 / Safari 14.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
AnOctopuscommented, Jul 22, 2021

@vincbo

With the recent release of session state in core streamlit, the snippet you provided can now be written in a cleaner and more concise way that does not have this issue. Since widgets are integrated into the session state api, there is no longer a need for manually syncing the widget and session state values. This snippet works and I think does what the original wanted to do:

import streamlit as st

state = st.session_state


options = ["option 1", "option 2"]
st.sidebar.selectbox("Options", options, key="option")
st.write(state.option)  # to show us retrieving the value from state

The selected value resetting used to be caused by the changing index argument giving the widget a new identity, but now we do it deliberately to facilitate live-editing scripts. I’m starting a discussion internally to evaluate that use case versus this being a potential footgun. The way to make it work correctly is to only ever set a value or index to a constant, and use the session state api to get/set the widget value instead of trying to manually sync them.

0reactions
kmcgradycommented, Aug 5, 2021

Going to close this issue with a new, preferred, solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

State value doesn't seem to persist for a Select element ...
Hi I have a very simple example (in Next JS) that doesn't seem to work, where I have created a basic store: interface...
Read more >
How to Maintain Persistence State of a Dropdown with ...
But for the drop-down I happen to be using, I cannot get it to persist. Well, I can, but then the default selection...
Read more >
Multi-page app with session state - Streamlit
What I am trying to do is to not have state.input depend on state.selectbox if text is written into the text_input. If a...
Read more >
Session State for Streamlit - Towards Data Science
But with Session State, it's possible to have values persist across reruns for those instances when you don't want your variables reinitialized.
Read more >
DynamicModule persistent UI changes - Mathematica Stack ...
Lastly, be sure the checkbox is on, and close the notebook. Note that there's no external or internal clue about the notebook saved...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found