St.fragment with session_states

Hello,
I am trying to avoid a full rerun of an app every time the radio button and/or st.select is activated. I decided to create two functions with st.fragment decorators (see sample code below). so basically, if under Col3 st.radio==‘custom’ then the dropdown will show up under column2, but if st.radio==‘Other’, the content under col2 will be empty.
Is there a way to achieve this with st.fragment or another method that will not require a full app re-run ?
Thanks

import streamlit as st
import pandas as pd
from datetime import datetime, timedelta


df=pd.DataFrame({'a': ['x','y','z','t'],'b':['x1','y1','z1','t1']})


#--- defining some functions
@st.fragment
def radio_pick():
    
    if( st.radio('levels', options=['custom','Other'],key='level',index=1) ):
        st.write('Value widget inside function: ', f':blue[**{st.session_state.level}**]')
        #st.rerun(scope='app')
    
    


@st.fragment
def drop_down():
    if  st.session_state.level=='custom':
        st.selectbox('pick option1',options=df.a.unique(), index=None,key='option1')
        if(not isinstance(st.session_state.option1, type(None)) ):
            st.selectbox('Pick option2',options=df.b.unique(),index=None,key='option2')
    return
#--------------------------
col1,col2,col3=st.columns(3)

with col1:
    st.write('Col1')
    st.date_input('pick a date', (datetime.today()-timedelta(days=90),datetime.today()),format='YYYY-MM-DD')

with col3:
    st.write('**Col3**')
    radio_pick()
    st.write('Value of Radio button outside: ', f':orange[{st.session_state.level}]')
    

with col2:
    st.write("**Col2**")
    drop_down()

A quick solution - add these elements inside a form.
Link - st.form - Streamlit Docs

Forms prevents re-load when input elements are being changed. This a simple and elegant approach here.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.