Trigger button or checkbox

This one feels obvious so sorry if it has been answered before.

Is there any way for me to trigger another button press or checkbox when a different button is pressed?

i.e.

if submit_button:
other_submit_button()

You can do the following:

agree = st.checkbox("I agree")
if agree:
    st.checkbox("Great", value = True)

Value = True will set the default selection to triggered.

Bjorn,

Thanks for the response and that is helpful! but what I’m really looking for is a way to check an already rendered button.

For whatever reason, I have to press a button twice for the rest of the page to update.

Hi @posey22,

Perhaps you could do something like this. Note however that if you uncheck the first checkbox it will also uncheck the second.

import streamlit as st

cb1 = st.checkbox('a')

empty = st.empty()
empty.checkbox('b')

if cb1:
    empty.checkbox('b', True)

Or

import streamlit as st

button = st.button('a')

empty = st.empty()
empty.checkbox('c')

if button:
    empty.checkbox('c', True)

I don’t think there’s a way to trigger a click on a button at the moment as st.button doesn’t accept a value argument.

However I’m interested to understand your use case for this. In what scenario do you have to press a button twice for the rest of the page to update.?

2 Likes

When I press a button currently I’m submitting a sql query that updates my database with some new values. There’s a table on the page that is reflecting the data in that DB that I’m updating. That table doesn’t update when I click that button to submit the sql query. If I click update a second time it does update the table. Was just brainstorming ways to hack my way around this feature. @Jonathan_Rhone

Right now my users are hitting a checkbox to update the table which is kinda confusing for them. If the table could update whenever I click submit that would be way preferred obviously.

Hey @posey22, do you have a small toy example in order to replicate this behavior?

I’ve been playing with st.button() and SessionState and it works at the first time.

import streamlit as st
import SessionState

data = [1, 2, 3, 4, 5, 6, 7, 8, 9]

state = SessionState.get(table=data)


def doSqlQuery():
    # sql code here

    return state.table.append(len(state.table) + 1)


if st.button("Do SQL"):
    doSqlQuery()

st.table(state.table)