Session State for multiple nested button

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.
Here is the code:

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

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

            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',
                )

Try putting the upload codes inside a st.form, you’ll have more control on when the refresh occur. Here’s how I do my upload process.

with st.form("my-form", clear_on_submit=True):
        UPLOADED_FILES = st.file_uploader('Attachments',accept_multiple_files=True)
        submitted = st.form_submit_button("UPLOAD")
        if submitted :
            if not util.save_attachment(UPLOADED_FILES,str_table,record_id):
                st.error(f"Fail upload file")

Hi @Initial_DNA

Thanks for your reply

when I did this, it brought an error
“NameError: name ‘util’ is not defined”

That’s my code, it was just to give an idea on how to structure it. Try this

if 'data' not in st.session_state :
    st.session_state.data = None

if st.button("Upload CSV with DateTime Column"):
    with st.form("my_form_u", clear_on_submit=True):
        st.write("IMPORT DATA")
        st.write(
            "Import the time series CSV file. It should have one column labelled as 'DateTime'")

        st.file_uploader('Upload here', type='csv', key='data')
        submitted = st.form_submit_button("UPLOAD")

if st.session_state.data is not None:
    dataset = pd.read_csv(st.session_state.data)
    .
    .

Thanks, I’ve been able to fix it.

Thanks a lot for your time.