Hi users,
I am not able to save the file with an absolute path + file name. I want to save it as a csv file. I can’t hit ‘Press enter to apply’ and ‘Download CSV’ buttons.
Here’s the following code:
if st.button(“Generate Rating Curve”):
try:
# Generate rating curve
df = generate_rating_curve(diameter, slope, manning_n, invert_level)
# Plot the rating curve
fig, ax = plt.subplots()
ax.plot(df['Flow (m³/s)'], df['Elevation (mOD)'])
ax.set_ylabel("Elevation (mOD)")
ax.set_xlabel("Flow (m³/s)")
ax.grid(True)
st.pyplot(fig)
# Optional file name input
file_name_input = st.text_input("Enter the file name for the CSV (with extension, e.g., rating_curve.csv)", value="")
if file_name_input:
if not file_name_input.endswith(".csv"):
st.error("File name must end with '.csv'")
else:
# Save file locally
file_path = os.path.join(os.getcwd(), file_name_input)
df.to_csv(file_path, index=False)
st.success(f"File saved successfully at: {file_path}")
# Always provide download button with default name
csv = df.to_csv(index=False)
st.download_button(
label="Download CSV",
data=csv,
file_name="rating_curve.csv" if not file_name_input else file_name_input,
mime='text/csv'
)
except Exception as e:
st.error(f"An error occurred: {str(e)}")
If you’re creating a debugging post, please include the following info:
- I am running this app locally and not deployed.
- I am not getting an error message.
- Streamlit version is 1.36 and Python version is 3.11.