Attribute Error - cacheing - plotly

This is a new error in one of the latest versions of streamlit. If I remove @st.cache_data on the method it goes away. The error comes on my
st.plotly_chart(fig, use_container_width=True) calls. Its being thrown by the streamlit library.

AttributeError
Traceback:
File “/usr/local/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 600, in _run_script
exec(code, module.dict )
File “/cl-dashboard-internal/pages/27_Engagement_Improvement.py”, line 60, in
uic.funnel_change_by_language_chart(
File “/usr/local/lib/python3.12/site-packages/streamlit/runtime/caching/cache_utils.py”, line 165, in wrapper
return cached_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/site-packages/streamlit/runtime/caching/cache_utils.py”, line 196, in call
return self._get_or_create_cached_value(args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/site-packages/streamlit/runtime/caching/cache_utils.py”, line 221, in _get_or_create_cached_value
return self._handle_cache_miss(cache, value_key, func_args, func_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/site-packages/streamlit/runtime/caching/cache_utils.py”, line 277, in _handle_cache_miss
computed_value = self._info.func(*func_args, **func_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/cl-dashboard-internal/ui_components.py”, line 682, in funnel_change_by_language_chart
st.plotly_chart(fig, use_container_width=True)
File “/usr/local/lib/python3.12/site-packages/streamlit/runtime/metrics_util.py”, line 397, in wrapped_func
result = non_optional_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/site-packages/streamlit/elements/plotly_chart.py”, line 527, in plotly_chart
return self.dg._enqueue(“plotly_chart”, plotly_chart_proto)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/site-packages/streamlit/delta_generator.py”, line 486, in _enqueue
caching.save_element_message(
File “/usr/local/lib/python3.12/site-packages/streamlit/runtime/caching/init .py”, line 47, in save_element_message
CACHE_DATA_MESSAGE_REPLAY_CTX.save_element_message(
File “/usr/local/lib/python3.12/site-packages/streamlit/runtime/caching/cached_message_replay.py”, line 314, in save_element_message
raise AttributeError

What version and what method? And is the error message just AttributeError? That would be quite unusual.

streamlit 1.35.0

Yes, the error is just AttributeError as you can see in the stacktrace provided. The method:

st.plotly_chart(fig, use_container_width=True)

Can you please provide a small standalone script which demonstrates the error?

Not really as it requires a dataframe filled with query data. But I can provide the entire function:

@st.cache_data(ttl="1d", show_spinner=False)
def spend_by_country_map():

    if "df_campaigns" not in st.session_state:
        return pd.DataFrame()
    else:
        df_campaigns = st.session_state.df_campaigns

    df_campaigns = df_campaigns.groupby("country")["cost"].sum().round(2).reset_index()

    country_fig = px.choropleth(
        df_campaigns,
        locations="country",
        color="cost",
        color_continuous_scale=[
            [0, "rgb(166,206,227, 0.5)"],
            [0.05, "rgb(31,120,180,0.5)"],
            [0.1, "rgb(178,223,138,0.5)"],
            [0.3, "rgb(51,160,44,0.5)"],
            [0.6, "rgb(251,154,153,0.5)"],
            [1, "rgb(227,26,28,0.5)"],
        ],
        height=600,
        projection="natural earth",
        locationmode="country names",
        hover_data={
            "cost": ":$,.2f",
        },
    )

    country_fig.update_geos(fitbounds="locations")
    country_fig.update_layout(
        height=600,
        margin=dict(l=10, r=1, b=10, t=10, pad=4),
        geo=dict(bgcolor="rgba(0,0,0,0)"),
    )
    st.plotly_chart(country_fig)


Here is my attempt to have some fake data, and it works fine without an Attribute Error:

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

if "df_campaigns" not in st.session_state:
    st.session_state.df_campaigns = pd.DataFrame(
        {
            "country": [
                "United States",
                "China",
                "Japan",
                "Germany",
                "United Kingdom",
                "France",
            ],
            "cost": [1000000, 500000, 200000, 100000, 50000, 20000],
        }
    )


@st.cache_data(ttl="1d", show_spinner=False)
def spend_by_country_map():
    df_campaigns = st.session_state.df_campaigns

    df_campaigns = df_campaigns.groupby("country")["cost"].sum().round(2).reset_index()

    country_fig = px.choropleth(
        df_campaigns,
        locations="country",
        color="cost",
        color_continuous_scale=[
            [0, "rgb(166,206,227, 0.5)"],
            [0.05, "rgb(31,120,180,0.5)"],
            [0.1, "rgb(178,223,138,0.5)"],
            [0.3, "rgb(51,160,44,0.5)"],
            [0.6, "rgb(251,154,153,0.5)"],
            [1, "rgb(227,26,28,0.5)"],
        ],
        height=600,
        projection="natural earth",
        locationmode="country names",
        hover_data={
            "cost": ":$,.2f",
        },
    )

    country_fig.update_geos(fitbounds="locations")
    country_fig.update_layout(
        height=600,
        margin=dict(l=10, r=1, b=10, t=10, pad=4),
        geo=dict(bgcolor="rgba(0,0,0,0)"),
    )
    st.plotly_chart(country_fig)


spend_by_country_map()

Does that work for you? If it does, can you modify the example data so that it’s similar to the data you have?

Thank you for the script. It does not work for me, and throws the same error. When I remove
@st.cache_data(ttl=“1d”, show_spinner=False)

The error goes away.

And its not a local environment thing because it happens in Google Cloud Run as well as Streamlit Community Cloud.

Here it is on Community Cloud with cacheing turned off:
https://curiouslearning-dashboard.streamlit.app/Acquisition

And here it is on Google Cloud run with cacheing on (scroll to bottom)
https://cl-data-dashboard-ugk3streia-uw.a.run.app/Acquisition

Thanks so much! There appears to be a bug in streamlit 1.35 related to how plotly charts are now interactive (e.g. you can click on them and get information about what you clicked on). I’ve reported this bug here st.plotly_chart causes AtrributeError if inside of st.cache_data in streamlit 1.35 · Issue #8885 · streamlit/streamlit · GitHub, and for now an easy fix would be to either:

  1. Just cache the data itself, not the plotly chart
  2. Downgrade to streamlit 1.34
1 Like

There is a fix to this that will be released in streamlit 1.36

Awesome! Thanks so much for the update!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.