Text_input returns nothing unless mouse is clicked

Hi,

I have a text_input asking for a username and a button that triggers a function that creates a user with the username supplied.

The issue is that if I enter the username and click the button, the username = β€œβ€.
only if I enter the username, click anywhere on the screen and then click the button, I get the real value of the text_input.

Steps to reproduce

Code snippet:

username = tenant_actions_tab.text_input("Email address")
st.button("Submit", on_click=add_user, args=(username, st,))

def select_tenant(username, st):
     st.write(username)

Expected behavior:

enter β€œabc” in the text input, click the button and β€œabc” would appear on the screen

Actual behavior:

enter β€œabc” in the text input, click the button. not printing anything.
clicking the button again, prints β€œabc”

Debug info

streamlit==1.22.0

Requirements file

streamlit==1.22.0

Hey @Yaron_Bialik,

Thanks for sharing your question! Please edit your code snippet to make it runnable as-is so we can reproduce the issue

I simplified your code so it was executable, and this works for me:

import streamlit as st

if 'user' not in st.session_state:
    st.session_state['user'] = None
    st.session_state['i'] = 0

def add_user(username):
    st.session_state['user'] = username

username = st.text_input("Email address")
st.button("Submit", on_click=add_user, args=[username])

st.write(st.session_state)
st.session_state['i'] += 1

It is true in general that for a text_input widget, the return value will not be updated until you press enter or click out of it somewhere. I tested by typing into the text_input widget and clicking directly on the Submit button. What happens in this case is that the page is going to rerun twice: once in response to change the text_input and a second time in response to clicking the button. This seems like a peculiar kind of edge case as it would be more expected to hit enter at the end of typing into the widget (which would reload the page) and then click submit (which would reload the page a second time).

The st.session_state.i value is how you can see the page reruns twice if you click directly on submit before committing the data to text_input.

If you are still having problems, feel free to edit you example to be executable as Caroline suggested. If you describe the workflow you want your user to go through exactly, there might be some alternative structure to recommend for you.

Hi,

so weirdly enough your code works but if I am adding a function that runs a postgres query (unrelated to the button), it writes blank.

meaning

def add_user_button(username):
    st.session_state['user'] = username
    tenant_actions_tab.write(username)

 tenant_info_tab, tenant_actions_tab = st.tabs(["Tenants","Actions"])
    selected_tenant = tenant_actions_tab.selectbox("Select a tenant", ["a","b"])
    selected_action = tenant_actions_tab.selectbox("Action",(
        [
            "Select an option",
            "Get initial login information", 
            "Get firewall rules", 
            "Reset user password", 
            "Create a new user",
            "Delete a user",
            "Add SAML"
        ]
    ))
    if (selected_action == "Create a new user"):
        tenant_actions_tab.info("Please press enter after supplying the email address")
        selected_user = tenant_actions_tab.text_input("Email address")
        st.button("Submit", on_click=add_user_button, args=[selected_user])



works. but if I add this line after the creation of the tabs
fetch_tenants(mssp)

and this function is:

def fetch_tenants(mssp):
    query = f"""
    SELECT *
    FROM tenants;
    """

    # execute query
    cursor.execute(query)

    # fetch all results
    results = cursor.fetchall()
    col_names = [desc[0] for desc in cursor.description]

    return pd.DataFrame(results, columns=col_names)

it will write β€œβ€ when clicking the button.
I changed the fetch_tenants to return β€œβ€ without running the sql query and it works, the button prints the username.

I really have no idea why running a postgres query changes the behavior :expressionless:

Hi all,

so I added @st.cache_data
before the postgresql function and it fixed the issue

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