TypeError: unhashable type: 'set' in dropdown plotly python

303 views Asked by At

The following code works by itself but I have a "TypeError: unhashable type: 'set'" when it's in my plotly graph. Can you help me out with this ? I never worked with set or frozenset.

Thanks a lot !

code that works be itself :

updatemenus = []
for n, ecole in enumerate(liste_ecole):
  visible = [False]*len(liste_ecole)
  visible[n] = True
  temp_dict = dict(label = str(liste_ecole),
                     method = 'update',
                     args = [{"visible" : visible,
                             {"title"} : "Prévisions des effectifs pour {}".format(liste_ecole)}])
updatemenus.append(temp_dict)

code inside my plotly graph :

fig = go.Figure([
    go.Scatter(
        name='Effectif réel',
        x=df2['date_str'],
        y=df2['reel'],
        mode='markers+lines',
        marker=dict(color="#1e3d59"),
        line=dict(width=1),
        showlegend=True,
        text=df2['jour']
    ),
    go.Scatter(
        name='Prévision algorithme',
        x=df2['date_str'],
        y=df2['output'],
        mode='markers+lines',
        marker=dict(color="#ff6e40", size=4),
        line=dict(width=2),
        showlegend=True,
        text=df2['jour']
    )
])
fig.layout.plot_bgcolor = '#f5f0e1'


updatemenus = []
for n, ecole in enumerate(liste_ecole):
  visible = [False]*len(liste_ecole)
  visible[n] = True
  temp_dict = dict(label = str(liste_ecole),
                     method = 'update',
                     args = [{"visible" : visible,
                             {"title"} : "Prévisions des effectifs pour {}".format(liste_ecole)}]) **#THIS IS WHERE I GOT MY ERROR**
updatemenus.append(temp_dict)


fig.update_layout(
    updatemenus=list([dict(buttons= list_updatemenus)]),
    yaxis_title="Nombre de convives",
    hovermode="x",

    direction="down",
    pad={"r": 10, "t": 10},
    showactive=True,
    x=1.02,
    xanchor="left",
    y=0.75,
    yanchor="top"
    )
                 
fig.update_xaxes(tickformat='%d-%b-%Y')    
fig.update_xaxes(rangeslider_visible=True)

fig.show()
1

There are 1 answers

0
saaj On

This error has nothing to do with Plotly. You can't have a set (literal) as a key in a dict because a set isn't hashable.

In [1]: {
   ...:     "visible" : ...,
   ...:     {"title"} : "Prévisions des effectifs pour {}".format(...)
   ...: }
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-24fea1201dd1> in <module>
      1 {
      2     "visible" : ...,
----> 3     {"title"} : "Prévisions des effectifs pour {}".format(...)
      4 }

TypeError: unhashable type: 'set'

In [2]: type({"title"})
Out[2]: set

And you don't need a set there, so just replace {"title"} with "title" (a hashable str).