create an app that generates a plot, and add a sleep call to simulate it takes along time to plot, cache this so we see that after the first generation it doesn't take much to run
To upload files, please first save the app
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
from io import BytesIO
@st.cache_data
def generate_plot():
print("generating plot...")
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(10, 5))
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid()
buf = BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
val = buf.getvalue()
return val
st.title('Slow Plot Generator')
# you'll only see "generating plot..." once since the second call is cached
st.image(generate_plot(), caption='Sine Wave Plot', use_column_width=True)
st.image(generate_plot(), caption='Sine Wave Plot', use_column_width=True)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?