Summary
Ok, so the issue is the following: since version 1.15.2, Streamlit seems to override colours of any created plotly figure, which breaks its rendering when its not done via Streamlit itself (for example, fig.to_html()
)
Consider the following code:
import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
x_axis = list(range(4))*2
x_axis.sort()
data = np.array([["a", "b"]*4, x_axis, [2, 4, 3, 5, 7, 5, 8, 6]]).transpose()
df = pd.DataFrame(columns=["cat_value", "x", "y"], data=data)
fig = px.bar(df, x="x", y="y", color="cat_value", barmode="group")
for go_data in fig.data:
print(go_data["name"])
print(go_data["marker"]["color"])
st.plotly_chart(fig)
On Streamlit 1.15.0 this code would generate the following output in logs:
a
#636efa
b
#EF553B
and produce nice figure with blue and red bars. But if I update Streamlit to 1.15.2 or higher (pandas and plotly versions remain the same), figure will be the same, but text output will be the following:
a
#000001
b
#000002
which essentially means that if generated figure is to be rendered in any way other than st.plotly_chart
, all markers will be black. It seems that Streamlit overrides color of created figure and then swaps it for something else while rendering.
My question is: there any way to create a figure with proper colours while running a script from Streamlit? For example, I need to render it via to_html()
.
If there is no such way now, maybe it should be fixed?