Hi, I have a slider that updates the value of a plotly plot (in this case a 3D cone plot). I would like the plot to remember the previous camera position when the slider is moved, however as the script is run top to bottom, it’s not clear to me how to pass this information “between runs”.
Thanks!
Hi @Oliver_Wang, welcome to the forum !
Client-side state is being tracked there but for now on the Streamlit roadmap it’s planned for somewhere around 2020.
In the meantime you can work with the st.cache gist to preserve info between runs. You can check a working example of this here.
Cheers,
Fanilo
Would you all be so kind as to provide me with a brief explanation on how to ensure that the ploty state is preserved? I can envision a way to store the variables and simply replot when a user returns to a given tab but this seems inefficient. Especially, if the data plots are dense.
Here is the test case I have been using to start with. Any advice would be appreciated.
import streamlit as st
import numpy as np
import plotly.graph_objects as go
#https://gist.github.com/tvst/036da038ab3e999a64497f42de966a92
import SessionState
st.title("Plotly State Test")
activities = ["Plot","Dummy","About"]
choice = st.sidebar.selectbox("Processing Tools",activities)
if choice == 'Plot':
st.subheader("Exploratory Data Analysis")
state = SessionState.get(position=0)
widget = st.empty()
if st.button('Increment position'):
state.position += 1
state.position = widget.slider('Position', 5, 100, state.position)
x = np.arange(state.position)
y = np.random.randn(state.position)
fig = go.Figure()
fig.add_trace(go.Scatter(x = x, y=y,
mode='lines',
name="Slider Test"))
st.plotly_chart(fig, use_container_width=True)
if choice == 'Dummy':
st.text("Dummy Setup")
if choice == 'About':
st.text("Help Please")
1 Like