Summary
When I update one of my inputs the plotly streamlit figure only partially updates:
- The title updates
- The hover labels updates
- The plot as a visual remains the same (does not update)
If I insert a fig.show()
before st.plotly_chart
I get the expected figure.
If
fig.show()
shows the correct figure andst.plotly_chart
does not, how can I go about debugging this?
Unfortunately, I’ve been unable to create a minimum working example (MWE) of this behaviour.
Steps to reproduce
Code snippet:
Despite trying for a while I cannot reproduce this in a MWE and it is not possible for me to share my entire project, here is something similar, but doesn’t exhibit the behaviour. My actual plot has far more points on it. The code plots multiple lines on a map with each having hover text and custom styling.
As this is not a valid MWE, I’m just asking for general debugging ideas
import streamlit as st
from plot import plot
import plotly.express as px
import plotly.graph_objs as go
import numpy as np
mapbox_token = open(".mapbox_token").read()
def plot(lat):
init_lat = 0
init_lon = 0
lon = 75
lats =[]
lons =[]
hover_names = []
lats.extend(np.linspace(init_lat, lat, 100))
lons.append(None)
lons.extend(np.linspace(init_lon, lon, 100))
lons.append(None)
hover_names.extend(['selected']*100)
hover_names.append(None)
lats.extend(np.linspace(20, 20, 100))
lons.append(None)
lats.extend(np.linspace(-20, -20, 100))
lons.append(None)
hover_names.extend(['default']*100)
hover_names.append(None)
_fig = go.Figure()
_fig.add_trace(go.Scattermapbox(
mode='lines',
lon=lons,
lat=lats,
line = {'width': 2, 'color': 'red'},
hoverinfo='text',
text=hover_names,
opacity=1
))
_fig.add_trace(go.Scattermapbox(
mode='lines',
lon=lats,
lat=lons,
line = {'width': 2, 'color': 'green'},
hoverinfo='text',
text=hover_names,
opacity=1
))
_fig.update_layout(title=lat, showlegend=False, height=1000,hoverlabel_align='left', margin={"r":0,"l":0,"b":0, 't':30})
_fig.update_mapboxes(zoom=1.9, accesstoken=mapbox_token)
_fig.show()
return _fig
st.set_page_config(layout="wide")
with st.form("Optional"):
lats = list(range(100))
lat = st.select_slider("lat",lats, value=lats[40])
submitted = st.form_submit_button("Submit")
fig = plot(lat)
st.plotly_chart(fig, use_container_width=True)
Debug info
- Streamlit version: 1.23.0,1.24.0,1.25.0 (have tried them all
- Python version: 3.11.4
- Mamba(Conda) and poetry
- OS version: macOS 13.5
- Browser version: Safari 16.6, Arc 1.4.1 (Chromium 116.0.5845.96)
Requirements file
python = ">=3.10,<3.13"
streamlit = "=1.25.0"
plotly = "=5.15.0"
dill = "=0.3.7"