2018-09-20 13:20:38 +00:00
|
|
|
# API Reference
|
|
|
|
|
|
|
|
*pyodide version 0.1.0*
|
|
|
|
|
|
|
|
Backward compatibility of the API is not guaranteed at this point.
|
|
|
|
|
|
|
|
|
|
|
|
## Python API
|
|
|
|
|
|
|
|
|
|
|
|
### pyodide.open_url(url)
|
|
|
|
|
|
|
|
Fetches a given *url* and returns a `io.StringIO` to access its contents.
|
|
|
|
|
|
|
|
*Parameters*
|
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|-------|------|-----------------|
|
|
|
|
| *url* | str | the URL to open |
|
|
|
|
|
|
|
|
|
|
|
|
*Returns*
|
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
A `io.StringIO` object with the URL contents./
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
### pyodide.eval_code(code, ns)
|
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
Runs a string of code. The last part of the string may be an expression, in which case, its value is returned.
|
|
|
|
|
|
|
|
This function may be overridden to change how `pyodide.runPython` interprets code, for example to perform
|
|
|
|
some preprocessing on the Python code first.
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
*Parameters*
|
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|--------|-------|-----------------------|
|
|
|
|
| *code* | str | the code to evaluate |
|
|
|
|
| *ns* | dict | evaluation name space |
|
|
|
|
|
|
|
|
|
|
|
|
*Returns*
|
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
Either the resulting object or `None`.
|
2018-09-20 13:20:38 +00:00
|
|
|
|
2019-02-25 13:32:03 +00:00
|
|
|
### pyodide.as_nested_list(obj)
|
|
|
|
|
|
|
|
Converts Javascript nested arrays to Python nested lists. This conversion can not
|
|
|
|
be performed automatically, because Javascript Arrays and Objects can be combined
|
|
|
|
in ways that are ambiguous.
|
|
|
|
|
|
|
|
*Parameters*
|
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|--------|-------|-----------------------|
|
|
|
|
| *obj* | JS Object | The object to convert |
|
|
|
|
|
|
|
|
*Returns*
|
|
|
|
|
|
|
|
The object as nested Python lists.
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
## Javascript API
|
|
|
|
|
2020-02-11 13:42:15 +00:00
|
|
|
### pyodide.loadPackage(names, messageCallback, errorCallback)
|
2018-09-20 13:20:38 +00:00
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
Load a package or a list of packages over the network.
|
2018-09-20 13:20:38 +00:00
|
|
|
|
2018-10-09 22:47:24 +00:00
|
|
|
This makes the files for the package available in the virtual filesystem.
|
2018-09-20 14:50:31 +00:00
|
|
|
The package needs to be imported from Python before it can be used.
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
*Parameters*
|
|
|
|
|
2018-10-09 22:47:24 +00:00
|
|
|
| name | type | description |
|
|
|
|
|-------------------|-----------------|---------------------------------------|
|
|
|
|
| *names* | {String, Array} | package name, or URL. Can be either a single element, or an array. |
|
|
|
|
| *messageCallback* | function | A callback, called with progress messages. (optional) |
|
2020-02-11 13:42:15 +00:00
|
|
|
| *errorCallback* | function | A callback, called with error/warning messages. (optional) |
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
*Returns*
|
|
|
|
|
2018-10-09 22:47:24 +00:00
|
|
|
Loading is asynchronous, therefore, this returns a `Promise`.
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
|
2019-05-16 14:03:34 +00:00
|
|
|
### pyodide.loadedPackages
|
2018-09-20 13:20:38 +00:00
|
|
|
|
2020-02-11 13:42:15 +00:00
|
|
|
`Object` with loaded packages.
|
2018-09-20 13:20:38 +00:00
|
|
|
|
2019-05-16 14:03:34 +00:00
|
|
|
Use `Object.keys(pyodide.loadedPackages)` to access the names of the
|
|
|
|
loaded packages, and `pyodide.loadedPackages[package_name]` to access
|
2018-09-20 13:20:38 +00:00
|
|
|
install location for a particular `package_name`.
|
|
|
|
|
|
|
|
### pyodide.pyimport(name)
|
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
Access a Python object from Javascript. The object must be in the global Python namespace.
|
|
|
|
|
|
|
|
For example, to access the `foo` Python object from Javascript:
|
2018-09-20 13:20:38 +00:00
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
`var foo = pyodide.pyimport('foo')`
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
*Parameters*
|
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
| name | type | description |
|
|
|
|
|---------|--------|----------------------|
|
|
|
|
| *names* | String | Python variable name |
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
*Returns*
|
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|-----------|---------|---------------------------------------|
|
2018-09-20 14:50:31 +00:00
|
|
|
| *object* | *any* | If one of the basic types (string, |
|
|
|
|
| | | number, boolean, array, object), the |
|
|
|
|
| | | Python object is converted to |
|
|
|
|
| | | Javascript and returned. For other |
|
|
|
|
| | | types, a Proxy object to the Python |
|
|
|
|
| | | object is returned. |
|
2018-09-20 13:20:38 +00:00
|
|
|
|
2018-12-19 21:28:39 +00:00
|
|
|
### pyodide.globals
|
|
|
|
|
|
|
|
An object whose attributes are members of the Python global namespace. This is a
|
|
|
|
more convenient alternative to `pyodide.pyimport`.
|
|
|
|
|
|
|
|
For example, to access the `foo` Python object from Javascript:
|
|
|
|
|
|
|
|
`pyodide.globals.foo`
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
### pyodide.repr(obj)
|
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
Gets the Python's string representation of an object.
|
|
|
|
|
|
|
|
This is equivalent to calling `repr(obj)` in Python.
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
*Parameters*
|
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|---------|--------|---------------------|
|
2018-09-20 14:50:31 +00:00
|
|
|
| *obj* | *any* | Input object |
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
*Returns*
|
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------------|---------|-------------------------------------------|
|
2018-09-20 14:50:31 +00:00
|
|
|
| *str_repr* | String | String representation of the input object |
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
### pyodide.runPython(code)
|
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
Runs a string of code. The last part of the string may be an expression, in which case, its value is returned.
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
*Parameters*
|
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|---------|--------|--------------------------------|
|
2018-09-20 14:50:31 +00:00
|
|
|
| *code* | String | Python code to evaluate |
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
*Returns*
|
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
| name | type | description |
|
|
|
|
|------------|---------|---------------------------------|
|
|
|
|
| *jsresult* | *any* | Result, converted to Javascript |
|
2018-09-20 13:20:38 +00:00
|
|
|
|
|
|
|
|
2020-02-11 13:42:15 +00:00
|
|
|
### pyodide.runPythonAsync(code, messageCallback, errorCallback)
|
2018-10-09 22:47:24 +00:00
|
|
|
|
|
|
|
Runs Python code, possibly asynchronously loading any known packages that the code
|
|
|
|
chunk imports.
|
|
|
|
|
|
|
|
For example, given the following code chunk
|
|
|
|
|
|
|
|
```python
|
|
|
|
import numpy as np
|
|
|
|
x = np.array([1, 2, 3])
|
|
|
|
```
|
|
|
|
|
|
|
|
pyodide will first call `pyodide.loadPackage(['numpy'])`, and then run the code
|
2019-04-21 12:38:30 +00:00
|
|
|
chunk, returning the result. Since package fetching must happen asynchronously,
|
2018-10-09 22:47:24 +00:00
|
|
|
this function returns a `Promise` which resolves to the output. For example, to
|
|
|
|
use:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
pyodide.runPythonAsync(code, messageCallback)
|
|
|
|
.then((output) => handleOutput(output))
|
|
|
|
```
|
|
|
|
|
|
|
|
*Parameters*
|
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|-------------------|----------|--------------------------------|
|
|
|
|
| *code* | String | Python code to evaluate |
|
2020-02-11 13:42:15 +00:00
|
|
|
| *messageCallback* | function | A callback, called with progress messages. (optional) |
|
|
|
|
| *errorCallback* | function | A callback, called with error/warning messages. (optional) |
|
2018-10-09 22:47:24 +00:00
|
|
|
|
|
|
|
*Returns*
|
|
|
|
|
|
|
|
| name | type | description |
|
|
|
|
|------------|---------|------------------------------------------|
|
|
|
|
| *result* | Promise | Resolves to the result of the code chunk |
|
|
|
|
|
|
|
|
|
2018-09-20 13:20:38 +00:00
|
|
|
### pyodide.version()
|
|
|
|
|
|
|
|
Returns the pyodide version.
|
|
|
|
|
2018-12-19 21:28:39 +00:00
|
|
|
It can be either the exact release version (e.g. `0.1.0`), or
|
2018-09-20 13:20:38 +00:00
|
|
|
the latest release version followed by the number of commits since, and
|
|
|
|
the git hash of the current commit (e.g. `0.1.0-1-bd84646`).
|
|
|
|
|
|
|
|
*Parameters*
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
*Returns*
|
|
|
|
|
2018-09-20 14:50:31 +00:00
|
|
|
| name | type | description |
|
|
|
|
|-----------|--------|------------------------|
|
|
|
|
| *version* | String | Pyodide version string |
|