How to stop stream lit to run the function

Summary

I run the streamlit app.py file it also run the function which has to be runned on specific conditioned , but I assume that when I run the file app.py it also run that function?

how to stop that?
Share a clear and concise description of the issue. Aim for 2-3 sentences.

Steps to reproduce

Code snippet:

add code here

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

Explain what you expect to happen when you run the code above.

Actual behavior:

Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

Debug info

  • Streamlit version: (get it with $ streamlit version)
  • Python version: (get it with $ python --version)
  • Using Conda? PipEnv? PyEnv? Pex?
  • OS version:
  • Browser version:

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

You can test this way:

  • Run your script without using streamlit.
  • Check the result. If the function was run, then that function will be run by streamlit too if you use streamlit.

It is better if you post a sample code to avoid misunderstanding.

The code which I have

user_input = st.text_input("Please enter the word")

if len(user_input.split()) > 1:
        st.write("please enter one word only:")
else:
    test = user_input
    k = mainp(test)

The mainp is the function which process the word, but it run that function, since it run the function with out having the input it throws the error.

The function still work if I enter the word , if I comment the section where i am calling the function it will not show any error,
what i am missing?

user_input = st.text_input("Please enter the word")

From the start, the value of user_input from the code above is an empty character or string β€œβ€.

if len(user_input.split()) > 1:

That code checks if empty char converted to a list, the number of elements is more than 1.

Let’s calculate.

num_elem = len("".split())
# num_elem = 0
if num_elem > 1:
    st.write("please enter one word only:")
else:
    # since num_elem is 0, the code below will be executed.
    test = user_input
    k = mainp(test)

So the mainp() is run because num_elem is not more than 1. It is zero.

Workaround

import streamlit as st


def mainp(input_):
    st.write(f'mainp: got {input_}')


user_input = st.text_input("Please enter the word")

# if len(user_input.split()) > 1:
    # st.write("please enter one word only:")
# else:
    # test = user_input
    # k = mainp(test)

if len(user_input.split()) == 1:
    test = user_input
    k = mainp(test)
else:
    st.write("please enter one word only:")

Reference

text input

Oh thank you much , it was so absurd of me to not see that flaw in my logic of checking word lenght, it works now perfectly , thankyou for the assitance really appreciated

Kind Regards,

AB

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