Context:
I have a slider initialized and a function that creates n fields where n is the loop range taken from slider.
A field is composed of a text field (column 1), a text input (column 2) and a list to choose two options from (column 3).
Issue:
I would like to update the slider value conditionally if the ‘Option B’ is selected from the list.
Examples and documentations only explain such attempts using callbacks that call a function inside a button for example which is not my case.
import streamlit as st
def generate_field(key, field:str=None, option:str='', value:str=None):
c1, c2, c3 = st.columns([1, 1, 2], gap="small")
with c1: # Field title (i.e. Field 1)
c1.markdown("#")
st.write(f'Field {key}')
with c2: # Field text input (df column name)
field_input = st.text_input(label=f'Enter field {key} name',
value = field if field != None else '', # In case of generating a ready-made template, field name 'field' is passed to function from Template class.
key = f'row_{key}_c2'
)
with c3:
lookup_dict = {'':'','Option A':'some_func', 'Option B':'some_func'}
list_options = list(lookup_dict.keys()) # A list of functions options
choice = st.selectbox(label='Choose data type',
options = list_options,
key = f'row_{key}_c3'
)
return field_input, choice
# Initialize the slider value in session state
if "fields_count" not in st.session_state:
st.session_state["fields_count"] = 0
with st.form('init_rows_form'): # Init form - with user input rows
st.session_state['fields_count'] = st.slider(label='**Select number of fields**', min_value=1, max_value=5, key='c1_fields')
if st.form_submit_button("Select"):
st.session_state['step'] = 1
for i in range(1,st.session_state['fields_count']+1): # A foundational loop that re-draws row (generate_field () UI Component)
inputs = generate_field('{}'.format(i))
if inputs[1] == 'Option B':
i+=1
generate_field('{}'.format(i))
st.session_state['fields_count'] += 1 # Increment slider by 1 <--- Problem: It does not update the slider.