Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep state of plotly charts on browser window resize #6324

Closed
3 of 5 tasks
sebwills opened this issue Mar 16, 2023 · 4 comments · Fixed by #8191
Closed
3 of 5 tasks

Keep state of plotly charts on browser window resize #6324

sebwills opened this issue Mar 16, 2023 · 4 comments · Fixed by #8191
Labels
feature:st.plotly_chart type:enhancement Requests for feature enhancements or new features

Comments

@sebwills
Copy link

sebwills commented Mar 16, 2023

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

If a user interacts with a plotly line chart in a streamlit dash (for example, zooming in, or hiding some series), then resizes the browser window (in a way that affects the rendering of the chart), the chart returns to its initial state.

Reproducible Code Example

Open in Streamlit Cloud

import pandas as pd
import streamlit as st
import plotly.express as px

st.set_page_config(layout="wide")  # Not required, but makes bug more obvious, since every resize then affects the plot

df = pd.DataFrame([[1, 2, 3], [4, 5, 6]])

st.plotly_chart(px.line(df),
                use_container_width=True  # Makes bug more obvious
                )

Steps To Reproduce

  1. Run the above as a streamlit app
  2. click on one of the legend entries in the plot to hide it, or use the zoom tool in the popup plotly toolbar
  3. resize your browser window, making it narrower or wider

Expected Behavior

Resizing the browser window should not undo changes made to the state of the plotly chart.

Current Behavior

Resizing the browser window undoes changes made interactively to the state of the plotly chart, returning it to default zoom, selected series, etc.

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • Streamlit version: 1.20.0
  • Python version: 3.9.9
  • Operating System: N/A
  • Browser: Chrome
  • Virtual environment: venv

Additional Information

No response

Are you willing to submit a PR?

  • Yes, I am willing to submit a PR!
@sebwills sebwills added status:needs-triage Has not been triaged by the Streamlit team type:bug Something isn't working labels Mar 16, 2023
@snehankekre
Copy link
Collaborator

Hi @sebwills 👋

Thank you for submitting this issue! IMO this is not a bug in st.plotly_chart but expected behavior. go.Layout() has a uirevision parameter that when set to any value preserves the state of the figure.

In your example, assign px.line(df) to fig and update the uirevision parameter like so to preserve the state of the figure while resizing the window:

import pandas as pd
import plotly.express as px

import streamlit as st

st.set_page_config(
    layout="wide"
)  # Not required, but makes bug more obvious, since every resize then affects the plot

df = pd.DataFrame([[1, 2, 3], [4, 5, 6]])

fig = px.line(df)
fig.update_layout({"uirevision": "foo"}, overwrite=True)

st.plotly_chart(fig, use_container_width=True)  # Makes bug more obvious

plotly-resize-state

Source: https://discuss.streamlit.io/t/cant-enter-values-without-updating-a-plotly-figure/28066/2

@LukasMasuch
Copy link
Collaborator

Thanks @snehankekre for investigating! uirevision seems to solve this 👍 Maybe we could mention this somewhere in the plotly_chart docs as a tip?

I'm relabeling this as enhancement since I believe we could potentially also make this work out of the box in Streamlit (e.g. automatically setting the uirevision value).

@LukasMasuch LukasMasuch added type:enhancement Requests for feature enhancements or new features feature:st.plotly_chart and removed type:bug Something isn't working status:needs-triage Has not been triaged by the Streamlit team labels Mar 17, 2023
@LukasMasuch LukasMasuch changed the title Plotly chart state resets when browser window resized Keep state of plotly charts on browser window resize Mar 17, 2023
@snehankekre
Copy link
Collaborator

snehankekre commented Mar 17, 2023

Good call, Lukas! I'll add the uirevision workaround to the st.plotly_chart API reference examples in the docs repo next week.

@sebwills
Copy link
Author

Thank you both! I confirm adding the suggested code works around the issue.

I do feel that this is unexpected behavior and as such it would make sense to incorporate the workaround into streamlit (automatically, not just in the docs) if you can. I can see it could make sense to reset the chart if the data it is plotting changes, but I can't imagine anyone would want or expect it to reset when you resize the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:st.plotly_chart type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants