SessionState, selectbox, avoid rerun

Dear all,

I got inspired by this post python 3.x - Script rerun when i change selectbox in Streamlit - Stack Overflow , trying to do the following:

I have 2 selectbox located in 2 different columns.
Once I click on ‘Calculate’, the selectbox in column 1 gets overriden by a certain value.
But I would like my script not to reset this value (rerun) and replace it by a new selectbox, when I use the other selectbox located in column 2 …

I didn’t manage to avoid the streamlit rerun when using the second selectbox after I clicked on ‘Calculate’. I’m having hard time using SessionState.

This is my code:

import streamlit as st
import pandas as pd
import SessionState

df = pd.DataFrame({‘name’: [‘steve’, ‘joe’, ‘paul’], ‘position’: [1, 2, 3]})

with st.sidebar.form(“my_form”):
calculate = st.form_submit_button(‘Calculate’)

session_state = SessionState.get(col1=False, col2=False)

col1, col2 = st.columns(2)

placeholder =
placeholder = col1.empty()

col1_one = placeholder.selectbox(‘box1’, df[‘name’].unique())
col2_one = col2.selectbox(‘box2’, df[‘name’].unique())

if col1_one or session_state.col1:
session_state.col1 = True
session_state.col2 = False

if col2_one or session_state.col2:
session_state.col1 = False
session_state.col2 = True

if calculate:
st.sidebar.write(‘Algo is running’)
placeholder.write(‘overriden value’)
session_state.col1 = False
session_state.col2 = True

Any thoughts about this ?
SessionState is really a great tool, but not easy to handle.

1 Like