Getting ValueError for st.text_input

Here is the rest of the code in the file just in case you do not have it.

sim_weibos = {}
    sim_list = []  # these are the values for the dict sim_weibos
    weibo_id = []  # these are the keys for the dict sim_weibos
               
    for i in range(len(final_weibos_new)):
        sim = model.n_similarity(weibo_test, final_weibos_new[i])
        sim_list.append(sim)
        weibo_id.append(i)
    st.write(8)
    #print("")
    #print("")
    #print(weibo_id, sim_list)
    #print("")

    for i in weibo_id:
            sim_weibos[i] = sim_list[i]

    #print(sim_weibos)
    #print("")
    sim_weibos_sorted_ascending = dict(sorted(sim_weibos.items(), key=lambda item: item[1])) 
    sim_weibos_sorted_descending = dict(sorted(sim_weibos.items(), key=lambda item: item[1], reverse=True)) 
    st.write(9)
    #print(sim_weibos_sorted_ascending)
    #print("\r")
    ##print(sim_weibos_sorted_descending)  #Uncomment this if you want to see the similar posts before the weibo_test is removed

    #print("What weibos are similar to weibo id " +str(weibo_input) +" :")
    st.write("What weibos are similar to weibo id" +str(weibo_input) +" :")
    #print(weibo_test)
    st.write(weibo_test)
    # remove the first key/value from the dict since it is comparing the weibo to itself
    # since we sort by ascending order the first key will always be the same as the weibo we're looking to compare
    # we use first_key when we dont know the key.  Comment the line below if you know the key such as
    # when you request input from the user
    st.write(10)
    #first_key = next(iter(sim_weibos_sorted_descending)) # gets first key in the dict  Use this when we dont know the the key like if we hardcode the weibo to check

    # If you request input from the user then we know the key/weibo id that we should remove from the
    # comparison.  If we know the key use the code line below for same key

    same_key = weibo  # use this when we request input from the user to select the weibo id
    st.write(11)
    #sim_weibos_sorted_descending.pop(first_key) # removes the first key when we dont know the key, like if we hardcode the key weibo1 for testing
    sim_weibos_sorted_descending.pop(same_key)
    print("\r")
    print(sim_weibos_sorted_descending)
    print("\r")
    st.write(12)
    print("Top 3 Most Similar Weibos to Weibo ID " +str(weibo_input) + ":")
    st.write("Top 3 Most Similar Weibos to Weibo ID " +str(weibo_input) + ":")
    # Get the keys from sim_weibos_sorted_descending to display the top 3 similar posts
    sim_weibo_ids = list(sim_weibos_sorted_descending.keys())  # list of similar weibo ids compared to the weibo_test

    print("\r")
    for i in range(len(sim_weibo_ids[0:3])):  # sim_weibo_ids[0:4] we only select the top 3 similar weibos
        #print("Weibo ID",sim_weibo_ids[i],":",final_weibos_new[i])
        st.write("Weibo ID",sim_weibo_ids[i],":",final_weibos_new[i])
    st.write(13)
    # In[56]:


    # WE DONT NEED THIS CODE.  IT IS ONLY USED TO FIND BAD CHARACTERS AND VERIFY THAT THEY WERE REMOVED IN PREVIOUS STEPS

    #print(final_weibos_new[110])
    #print(final_weibos[110])
    #word = '27'
    #word = '""""'
    #for i in range(len(final_weibos_new)):
    #    if word in final_weibos_new[i]:
    #        print(i,word)

    #for i in range(len(final_weibos_new)):
    #    final_weibos_new[i] = list(filter(('""""').__ne__, final_weibos_new[i]))

    #print(final_weibos[110])
    #print(final_weibos_new[110])

    # In[423]:


    #################################################################################################### 
    #            Compute the cosine similarity between the first 10 posts and store in sim_matrix
    ####################################################################################################
    temp_list = []
    num_weibos = 10
    sim_matrix = [0]*num_weibos
    #print(sim_matrix)

    for i in range(num_weibos):
        for j in range(num_weibos):
            sim_i_j = model.n_similarity(final_weibos_new[i], final_weibos_new[j])
            temp_list.append(sim_i_j)
        sim_matrix[i] = list(temp_list)
        temp_list = []

    #print(len(sim_matrix))
    #print(sim_matrix)
    st.table(sim_matrix)
    #print(max(sim_matrix))
    st.table(max(sim_matrix))
    st.write(14)
    # In[424]:


    # Program to plot 2-D Heat map
    # using seaborn.heatmap() method
    import numpy as np
    import seaborn as sns
    import matplotlib.pylab as plt
    import streamlit as st

    fig = plt.figure(figsize = (15, 10))
    print(type(sim_matrix))
    print(len(sim_matrix))
    data_set = sim_matrix

    data_set = sim_matrix
    ax = sns.heatmap( data_set , linewidth = 0.2 , cmap = 'coolwarm' )
      
    plt.title( "2-D Heat Map" )
    plt.show()
    st.pyplot(fig)
    st.write(15)
    # In[426]:


    sim_input = input("Enter a weibo id between 0 and " + str(num_weibos-1) + " to find the most similar weibo: ")
    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)
    st.write(val)
    st.write(idx)
    #most_sim = max(sim_matrix[sim_input])
    #print(most_sim)
    st.write(16)
    # convert list of floats to string
    #result = ", ".join([str(i) for i in sim_matrix[sim_input]])
    #print(result)

    #most_sim_idx = result.index(str(most_sim))
    #print("The most similar weibo is " +str(most_sim)+ " at index", most_sim_idx)

    #print(sorted(sim_matrix[sim_input].index, reverse=True))
    #print(sorted(sim_matrix[sim_input], reverse=True))

