Hi, I am creating a support web app to help the support team at my company, at the moment is running locally.
Is a simple application, will receive a number and execute a ‘select’ on database and show to user. After this, I want a second button be enabled when the query is shown on the screen.
So is a button to consult and other (to delete a line in db). But I can’t make it work, any help is welcome I’m open to advice.
In below a simple example, a part of the full code.
I realize that the delete button ( btns[1] ) only activates when I click Two times on the first button.
[up there is the conection with database]
# start state
if 'botao_ativo' not in st.session_state:
st.session_state.botao_ativo = False
# if 'resultados_exibidos' not in st.session_state:
# st.session_state.resultados_exibidos = False
# Title
st.title('Consulta Imagem GED')
# Caixa de texto para o campo EMPRESA DO CTE
emp = st.text_input('Digite a EMPRESA DO CTE:', placeholder= "Qualquer empresa do Sistema Senior")
# Caixa de texto para o campo NUM
num = st.text_input('Digite o NUMERO DO CTE:', placeholder="EX: '4665656' ou '4665656, 4665657'")
# Botoes para Consultar e deletar
btns = st.columns((3,3,7))
with btns[0]:
consultar_bt = st.button('Consultar')
with btns[1]:
deletar_bt = st.button("Deletar", key="del", type="primary", disabled=not st.session_state.botao_ativo)
# Botão para iniciar a consulta
if consultar_bt:
# verifica se o campo empresa está preenchido
if emp == "" and num != "":
# Separa itens por vírgula
lista_num = num.split(",")
resultados = []
# Verifica se possui apenas 1 número e executa a consulta se sim
if len(lista_num) == 1:
resultado = consult_ged_emp(lista_num[0])
resultados.extend(resultado)
else:
# Se tiver mais de 1 número
for num_cte in lista_num:
resultado = consult_ged_emp(num_cte)
resultados.extend(resultado)
# Show Results to user
if resultados:
st.markdown('<h2>Resultado da consulta:</h2>', unsafe_allow_html=True)
for resultado in resultados:
#st.write(resultado)
st.markdown(f"""
<div style="background-color: #708090; padding: 10px; margin: 10px 0; border-radius: 5px; color: #000;">
<strong>Empresa:</strong> {resultado["empresa"]}<br>
<strong>Usuário:</strong> {resultado["usuario"]}<br>
<strong>Data e Arquivo:</strong> {resultado["data_arq"]}
</div>
""", unsafe_allow_html=True)
st.session_state.botao_ativo = True # Atualiza o estado para indicar que os resultados foram exibidos
elif resultado:
st.markdown('<h2>Resultado da consulta:</h2>', unsafe_allow_html=True)
st.write(resultado)
st.session_state.botao_ativo = True # Atualiza o estado para indicar que os resultados foram exibidos
else:
st.error('Nenhum resultado encontrado para os valores fornecidos.')
st.session_state.botao_ativo = False # atualiza o estado para indicar que os resultados nao foram exibidos
That’s because the streamlit script runs from top to bottom on any input, so in this case what happens is, if I’m reading your code correctly:
You push btns[0]
You check to see if butao_ativo is true, it’s not, so btns[1] stays disabled
You check btns[0] and do a bunch of stuff if it’s clicked, including changing butao_ativo.
Note that 3 happens after 2, so it won’t be until the next time the script runs that btns[1] is active.
There are two ways around this:
Move the code that should run if btns[0] is clicked into a function, and do on_click=that_function. on_click functions like that always run as if they were at the top of the script, so if you change butao_ativo inside of that function, it will be changed by the time the script gets to btns[1].
After you change butao_ativo in your current script, call st.rerun() to rerun the script from top to bottom.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.