Skip to content

Commit

Permalink
Add notes on external package installation (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
coatless authored Mar 1, 2024
1 parent afa49c4 commit a5da36d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ globalThis.qpyodideInstance = await import(
// Setup a namespace for global scoping
// await loadedPyodide.runPythonAsync("globalScope = {}");

// Add matplotlib
// Load the `micropip` package to allow installation of packages.
await mainPyodide.loadPackage("micropip");

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

// Set the backend for matplotlib to be interactive.
Expand Down
24 changes: 24 additions & 0 deletions docs/qpyodide-code-cell-demo.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@ import pandas as pd
df.Age
```

### Loading non-core Pyodide Python Packages

In the above example, everything just worked as `pandas` in available as part of [Pyodide's built-in packages](https://pyodide.org/en/stable/usage/packages-in-pyodide.html). However, if we need a package that is not part of the built-in list, then there either needs to be a pure Python wheel (no compiled code present) or a specially compiled version of the Package for Python.

In this case, we can install the `seaborn` package from PyPI with:

```{pyodide-python}
await micropip.install("seaborn")
```

Then, we have:

```{pyodide-python}
import seaborn as sns
import pandas as pd
data = np.random.multivariate_normal([0, 0], [[5, 2], [2, 2]], size=2000)
data = pd.DataFrame(data, columns=['x', 'y'])
sns.distplot(data['x'])
sns.distplot(data['y']);
```

## Graphing

We provide support for generating graphs through the interactive HTML5 backend, e.g. [`module://matplotlib_pyodide.html5_canvas_backend`](https://github.com/pyodide/matplotlib-pyodide). At the end of each graph call, you must include a `plt.show()` call for the graph to render.
Expand Down
17 changes: 15 additions & 2 deletions docs/qpyodide-faq.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,22 @@ To install the Quarto extension, please see our installation page.

### Can I use external libraries and packages in my Python cells?

Yes, you can use external libraries and packages within your Python cells. Pyodide includes a wide range of pre-installed libraries, and you can also install additional packages by (TODO).
Yes, you can use external libraries and packages within your Python cells. Pyodide includes a wide range of [pre-installed libraries](https://pyodide.org/en/stable/usage/packages-in-pyodide.html). These libraries can be downloaded and used on the fly once they are detected in an `import` statement.

<!-- using the standard `pip` syntax within your Python cells. -->
You can also install additional packages by using the `micropip` package loaded at the start of each Pyodide session. These packages can either be pure Python packages (denoted by `*py3-none-any.whl` on PyPI) or Python packages compiled for Pyodide (denoted by `*-cp310-cp310-emscripten_3_1_27_wasm32.whl` that require a specific Python and Emscripten versions). For instance, the following will download and install the [`seaborn`](https://seaborn.pydata.org/) visualization library.

```python
import micropip
micropip.install("seaborn")
```

Once done, please make sure to import the package:

```python
import seaborn as sns
```

More details can be found on Pyodide's [Loading package](https://pyodide.org/en/stable/usage/loading-packages.html#installing-wheels-from-arbitrary-urls) documentation.


## Sharing Documents
Expand Down

0 comments on commit a5da36d

Please sign in to comment.