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.
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.
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
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.