coatless-quarto/pyodide

[Feature]: Custom environments

psychemedia opened this issue · 1 comments

Feature Description

I notice that you do some of the lifting to get matplotlib charts to work via JS in each rendered page:

    // Load the `matplotlib` package with necessary environment hook
    await mainPyodide.loadPackage("matplotlib");

    // Set the backend for matplotlib to be interactive.
    await mainPyodide.runPythonAsync(`
    import matplotlib
    matplotlib.use("module://matplotlib_pyodide.html5_canvas_backend")
    from matplotlib import pyplot as plt
    `);

I wonder if it would also be useful to allow config either globally or locally that would allow a user to specify in header or _quarto.yml things like:

- pyodide-pip
  - pandas

which would create something like:

await mainPyodide.loadPackage("pandas");

or optionally something like:

- pyodide-pip
  - pandas, ["import pandas as pd", "pd.options.display.max_rows = 5"]

to generate:

    await mainPyodide.loadPackage("pandas");

    await mainPyodide.runPythonAsync(`
       import pandas as pd
       pd.options.display.max_rows = 5
    `);

Alternatively, keep pyodide-pip as a simple list and then allow:

- pyodide-init
  - import pandas as pd
  - pd.options.display.max_rows = 5
  - etc

On the first request regarding packages, the plan here is to allow for package loading via packages key, e.g.

pyodide: 
  packages: 
    - package: 'pkg1'
    - package: 'pkg2'
       alias: pd
    - package: 'https://example.com/files/pkg3-2.0.0-py2.py3-none-any.whl'

Packages would be installed via:

import micropip

for pkg in {{packages}}: 
  micropip.install(
      pkg
  )

Then imported using:

import {{pkg}}

or

import {{pkg}} as {{alias}}

For the second request regarding injecting the setup code before the cells are unlocked, we'd likely want to add an include-script-header designation that runs after all packages are installed, e.g.

---
pyodide: 
  include-script-header: | 
    pd.options.display.max_rows = 5
---