Repository: scikit-hep/pyhf

Issue:
Matplotlib broken in Pyodide demo in docs
In the docs https://pyhf.readthedocs.io/en/v0.7.0/, the Pyodide example is broken for me:
```pytb
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 3
      1 import piplite
      2 await piplite.install(["pyhf==0.7.0"])
----> 3 get_ipython().run_line_magic('matplotlib', 'inline')
      4 import pyhf

File /lib/python3.10/site-packages/IPython/core/interactiveshell.py:2369, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2367     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2368 with self.builtin_trap:
-> 2369     result = fn(*args, **kwargs)
   2371 # The code below prevents the output from being displayed
   2372 # when using magics with decodator @output_can_be_silenced
   2373 # when the last Python token in the expression is a ';'.
   2374 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File /lib/python3.10/site-packages/IPython/core/magics/pylab.py:99, in PylabMagics.matplotlib(self, line)
     97     print("Available matplotlib backends: %s" % backends_list)
     98 else:
---> 99     gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
    100     self._show_matplotlib_backend(args.gui, backend)

File /lib/python3.10/site-packages/IPython/core/interactiveshell.py:3540, in InteractiveShell.enable_matplotlib(self, gui)
   3519 def enable_matplotlib(self, gui=None):
   3520     """Enable interactive matplotlib and inline figure support.
   3521 
   3522     This takes the following steps:
   (...)
   3538         display figures inline.
   3539     """
-> 3540     from matplotlib_inline.backend_inline import configure_inline_support
   3542     from IPython.core import pylabtools as pt
   3543     gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)

File /lib/python3.10/site-packages/matplotlib_inline/__init__.py:1
----> 1 from . import backend_inline, config  # noqa
      2 __version__ = "0.1.6"  # noqa

File /lib/python3.10/site-packages/matplotlib_inline/backend_inline.py:6
      1 """A matplotlib backend for publishing figures via display_data"""
      3 # Copyright (c) IPython Development Team.
      4 # Distributed under the terms of the BSD 3-Clause License.
----> 6 import matplotlib
      7 from matplotlib import colors
      8 from matplotlib.backends import backend_agg

ModuleNotFoundError: The module 'matplotlib' is included in the Pyodide distribution, but it is not installed.
You can install it by calling:
  await micropip.install("matplotlib") in Python, or
  await pyodide.loadPackage("matplotlib") in JavaScript
See https://pyodide.org/en/stable/usage/loading-packages.html for more details.
```
It used to work previously, though I can not say for sure when it last worked for me. Running on MacOS (ARM), tried Firefox and Chrome (resulting in the above), while Safari seems stuck executing the import commands provided.


Discussion:
Ah that's unfortunate. Thanks for reporting @alexander-held. It is similarly broken for me. :( 

I'll have to dig into this over the weekend.
cc @henryiii given his involvement in Pyodide in general to see if he has any prior knowledge on what happened here.
When did it break? Guessing it was a Jupyterlite update? That was a little bit ago, though (something like https://jupyterlite.readthedocs.io/en/0.1.0-beta/changelog.html#b17) Also, It think a solution might be to include matplotlib in the list of things you install.
Might have been an update to the demo site, which I think you are using? https://github.com/jupyterlite/jupyterlite/commit/ef71996020ceb25cde6829f099740f50edfc297a was recent.
FYI, I'd probably recommend building Jupyterlite yourself, rather than using a demo site that is not stable. Something like https://github.com/henryiii/se-for-sci/blob/b97673d82361f1be50c1519efac2a1de50e1e157/noxfile.py#L8-L15, for example.
> It think a solution might be to include matplotlib in the list of things you install.

Yeah, that definitely works. 

Resolving patch:
diff --git a/docs/generate_jupyterlite_iframe.py b/docs/generate_jupyterlite_iframe.py
--- a/docs/generate_jupyterlite_iframe.py
+++ b/docs/generate_jupyterlite_iframe.py
@@ -4,7 +4,7 @@
 def main():
     code = """\
 import piplite
-await piplite.install(["pyhf==0.7.0"])
+await piplite.install(["pyhf==0.7.0", "matplotlib>=3.0.0"])
 %matplotlib inline
 import pyhf\
 """


Test patch: