"None" gets printed multiple times while output string is being generated

Summary

I have an if button. inside it output = function(). this function takes a minute to run, while it’s being ran, I get a lot of green "None"s printed in the webpage.

How do I stop this from happening? I’ve tried removing all print statements, changed the position of the function_that_takes_time_to_finish, added a time.sleep.
As soon as function_that_takes_time_to_finish is ran, I start getting None on the screen.
(This function is a bit long and has proprietary stuff that I can’t share.)

Steps to reproduce

Having a function that takes time to execute like the code snippet

Code snippet:

  website = st.text_input(label="Website", placeholder="Website Here...")
  slider = st.slider("How many children links to check", min_value=0, max_value=100, value=3)
  
  col1, col2, col3 = st.columns(3)
  with col2:
    button = st.button("Generate")
  
  if button:
    with col2:
      with st.spinner('Generating...',):
        output = function_that_takes_time_to_finish(website, slider) 
    st.write(f"""{output}""")

Expected behavior:

I expect no None’s to get printed like the picture

Actual behavior:

I get a lot of Nones. The longer my method takes to run, the more Nones I see.

Hi @AhmadRehan ,

In order to debug this, it is important to know what function_that_takes_time_to_finish does. Can you share the code?

Can’t test this right now but see below a different approach (saving the output to a session state variable). Let me know if it helps.

  website = st.text_input(label="Website", placeholder="Website Here...")
  slider = st.slider("How many children links to check", min_value=0, max_value=100, value=3)
  
  col1, col2, col3 = st.columns(3)
  with col2:
    button = st.button("Generate")
  
  if button:
    with col2:
      with st.spinner('Generating...',):
        st.session_state.output = function_that_takes_time_to_finish(website, slider)
        st.experimental_rerun()

  if 'output' in st.session_state:
      st.write(f"{st.session_state.output}")
1 Like

Thanks for your help. You made me try to isolate the culprit piece of code

It turns out a piece of code like this:

try:
  code
except:
  None

triggers streamlit to print None.

I’ve replaced None with pass and it’s no longer printing None.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.