Request: st.stop()
See original GitHub issueProblem
I frequently want to verify some condition before proceeding with my Streamlit script. If that condition isn’t met, I’d like the script to stop right there.
Right now, I my work-around is to wrap the logic in a function so that
I can use return
for this purpose:
def get_some_input_which_might_fail():
input = st.text_input('input')
if not condition(input):
st.error('Please update input so that blah, blah...')
return
do_something(input)
get_some_input_which_might_fail()
but I’d rather not define a function just to get this functionality.
Solution
I would suggest that we define a StopExecution
Exception with the unique property that it is not rendered by the Streamlit runtime.
This would allow us to rewrite the code above more elegantly:
input = st.text_input('input')
if not condition(input):
st.error('Please update input so that blah, blah...')
raise StopExecution
do_something(input)
An Even Cooler Use Case
StopExecution
would also allow us to essentially define async/await syntax in Streamlit, as described in this gist and reproduced here:
@cache_on_button_press('Authenticate')
def authenticate(username, password):
return username == "buddha" and password == "s4msara"
username = st.text_input('username')
password = st.text_input('password')
if authenticate(username, password):
st.success('Logged in.')
else:
st.error('Incorrect username or password')
To understand what this use case does, just run it:
streamlit run https://gist.githubusercontent.com/treuille/bc4eacbb00bfc846b73eec2984869645/raw/734a2522d68920dfd1c875f321df67b3168afbf5/confirm_button_hack.py
Note that cache_on_button_press
can be completely implemetnd in Streamlit but for StopExecution
.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:8 (6 by maintainers)
Top GitHub Comments
st.stop()
has been implemented, so I will close this ticket and open up a new issue forst.rerun()
.Just dropping this quickly here: we decided to move ahead with
st.stop
but notst.rerun
just yet.