2018-06-21 15:19:34 +00:00
|
|
|
# Using Pyodide from Iodide
|
|
|
|
|
|
|
|
This document describes using Pyodide inside Iodide. For information
|
|
|
|
about using Pyodide directly from Javascript, see [Using Pyodide from
|
|
|
|
Javascript](using_pyodide_from_javascript.md).
|
|
|
|
|
2019-01-17 21:03:30 +00:00
|
|
|
## Running basic Python
|
2018-06-21 15:19:34 +00:00
|
|
|
|
2019-01-17 21:03:30 +00:00
|
|
|
Create a Python chunk, by inserting a line like this:
|
2018-06-21 15:19:34 +00:00
|
|
|
|
2019-01-17 21:03:30 +00:00
|
|
|
```
|
|
|
|
%% py
|
2018-06-21 15:19:34 +00:00
|
|
|
```
|
|
|
|
|
2019-01-17 21:03:30 +00:00
|
|
|
Type some Python code into the chunk, and press Shift+Enter to evaluate it. If
|
|
|
|
the last clause in the cell is an expression, that expression is evaluated,
|
|
|
|
converted to Javascript and displayed in the console like all other output
|
2018-06-21 15:19:34 +00:00
|
|
|
in Javascript. See [type conversions](type_conversions.md) for more information
|
|
|
|
about how data types are converted between Python and Javascript.
|
|
|
|
|
|
|
|
```python
|
2019-01-17 21:03:30 +00:00
|
|
|
%% py
|
2018-06-21 15:19:34 +00:00
|
|
|
import sys
|
|
|
|
sys.version
|
|
|
|
```
|
|
|
|
|
|
|
|
## Loading packages
|
|
|
|
|
|
|
|
Only the Python standard library and `six` are available after importing
|
2019-01-17 21:03:30 +00:00
|
|
|
Pyodide. Other available libraries, such as `numpy` and `matplotlib` are loaded
|
|
|
|
on demand.
|
2018-08-29 08:40:10 +00:00
|
|
|
|
2019-01-17 21:03:30 +00:00
|
|
|
If you just want to use the versions of those libraries included with Pyodide,
|
|
|
|
all you need to do is import and start using them:
|
2018-06-21 15:19:34 +00:00
|
|
|
|
2019-01-17 21:03:30 +00:00
|
|
|
```
|
|
|
|
%% py
|
|
|
|
import numpy as np
|
|
|
|
np.arange(10)
|
2018-09-05 09:28:20 +00:00
|
|
|
```
|
|
|
|
|
2019-01-17 21:03:30 +00:00
|
|
|
For most uses, that is all you need to know.
|
2018-06-21 15:19:34 +00:00
|
|
|
|
2019-01-17 21:03:30 +00:00
|
|
|
However, if you want to use your own custom package or load a package from
|
|
|
|
another provider, you'll need to use the `pyodide.loadPackage` function from a
|
|
|
|
Javascript chunk. For example, to load a special distribution of Numpy from
|
|
|
|
`custom.com`:
|
|
|
|
|
|
|
|
```
|
|
|
|
%% js
|
|
|
|
pyodide.loadPackage('https://custom.com/numpy.js')
|
2018-06-21 15:19:34 +00:00
|
|
|
```
|
2019-01-17 21:03:30 +00:00
|
|
|
|
|
|
|
After doing that, the numpy you import from a Python chunk will be this special
|
|
|
|
version of Numpy.
|
2019-06-03 19:46:14 +00:00
|
|
|
|
|
|
|
## Using a local build of Pyodide with Iodide
|
|
|
|
|
|
|
|
You may want to build a local copy of Pyodide with some changes and test it
|
|
|
|
inside of Iodide.
|
|
|
|
|
|
|
|
By default, Iodide will use a copy of Pyodide deployed to Netlify. However, it
|
|
|
|
will use locally-installed copy of Pyodide if `USE_LOCAL_PYODIDE` is set.
|
|
|
|
|
|
|
|
Set that environment variable in your shell:
|
|
|
|
|
|
|
|
```
|
|
|
|
export USE_LOCAL_PYODIDE=1
|
|
|
|
```
|
|
|
|
|
|
|
|
Then follow the building and running instructions for Iodide as usual.
|
|
|
|
|
|
|
|
Next, build Pyodide using the regular instructions in `../README.md`. Copy the
|
|
|
|
contents of Pyodide's build directory to your Iodide checkout's `build/pyodide`
|
|
|
|
directory:
|
|
|
|
|
|
|
|
```
|
|
|
|
mkdir $IODIDE_CHECKOUT/build/pyodide
|
|
|
|
cp $PYODIDE_CHECKOUT/build/* $IODIDE_CHECKOUT/build/pyodide
|
|
|
|
```
|