Getting ValueError for st.text_input

Summary

I am getting an error when I run my program which is using the st.text_input function and I am not sure why I am getting this error.

Steps to reproduce

Code snippet:

weibo_input = st.text_input("Enter a weibo id between 0 and " + str(max_num_weibos) + " to find similar weibos:")
    #print("Finding similar weibos to weibo id:", weibo_input, ".....")
    st.write("Finding similar weibos to weibo id:", weibo_input, ".....")
    #print("\r")
    st.write("\r")

    weibo = int(weibo_input)

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

The code should run normally and it was working in my Jupyter notebook so I am not sure what broke. Is it possible that the st.text_input is causing the issue?

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

The program should run normally and output a result.

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: 1.15.1
  • Python version: 3.9.15
  • Using Conda
  • OS version: Windows 10
  • Browser version: Chrome

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.

Your code raises IndentationError for me. After fixing the indentation it raises NameError because max_num_weibos is undefined. After fixing that it raises ValueError because weibo_input is an empty string which cannot be converted to an int.

1 Like

In that case, weibo_input should use st.number_input instead of st.text_input, if weibo IDs are integers.

Thank you that seems to have solved the problem but I am now getting an error called “Calling st.experimental_rerun() within a callback is a no-op.” when I enter in the value into my app. Not sure how to solve this issue.

  1. Find where you have st.experimental_rerun() inside of a function definition.
  2. If that function is assigned to any on_change or on_click keyword for a widget elsewhere, you need to remove st.experimental_rerun() from that function
  3. If you were using the st.experimental_rerun() to escape out of the callback function prematurely, replace it with return to accomplish the same effect

So I actually do not have a st.experimental_rerun() in my code at all.

Can you share the full code or link to GitHub?

Yeah my github is GitHub - Arothman12/FreeWeibo The main file is called POS.py and in there I have the st.button command that when clicking the Word Embeddings button it runs the code called avidanwordembeddings.py. I was able to run the program again and I did not see the error I mentioned above again but I am having other issues particularly regarding the commands I have in the avidanwordembeddings.py lines 390-468

For your input, you should include restrictions within the widget so a user cannot ignore your instructions and enter an invalid number. You immediately use the widget value in a list lookup so your code will break here if a user doesn’t follow the written directions. Also, your max_num_weibos is the length of your list. Since Python indexes starting at 0, you need a shift by 1.

label = "Enter a weibo id between 0 and " + str(max_num_weibos - 1) + " to find similar weibos:"
weibo_input = st.number_input(label, 0, max_num_weibos-1, value=0, step=1)

Try that for starters. If you have other problems, feel free to specify what’s going wrong.

The above code helped wonders I actually had to change things a bit up and I have hit a new snag. I am working on the file called P2WordEmbedding.py found in my github I listed above and I am getting an error on line 206 saying that the name ‘final_weibos’ is not defined. I am not sure why I am getting this error since I have the function final_weibos defined above.

I’m on mobile, so maybe I missed it, but I’m seeing final_weibos_new defined above, not final_weibos. What line do you think it’s defined on?

Good catch! I am stuck getting the last part of the code to appear as an user input the same way the first part of the code is asking for user input. Where they are asked to enter a Weibo ID from 0-max_weibos on line 105 I am working on the code on line 241 and I have it built as of now like this.

label = sim_input = input("Enter a weibo id between 0 and " + str(num_weibos-1) + " to find the most similar weibo: ")
sim_input = st.number_input(label2, 0, num_weibos-1, value=0, step=1)
sim_input = int(sim_input)
remove_input = sim_matrix[sim_input].pop(sim_input)  # remove the weibo you're searching from the row since it will always be the largest
#print(remove_input)

sim_row = sim_matrix[sim_input] # get the row of the weibo

val, idx = max((val, idx) for (idx, val) in enumerate(sim_row))
print(val,idx)

