Weird Plotly Express Bug…

Plotly 4.5.0 has always worked well for me but recently, I ran into this weird bug where it just crash out like a BAMF.

I wasn't even trying anything fancy, just an example code, on a BRAND NEW notebook: 

import plotly.express as px
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x='year', y='pop')
fig.show()

Then it puked its gut out more or less...


---------------------------------------------------------------------------
SyntaxError                               Traceback (most recent call last)
C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\express\_core.py in apply_default_cascade(args)
    748         # retrieve the actual template if we were given a name
--> 749         args["template"] = pio.templates[args["template"]]
    750     except Exception:

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\io\_templates.py in __getitem__(self, item)
     90                     template_dict = json.loads(template_str)
---> 91                     template = Template(template_dict)
     92 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\graph_objs\layout\__init__.py in __init__(self, arg, data, layout, **kwargs)
   9449         _v = arg.pop("data", None)
-> 9450         self["data"] = data if data is not None else _v
   9451         _v = arg.pop("layout", None)

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\basedatatypes.py in __setitem__(self, prop, value)
   3481             if isinstance(validator, CompoundValidator):
-> 3482                 self._set_compound_prop(prop, value)
   3483 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\basedatatypes.py in _set_compound_prop(self, prop, val)
   3835         validator = self._validators.get(prop)
-> 3836         val = validator.validate_coerce(val, skip_invalid=self._skip_invalid)
   3837 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v, skip_invalid)
   2443         elif isinstance(v, dict):
-> 2444             v = self.data_class(v, skip_invalid=skip_invalid)
   2445 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\graph_objs\layout\template\__init__.py in __init__(self, arg, area, barpolar, bar, box, candlestick, carpet, choroplethmapbox, choropleth, cone, contourcarpet, contour, densitymapbox, funnelarea, funnel, heatmapgl, heatmap, histogram2dcontour, histogram2d, histogram, image, indicator, isosurface, mesh3d, ohlc, parcats, parcoords, pie, pointcloud, sankey, scatter3d, scattercarpet, scattergeo, scattergl, scattermapbox, scatterpolargl, scatterpolar, scatter, scatterternary, splom, streamtube, sunburst, surface, table, treemap, violin, volume, waterfall, **kwargs)
   1582         _v = arg.pop("parcoords", None)
-> 1583         self["parcoords"] = parcoords if parcoords is not None else _v
   1584         _v = arg.pop("pie", None)

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\basedatatypes.py in __setitem__(self, prop, value)
   3485             elif isinstance(validator, (CompoundArrayValidator, BaseDataValidator)):
-> 3486                 self._set_array_prop(prop, value)
   3487 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\basedatatypes.py in _set_array_prop(self, prop, val)
   3909         validator = self._validators.get(prop)
-> 3910         val = validator.validate_coerce(val, skip_invalid=self._skip_invalid)
   3911 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v, skip_invalid)
   2531                 elif isinstance(v_el, dict):
-> 2532                     res.append(self.data_class(v_el, skip_invalid=skip_invalid))
   2533                 else:

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\graph_objs\__init__.py in __init__(self, arg, customdata, customdatasrc, dimensions, dimensiondefaults, domain, ids, idssrc, labelangle, labelfont, labelside, line, meta, metasrc, name, rangefont, stream, tickfont, uid, uirevision, visible, **kwargs)
  43792         _v = arg.pop("line", None)
> 43793         self["line"] = line if line is not None else _v
  43794         _v = arg.pop("meta", None)

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\basedatatypes.py in __setitem__(self, prop, value)
   3481             if isinstance(validator, CompoundValidator):
-> 3482                 self._set_compound_prop(prop, value)
   3483 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\basedatatypes.py in _set_compound_prop(self, prop, val)
   3835         validator = self._validators.get(prop)
-> 3836         val = validator.validate_coerce(val, skip_invalid=self._skip_invalid)
   3837 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v, skip_invalid)
   2443         elif isinstance(v, dict):
-> 2444             v = self.data_class(v, skip_invalid=skip_invalid)
   2445 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\graph_objs\parcoords\__init__.py in __init__(self, arg, autocolorscale, cauto, cmax, cmid, cmin, color, coloraxis, colorbar, colorscale, colorsrc, reversescale, showscale, **kwargs)
   1417         _v = arg.pop("colorbar", None)
-> 1418         self["colorbar"] = colorbar if colorbar is not None else _v
   1419         _v = arg.pop("colorscale", None)

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\basedatatypes.py in __setitem__(self, prop, value)
   3481             if isinstance(validator, CompoundValidator):
