Use st.selectbox if submit_button = True

In my code, if the user clicks on submitting a form, a selectbox is displayed where each selected option will have a different destination. It happens that when selecting an option the page is reloaded automatically. How to make the streamlit validate the selected option?

verificador=pgadmin_connect.consultar_db_by_telefone(input_name)

if input_button_submit:
    
    if verificador !=None:
        opcoes=['','Escolher','Nada']
        for item in pgadmin_connect.consultar_db_by_telefone(input_name):
            st.write('O nome {} já existe. Favor confirmar o telefone:'.format(input_name))
            col1, col2, col3=st.columns((1,1.1,1.2))
            col1.write(item.nome)
            col2.write(item.telefone)
            #select_space=col3.empty()
            select_space=st.selectbox('Escolha uma opção',options=opcoes)
            
            if select_space=='Escolher':
                #Chamar o atualizar e passar o id antigo
                pgadmin_connect.editar_db_recorrente(item.id)
                st.write('Editado com sucesso! {}'.format(item.telefone))
                st.write('Editado com sucesso! {}'.format(item.telefone))
                st.write('Editado com sucesso! {}'.format(item.telefone)) 
                st.form_submit_button('Enviar')
                st.experimental_rerun()
                
            elif select_space=='Nada':
                
                vs.nome=input_name
                #vs.sobrenome=input_lastname
                vs.sexo=input_sexo
                vs.idade=input_age
                if input_recorrente == 'Sim':
                    vs.recorrente='Visitante novo'
                else:
                    vs.recorrente='Recorrente'
                    vs.observacao=input_observacao
                    vs.endereco=input_adress
                    vs.data=datetime.date.today()
                        
                    if input_phone !=(27):    
                        vs.telefone=input_phone
                    else:
                        input_phone=None
                        vs.telefone=input_phone
                        pgadmin_connect.inserir_db(vs)     
                        st.experimental_rerun()    
            
            
            
    if visitanteRecuperado==None:    

            vs.nome=input_name
            #vs.sobrenome=input_lastname
            vs.sexo=input_sexo
            vs.idade=input_age
            if input_recorrente == 'Sim':
                vs.recorrente='Visitante novo'
            else:
                vs.recorrente='Recorrente'
            vs.observacao=input_observacao
            vs.endereco=input_adress
            vs.data=datetime.date.today()
            
            if input_phone !=(27):    
                vs.telefone=input_phone
            else:
                input_phone=None
                vs.telefone=input_phone

Buttons aren’t stateful. They return True on the page load resulting from their click, and then go back to False. If you put a widget nested inside a button, then that widget will always disappear when you interact with it (the page reloads with the button being False, hence no nested widget). See the first point in this blog post.

The most common suggestion I give is to create a callback and assign it to the button so you can keep track of the fact that the button was pressed.

import streamlit as st

if 'submitted' not in st.session_state:
    st.session_state.submitted = False

def submitted():
    st.session_state.submitted = True

st.button('Submit', callback=submitted)

if st.session_state.submitted:
    # Add widgets and content here
    st.write('Lorem ipsum')

Thank you very much for your help, just to finish, could you give an example of this call but coming from a form submit button?

if input_button_submit:

There’s really no difference. Instead of st.button('Submit', callback=submitted) you have your form and within it you have st.form_submit_button('Submit', callback=submitted) instead.

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