I think I found a problem with streamlit but I am not sure…
I would like to build an app that samples a random number, let the user input some answer and save the number and the answer to a .csv file, from some reason, the input is saved with the next randomly generated number, below is an example:
I expected this code to create two-line .csv files that look like the following:
inputted_idx,idx
42,42
but instead, the number that will be saved under idx will be the next generated index (the one that will show after the refresh). for example:
inputted_idx,idx
42,22
import random
import streamlit as st
import pandas as pd
import datetime
save_path = '~/'
idx = random.randint(1,100)
st.write(idx)
with st.form('Form', clear_on_submit=True):
inputted_idx = st.text_input('Input the number written above')
submit = st.form_submit_button('Submit')
if submit:
pd.DataFrame({
'inputted_idx':[inputted_idx], 'idx':[idx]}) \
.to_csv(save_path+str(datetime.datetime.now().time()), index=False)
This is not a problem with Streamlit, it is the basis of how Streamlit works and runs your code. This is because as soon as the user interacts with your app, it reruns, and your code re-generates a new idx.
So the inputted_idx is the random number from the first run of the app, in your example 42. But the idx is the new random number from the current run of the app, in your example 22.
I’m not sure I fully understand why you generate a random number to display on the screen and then have the user input that same number. Can you explain the use case here?
Hi @Marisa_Smith, thanks for your replay,
The code I posted is not my use case, only the simplest runnable example of my problem.
My actual use case is the following:
I have a DataFrame, from which I would like to sample a random row and have the user input some annotations (with some input widgets). I would then like to save the sampled row along with the annotation.
In the current situation, I am finding myself saving the annotations along with the next sampled row.
AH! I see what you’re doing now! I have actually done something similar with an app I made about Monte Carlo modelling!
So, instead of using random to generate a number every time the script runs, you can use state to store a random number until you want to generate a new one.
# initializing with a random number
if "rn" not in st.session_state:
st.session_state["rn"] = random.randint(1,100)
# callback function to change the random number stored in state
def change_number():
st.session_state["rn"] = random.randint(1,100)
return
st.write(st.session_state.rn)
## button to generate a new random number
st.button("New row?", on_click=change_number)
with st.form('Form', clear_on_submit=True):
inputted_idx = st.text_input('Input the number written above')
submit = st.form_submit_button('Submit')
if submit:
data = pd.DataFrame({'inputted_idx':[inputted_idx], 'idx':[st.session_state.rn]})
st.dataframe(data)
With this flow, the app generates a random number that is displayed on the screen, the person can then use the form to enter that number into the app and hit submit. I just displayed the data to the app so you can see that both are showing the correct number.
Once the user is finished with that random number/row of the data, they can hit the button at the top called New row?, which triggers a callback function. The callback function simply updates the session state to have a. new random number to work with.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.