a button that adds a rectangle to a plotly plot
To upload files, please first save the app
import streamlit as st
import plotly.graph_objects as go
# Initialize the figure in session state if it doesn't exist
if 'fig' not in st.session_state:
st.session_state.fig = go.Figure()
st.session_state.fig.update_layout(
xaxis=dict(range=[0, 10]),
yaxis=dict(range=[0, 10]),
title="Click to add rectangles"
)
# Button to add rectangle
if st.button("Add Rectangle"):
# Add a rectangle with random position and size
import random
x0 = random.uniform(1, 8)
y0 = random.uniform(1, 8)
x1 = x0 + random.uniform(0.5, 2)
y1 = y0 + random.uniform(0.5, 2)
st.session_state.fig.add_shape(
type="rect",
x0=x0, y0=y0,
x1=x1, y1=y1,
line=dict(color="RoyalBlue"),
fillcolor="LightSkyBlue",
opacity=0.5,
)
# Display the plot
st.plotly_chart(st.session_state.fig, use_container_width=True)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?