-> 3482                 self._set_compound_prop(prop, value)
   3483 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\basedatatypes.py in _set_compound_prop(self, prop, val)
   3835         validator = self._validators.get(prop)
-> 3836         val = validator.validate_coerce(val, skip_invalid=self._skip_invalid)
   3837 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v, skip_invalid)
   2443         elif isinstance(v, dict):
-> 2444             v = self.data_class(v, skip_invalid=skip_invalid)
   2445 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\graph_objs\parcoords\line\__init__.py in __init__(self, arg, bgcolor, bordercolor, borderwidth, dtick, exponentformat, len, lenmode, nticks, outlinecolor, outlinewidth, separatethousands, showexponent, showticklabels, showtickprefix, showticksuffix, thickness, thicknessmode, tick0, tickangle, tickcolor, tickfont, tickformat, tickformatstops, tickformatstopdefaults, ticklen, tickmode, tickprefix, ticks, ticksuffix, ticktext, ticktextsrc, tickvals, tickvalssrc, tickwidth, title, titlefont, titleside, x, xanchor, xpad, y, yanchor, ypad, **kwargs)
   1800         _v = arg.pop("tickfont", None)
-> 1801         self["tickfont"] = tickfont if tickfont is not None else _v
   1802         _v = arg.pop("tickformat", None)

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\basedatatypes.py in __setitem__(self, prop, value)
   3481             if isinstance(validator, CompoundValidator):
-> 3482                 self._set_compound_prop(prop, value)
   3483 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\basedatatypes.py in _set_compound_prop(self, prop, val)
   3835         validator = self._validators.get(prop)
-> 3836         val = validator.validate_coerce(val, skip_invalid=self._skip_invalid)
   3837 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\_plotly_utils\basevalidators.py in validate_coerce(self, v, skip_invalid)
   2440         if v is None:
-> 2441             v = self.data_class()
   2442 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\graph_objs\parcoords\line\colorbar\__init__.py in __init__(self, arg, color, family, size, **kwargs)
    694         # -----------------
--> 695         from plotly.validators.parcoords.line.colorbar import tickfont as v_tickfont
    696 

SyntaxError: invalid character in identifier (__init__.py, line 1)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-2-11d79a4bcb46> in <module>
      1 import plotly.express as px
      2 data_canada = px.data.gapminder().query("country == 'Canada'")
----> 3 fig = px.bar(data_canada, x='year', y='pop')
      4 fig.show()

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\express\_chart_types.py in bar(data_frame, x, y, color, facet_row, facet_col, facet_col_wrap, hover_name, hover_data, custom_data, text, error_x, error_x_minus, error_y, error_y_minus, animation_frame, animation_group, category_orders, labels, color_discrete_sequence, color_discrete_map, color_continuous_scale, range_color, color_continuous_midpoint, opacity, orientation, barmode, log_x, log_y, range_x, range_y, title, template, width, height)
    311         constructor=go.Bar,
    312         trace_patch=dict(orientation=orientation, textposition="auto"),
--> 313         layout_patch=dict(barmode=barmode),
    314     )
    315 

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\express\_core.py in make_figure(args, constructor, trace_patch, layout_patch)
   1321 
   1322 def make_figure(args, constructor, trace_patch={}, layout_patch={}):
-> 1323     apply_default_cascade(args)
   1324 
   1325     args, trace_specs, grouped_mappings, sizeref, show_colorbar = infer_config(

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\express\_core.py in apply_default_cascade(args)
    750     except Exception:
    751         # otherwise try to build a real template
--> 752         args["template"] = go.layout.Template(args["template"])
    753 
    754     # if colors not set explicitly or in px.defaults, defer to a template

C:\ProgramData\Anaconda3\envs\bengali.ai\lib\site-packages\plotly\graph_objs\layout\__init__.py in __init__(self, arg, data, layout, **kwargs)
   9429 The first argument to the plotly.graph_objs.layout.Template
   9430 constructor must be a dict or
-> 9431 an instance of plotly.graph_objs.layout.Template"""
   9432             )
   9433 

ValueError: The first argument to the plotly.graph_objs.layout.Template 
constructor must be a dict or 
an instance of plotly.graph_objs.layout.Template

I fixed the issue by pip uninstall and reinstall plotly 4.5.0.

Who says turn off and turn back on doesn’t work in software world ^o^.