Button on_click behavior

I have a button with an on_click() to run a function which executes a query. It seems the on_click() is triggered when the parameters change, even if the button is not clicked. Example below - when queryText is changed via a text_area control, the on_click() function is executed.

 queryText = st.sidebar.text_area("SQL to execute:", value="", height=3, max_chars=None )     
    if queryText:                     
        # issue - on_click executes when queryText is changed  , even if not clicked
        btnResult= st.sidebar.button('Run', key='RunBtn', help=None, on_click=runQuery(queryText,False), args=None, kwargs=None)
        if btnResult:
            st.sidebar.text('Button pushed')

Hi @MarkSim.,

Based on your simplified example I don’t think you should use state for this. I actually think you just need to use a form, that collects the SQL query and then has a form_submit_button that runs the typed query:

with st.sidebar.form("Input"):
    queryText = st.text_area("SQL to execute:", height=3, max_chars=None)
    btnResult = st.form_submit_button('Run')

if btnResult:
    st.sidebar.text('Button pushed')

    # run query

In my token example, I simply write the query to the page but you would be able to actually send the queryText to get the data.

Let me know if this solves your issue!
Happy Streamlit-ing

1 Like