Problems running notebook from reference/panes/Matplotlib.html in Panel - strange SyntaxError

I’ve got problems using matplotlib in Panel - somehow plots are visible in Jupyter notebook, but when doing ‘Preview with Panel’ no plot is show: the pane where the plot should be is empty.

Therefore I have tried to run examples from the Panel documentation, namely from Component Gallery

Panes
Matplotlib

I have downloaded the notebook from https://raw.githubusercontent.com/holoviz/panel/main/examples/reference/panes/Matplotlib.ipynb and ran all cells. Everything but the example in “Using the interactive Matplotlib backend” section shows correctly in the notebook interface, however when I try ‘Preview with Panel’, or run panel serve Matplotlib.ipynb --dev from the terminal (in JupyterLab) I get the following strange error:

2024-10-15 11:11:00,499 Uncaught exception GET /00-panel-Matplotlib (172.18.56.66)
HTTPServerRequest(protocol='http', host='158.75.112.151:5006', method='GET', uri='/Matplotlib', version='HTTP/1.1', remote_ip='172.18.56.66')
Traceback (most recent call last):
  File ".venv/lib/python3.12/site-packages/tornado/web.py", line 1790, in _execute
    result = await result
             ^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/panel/io/server.py", line 466, in get
    session = await self.get_session()
              ^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/panel/io/server.py", line 356, in get_session
    session = await super().get_session()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/bokeh/server/views/session_handler.py", line 145, in get_session
    session = await self.application_context.create_session_if_needed(session_id, self.request, token)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/bokeh/server/contexts.py", line 240, in create_session_if_needed
    self._application.initialize_document(doc)
  File ".venv/lib/python3.12/site-packages/panel/io/application.py", line 149, in initialize_document
    super().initialize_document(doc)
  File ".venv/lib/python3.12/site-packages/bokeh/application/application.py", line 190, in initialize_document
    h.modify_document(doc)
  File ".venv/lib/python3.12/site-packages/panel/io/handlers.py", line 668, in modify_document
    nodes = ast.parse(source, os.fspath(path))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/ast.py", line 52, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "notebooks/panel/Matplotlib.ipynb", line 190
    "    for (const name in js_exports) {\n",
SyntaxError: invalid syntax

When I examine the file, it turns out that this line comes from the very first cell, with the following code:

import panel as pn

pn.extension('ipywidgets')

When I look at the Matplotlib.ipynb file with less -N, this is what I see:

     53     {
     54      "data": {
     55       "application/javascript": [
     56        "(function(root) {\n",
     57        "  function now() {\n",
     58        "    return new Date();\n",
     59        "  }\n",
     60        "\n",
[...]
    188        "      document.head.appendChild(element);\n",
    189        "    }\n",
    190        "    for (const name in js_exports) {\n",
    191        "      const url = js_exports[name];\n",
    192        "      const escaped = encodeURI(url)\n",

Could you help me debug this problem?

List of relevant packages installed:

  • bokeh==3.6.0
  • holoviews==1.19.1
  • hvplot==0.11.0
  • ipykernel==6.29.5
  • ipympl==0.9.4
  • ipython==8.27.0
  • ipython-genutils==0.2.0
  • ipywidgets==8.1.5
  • ipywidgets_bokeh==1.6.0
  • jupyter-events==0.10.0
  • jupyter-lsp==2.2.5
  • jupyter-server-mathjax==0.2.6
  • jupyter_client==8.6.3
  • jupyter_core==5.7.2
  • jupyter_server==2.14.2
  • jupyter_server_terminals==0.5.3
  • jupyterlab==4.2.5
  • jupyterlab_pygments==0.3.0
  • jupyterlab_server==2.27.3
  • jupyterlab_widgets==3.0.13
  • matplotlib==3.9.2
  • matplotlib-inline==0.1.7
  • panel==1.5.2
  • pyviz_comms==3.0.3
  • watchfiles==0.24.0

What is strange that it is adding %matplotlib ipympl to the cell with code for “Using the interactive Matplotlib backend” that makes panel serve to fail.

Commenting it out makes it work.