Where you see st.write(7) is the last number I see when I run the code I have to input a number in the input I showed you above but when that input runs afterwards I get the st.experimental_rerun error.

I don’t see st.write(7) in the snippet you posted, but assuming this is what immediately follows, my eyes are drawn to your loop and wondering if the model is executing correctly (or sufficiently quickly that it’s not a bottleneck). Try checking how the loop is progressing:

for i in range(len(final_weibos_new)):
    print(f'Starting iteration {i}')
    print(f'Looking at {final_weibos_new[i]}') # Or some variation, depending on how big it is to print a weibo
    sim = model.n_similarity(weibo_test, final_weibos_new[i])
    sim_list.append(sim)
    weibo_id.append(i)

st.write(7) was the line above first line I copied above. I can copy in your code and test it.






ok the code I copied in basically bypassed the error initially but it wouldn’t allow me to enter in the user input and then the code ran for the other parts and then gave the error at the end. Above are screenshots of the program after I ran it.

You’ve got an input statement after st.write(15). Make sure you don’t have Python input statements. You need to use Streamlit input elements.

Change this part:

sim_input = input("Enter a weibo id between 0 and " + str(num_weibos-1) + " to find the most similar weibo: ")
sim_input = int(sim_input)

Yeah I was trying to figure out what the correct syntax I tried to put the first in a st.write but I believe i got an error I wasn’t able to get those lines to work properly, any suggestions I tried to replicate the input mentioned above and follow that same format but it didn’t work.

I am referring to these lines.
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”)

What error do you get when you use a Streamlit input for your sim_input? A specific error and trace would help, especially if this is the part that’s somehow contributing to the larger issue.

Unfortunately I do not know the code cuts out before that code can be initiated that is the problem with that st.experimental_rerun error.

Here is what happens when I enter a input into the first prompt.

Ok. To recap so I understand where you are. You’ve got:

st.write(15)
sim_input = input("Enter a weibo id between 0 and " + str(num_weibos-1) + " to find the most similar weibo: ")
sim_input = int(sim_input)

You are not able to enter the number input into the terminal to continue?

If you use a Streamlit Input like this:

st.write(15)
sim_label = "Enter a weibo id between 0 and " + str(num_weibos-1) + " to find the most similar weibo:"
sim_input = st.number_input(sim_label, 0, num_weibos-1, value=0, step=1, key='sim_input')
sim_input = int(sim_input)

The widget does not render and you go straight to the experimental rerun warning?

No so it doesn’t even get to this point it only runs for the user input all the way at the beginning. In my screenshot above where the user is asked it input a number from 0-37339 with the question being Enter a Weibo id between those numbers. The code that supposedly stops the program from running is on line 107.

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")

After I enter in the weibo Id it says its running and then I get the calling st.experimental_rerun() with a callback is a no-op. error.

Can you:

  1. Clear your cache/reboot your app again.
  2. Add a keys to both inputs with a check on how session state is seeing them as follows.
label = "Enter a weibo id between 0 and " + str(max_num_weibos - 1) + " to find similar weibos:"
st.session_state
weibo_input = st.number_input(label, 0, max_num_weibos-1, value=0, step=1, key='weibo_input')
st.session_state

and the second input similarly:

sim_label = "Enter a weibo id between 0 and " + str(num_weibos-1) + " to find the most similar weibo:"
st.session_state
sim_input = st.number_input(sim_label, 0, num_weibos-1, value=0, step=1, key='sim_input')
st.session_state

Ok I tried copying in the code you sent above and I am still getting the callback error after I input a number into the first code block from above.

Please can you show the screen shots with the readout from session state and st.write outputs for clarity?

Sure! I want by the way thank you so much for the amount of help you giving me I really appreciate it!

I looks to me to be acting the same!

Do you have the st.session_state lines, too? Streamlit magic should render those before and after the input if they are there.

Yeah I copied straight from your comment I also removed all the st.write statements I added but I am still getting the error.

I’m spinning up a VM and will run your code in debug to see if I can find where you get an st.experimental_rerun error without having the function called within the code. If you have any updates to include on your github, go ahead and push them. :smile:

Ok, no updates on my end just been trying to debug myself for the past 4 hours with no success!