Session state for multiple nested buttons

Please how can I handle this, this is my first time using streamlit

Expected Behavior
I want this code to work this way, once the user uploads the csv file, it runs the if data is not None if statement, then shows the “Predict Dataset” Button", which when pressed computes the code in that if statement that shows a download button for the data download.
Actual Behavior
It restarts the session anytime I upload a file
Debug info
Python 3.9.12
streamlit 1.13.0
OS:Windows 10
Chrome,
VS Code editor

My Question now is how to use session state to fix this issue.

if st.button("Upload CSV with DateTime Column"):
        

        st.write("IMPORT DATA")
        st.write(
            "Import the time series CSV file. It should have one column labelled as 'DateTime'")
        data = st.file_uploader('Upload here', type='csv')
        st.session_state.counter = 0
        if data is not None:
            dataset = pd.read_csv(data)
            dataset['DateTime'] = pd.to_datetime(dataset['DateTime'])
            dataset = dataset.sort_values('DateTime')

            junction = st.number_input(
                'Which Junction:', min_value=1, max_value=4, value=1, step=1, format='%d')

            results = predict_traffic(junction, dataset['DateTime'])
            st.write('Upload Sucessful')
            st.session_state.counter += 1
            if st.button("Predict Dataset"):
                result = results
                result = pd.concat([dataset, result], axis=1)
                st.success('Successful!!!')
                st.write('Predicting for Junction', 1)
                st.write(result)

                def convert_df(df):
                    # IMPORTANT: Cache the conversion to prevent computation on every rerun
                    return df.to_csv().encode('utf-8')

                csv = convert_df(result)

                st.download_button(
                    label="Download Traffic Predictions as CSV",
                    data=csv,
                    file_name='Traffic Predictions.csv',
                    mime='text/csv',
                )
                st.session_state.counter += 1

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.

Hi there,

Thanks for sharing your question with the community! Check out our guidelines on how to post an effective question here – in particular, please share your question or a description of the issue you’re trying to solve, rather than just the code.

This thread will be locked until the original post is updated.

Caroline :balloon:

1 Like