To upload files, please first save the app
import os
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
import io
# Function to create a random plot
def create_random_plot():
plt.figure()
x = np.random.rand(10)
y = np.random.rand(10)
plt.scatter(x, y)
plt.title('Random Scatter Plot')
# Save the plot to a bytes buffer
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
return buf
st.title('Random Plot Generator')
# Generate and display the plot
plot_buffer = create_random_plot()
st.image(plot_buffer, caption='Generated Random Plot')
# Directory input for storing the image
directory = st.text_input("Enter directory to save the image:")
# Download button
if st.button("Download plot"):
directory = directory or "."
# Create parent directories if needed
if not os.path.exists(directory):
os.makedirs(directory)
file_path = os.path.join(directory, 'random_plot.png')
# Save the plot to the specified directory
with open(file_path, "wb") as f:
f.write(plot_buffer.getbuffer())
# Check if the file was saved successfully
if os.path.exists(file_path):
st.success(f"Plot saved successfully at: {file_path}")
# List directory contents to verify
st.write("Directory contents:")
st.write(os.listdir(directory))
else:
st.error("Failed to save the plot.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?