How to return early from streamlit book page

Hi,

How do you return from a page?

if text == '':
    st.error('empty....')
    return

In this case, I hope to avoid:

if text == '':
    st.error(''empty....')
else:
    <do something else>

So with the return, it generates error:

SyntaxError: 'return' outside function

It seems like streamlit_book wraps the page (.py program) into a function.

Any thoughts?

TIA!

Hi @jeisma -

return is a very specific keyword, and it’s complaining that you’re trying to use it outside of a def func(): statement.

You can frequently use pass in the manner I think you’re describing, or you could just do the inverse of your logic:

if text and len(text)>0: #assuming that 0-length strings can happen
    <do_something>

If you can post a link to the actual code, others might be able to suggest a more specific solution to the streamlit_book project.

Best,
Randy

Hi Randy,

Don’t have the link to the code, updating the code snip

import streamlit as st

text = st.text_area('Text to Parse')
    
if st.button('Submit'):

    if text == '':
        st.error('empty....')
        # won't let me do this      
        return 

    # <do something else>

TIA!

You’re missing what I’m saying here…return only works as part of a function, which isn’t what you are providing here, you are just using if statements.

oh yeah, completely missed that. have to wrap that first in a function.

thanks!