streamlit / streamlit

Streamlit — A faster way to build and share data apps.
https://streamlit.io
Apache License 2.0
35.19k stars 3.05k forks source link

Unable to persist camera view in 3d plotly charts #8882

Open lucasimi opened 4 months ago

lucasimi commented 4 months ago

Checklist

Summary

Before the upgrade of Streamlit to version 1.35.0 I could persist the camera view of a 3d plotly chart using the argument uirevision as a layout parameter. Since version 1.35, whenever I interact with a streamlit widget, the camera is reset.

Reproducible Code Example

import streamlit as st
import plotly.graph_objects as go
import numpy as np

st.title("3D Plot with Plotly in Streamlit")

st.sidebar.header("3D Plot Controls")
x_range = st.sidebar.slider("X-axis range", -10, 10, (-5, 5))
y_range = st.sidebar.slider("Y-axis range", -10, 10, (-5, 5))
z_func = st.sidebar.selectbox(
    "Z function",
    ("sin(x) * cos(y)", "cos(x) * sin(y)", "x^2 - y^2")
)

x = np.linspace(x_range[0], x_range[1], 100)
y = np.linspace(y_range[0], y_range[1], 100)
x, y = np.meshgrid(x, y)

if z_func == "sin(x) * cos(y)":
    z = np.sin(x) * np.cos(y)
elif z_func == "cos(x) * sin(y)":
    z = np.cos(x) * np.sin(y)
else:
    z = x**2 - y**2

fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])

# uirevision should enable the camera view to persist across user interactions
fig.update_layout(
    title=f"3D Plot of {z_func}",
    scene=dict(
        xaxis_title='X Axis',
        yaxis_title='Y Axis',
        zaxis_title='Z Axis',
    ),
    margin=dict(l=65, r=50, b=65, t=90),
    uirevision='const'
)

st.plotly_chart(fig, use_container_width=True)

Steps To Reproduce

  1. Run the app with streamlit 1.34.0 and see the expected behavior
  2. Run the app with stramlit 1.35.0 and see the camera reset at every user interaction

Expected Behavior

streamlit-app-2024-06-12-07-06-28.webm

Current Behavior

streamlit-app-2024-06-12-07-06-42.webm

Is this a regression?

Debug info

Additional Information

No response

github-actions[bot] commented 4 months ago

If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.

Your feedback helps us prioritize which bugs to investigate and address first.

Visits

lukasmasuch commented 4 months ago

@lucasimi Thanks for reporting this issue! I was able to reproduce this here. I think the root cause is the same as this issue: https://github.com/streamlit/streamlit/issues/8782

This is mostly expected with the new version since we added a stable identity to a plotly chart based on its parameters. Changing the parameter will create a new frontend instance for the plotly chart. This resolved various other issues with plotly, but negatively impacted this type of usecase of updating plotly charts. We should probably provide a way via the key parameter to keep the plotly chart identity stable.