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