question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request: st.stop()

See original GitHub issue

Problem

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:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
kmcgradycommented, Aug 7, 2020

st.stop() has been implemented, so I will close this ticket and open up a new issue for st.rerun().

2reactions
tvstcommented, Jun 30, 2020

Just dropping this quickly here: we decided to move ahead with st.stop but not st.rerun just yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

st.stop - Streamlit Docs
Stops execution immediately. Streamlit will not run any statements after st.stop() . We recommend rendering a message to explain why the script has...
Read more >
Street Signs | ddot - The District Department of Transportation
Request street sign services (replacement of missing or damaged signs), by calling 311 or online using the Service ... STOP sign replacement: 24...
Read more >
Streamlit Tricks — Application Reruns on Every Widget Click ...
Let's write a very simple Streamlit application, plotting your fruits lists ... import plotly.express as pxst.header("Fruits List")# ...
Read more >
Programmatically stop execution of python script? [duplicate]
sys.exit() gives errors before it kills the application. – CodeNinja. Aug 23, 2018 at 12:01.
Read more >
ExxonMobil
ExxonMobil is one of the world's largest publicly traded international oil and gas companies. Learn more at ExxonMobil.com.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found