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