I am not seeing on my streamlit page this user input it is still prompting the user in the backend in the command prompt.

I have fixed the above issue but I am having that same Calling st.experimental_rerun() within a callback is a no-op. error come up when I run POS.py and I select the button Word Embeddings. An update when the Word Embeddings is selecting is calling the code from the file P2WordEmbedding.py now.

Update I tried to change it back to the other file avidanwordembeddings.py and now I am getting the error no matter what I am still not sure why I get this error when I do not have st.experimental_rerun() in my code at all!

@mathcatsand

Since that error spontaneously resolved before can you:

  1. Reboot the app if you have it on Streamlit Cloud, or close out of your Python terminal session to restart it fresh just to guarantee nothing is bugged from something earlier in the session.
  2. You’ve got a lot of imports and reimports in your code. Try cleaning that up with all the imports up top.
  3. Try breaking WEBIO down into smaller functions (it looks like you have some obvious separations where you’ve repeated your import statements. Say you have 5 major steps you are going through:
def step1 ():
    # Do the first chunk
    return

# Etc

def WEBIO ():
    step1()
    step2()
    step3()
    step4()
    step5()

Since I can’t find any st.experimental_rerun in your code as you say, here’s how I’d propose you work on debugging: Start with WEBIO doing nothing to confirm if you get that warning. If all is good, have it do just step1. If all is good, add step2, etc. That button click is calling up a function that’s hundreds of lines long, so without an error trace you have a very large haystack in which to find your needle. (Or do a debug run and carefully go through step by step to find the point at which that warning generates. I made a bad callback with a 10 second wait before an experimental rerun, and it generated the warning after the 10 seconds. Then I did it in reverse. So you should receive the warning in real-time when it hits the line in question.)

PS. You have your SQL username and password publicly posted. I understand this may be a toy database, but I strongly recommend you get in the practice of using secret management/key vaults. Both GitHub and Streamlit Cloud have systems to accommodate that without modification.

What should I do in regards to github?

Could you explain further what you mean by doing breaking the code into steps. Are you said I create a def lets say WEBIO1 and after the part of the code I want WEBIO to stop at I put return and the I do for the next chunk of code WEBIO2 and at the end I put return and do that 5 times to break the code apart?

For GitHub, here’s their documentation, but I’m sure you can find all sorts of YouTube tutorials on it if that is preferred.

For breaking up your code, you can pass information explicitly to and from your function as arguments and returns. Alternatively, you can also have your functions operate on global or enclosing variables.

It’s just that you are only getting a warning and not an error with a trace, hence the hundreds of lines triggered by that button click needs to be narrowed down.

Other options include commenting out the interior of WEBIO and slowly uncommenting it until you reintroduce that part that triggers the warning.

Or adding in print statements and time.sleep(2) so it goes slow enough to catch when the warning goes up.

Or as I mentioned earlier, running it in debug mode to step through line by line until you find the trigger point.

So I did a st.write test putting st.write statements throughout. My code ran until st.write(7) but my code required a user input from the code below and once you enter the info the code needs then I get the Calling st.experimental_rerun() within a callback is a no-op. error. The code that I have that requires a user input is below.

label = "Enter a weibo id between 0 and " + str(max_num_weibos - 1) + " to find similar weibos:"
    weibo_input = st.number_input(label, 0, max_num_weibos-1, value=0, step=1)
    #print("Finding similar weibos to weibo id:", weibo_input, ".....")
    st.write(5)
    st.write("These are similar weibos to weibo id entered:", weibo_input, ".....")
    #print("\r")
    st.write("\r")

Is it possible the way the code is written that this is the cause?

The input looks fine. If you’ve found where it has a problem using this information, that would be more interesting.

When I force a warning about st.experimental_rerun it is only a warning and there is no error trace. Is that still the case for you? Is the code executing correctly except that you are seeing the warning, or are you ending up with some other error?

I do not see any other error but the rest of my code does not run.