Updating slidebar widget after initialization without a widget call-back

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).

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)
        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':
        st.session_state['fields_count'] += 1 # Increment slider by 1 <--- Problem: It does not update the slider.

Using callbacks with selectboxes is no differet from using them with buttons.

Thanks a lot for the reply. I might missing something but I understand your point.

In the case of st.selectbox, we have multiple options and in my case I want to trigger the callback conditionally and hence the if condition.

In the case of st.button, it’s just one trigger/condition which is a triggered once clicked.

According to documentation it says:

on_change (callable): An optional callback invoked when this selectbox’s value changes.

You cannot do that. The callback is always triggered, then in the callback you can conditionally do something.

1 Like

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