Randomly generate a number and save this number and user input


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:


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:


import random

import streamlit as st
import pandas as pd
import datetime

save_path = '~/'

idx = random.randint(1,100)

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:
            'inputted_idx':[inputted_idx], 'idx':[idx]}) \
                      .to_csv(save_path+str(datetime.datetime.now().time()), index=False)

Hi @per,

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?

Happy Streamlit-ing

1 Like

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.


1 Like

Hey @per,

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)


## 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]})

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.

Happy Streamlit-ing!

1 Like

This is exactly what I wanted :grinning:
Thanks for your help!
Streamlit rocks!

1 Like