import streamlit as st
def calc(name):
st.write(f'{name}, 1+{len(name)} is {1+len(name)}')
def name_input():
txt = st.text_input('Hi, whats your name?')
if (not txt) & (start==True):
st.warning('You MUST give me your name')
st.stop()
st.write(type(txt))
st.success(f'Hi {txt}!')
calc(txt)
start = st.button('Let us begin')
while start == True :
name_input()
First thing to note here, in your if statement, that & is a bitwise AND operator. What you want is a simple and instead: if not txt and start: .... You could even drop that start==True in my opinion.
Now to explain the issue, there are two things to understand with Streamlit:
Everytime you interact with your application (when you click on a button, when you input some text, etc.) the whole script is executed again from the beginning.
When you click a button, it will return True for one run only. Which means when you interact with your text_input, the script reruns, but your button will return False this time, therefore your text input won’t appear.
There are two solutions I would recommend:
Remove that button
Use a checkbox instead.
A third solution would be to use session states. But I wouldn’t recommend it because it’s a little bit hack-ish, and in terms of UI experience, I feel like streamlit buttons are meant to execute a one-time operation, and shouldn’t keep a “I was clicked” state. But this is subjective, I give you that solution in case you still want to do it that way
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.