2020-10-30 20:09:25 +00:00
|
|
|
(pypi)=
|
2019-05-03 17:43:31 +00:00
|
|
|
# Installing packages from PyPI
|
|
|
|
|
|
|
|
Pyodide has experimental support for installing pure Python wheels from PyPI.
|
|
|
|
|
|
|
|
For use in Iodide:
|
|
|
|
|
|
|
|
```
|
|
|
|
%% py
|
|
|
|
import micropip
|
|
|
|
micropip.install('snowballstemmer')
|
|
|
|
|
|
|
|
# Iodide implicitly waits for the promise to resolve when the packages have finished
|
|
|
|
# installing...
|
|
|
|
|
|
|
|
%% py
|
|
|
|
import snowballstemmer
|
2019-09-24 08:52:02 +00:00
|
|
|
stemmer = snowballstemmer.stemmer('english')
|
2019-05-03 17:43:31 +00:00
|
|
|
stemmer.stemWords('go goes going gone'.split())
|
|
|
|
```
|
|
|
|
|
|
|
|
For use outside of Iodide (just Python), you can use the `then` method on the
|
|
|
|
`Promise` that `micropip.install` returns to do work once the packages have
|
|
|
|
finished loading:
|
|
|
|
|
2020-05-11 08:06:33 +00:00
|
|
|
```py
|
2019-05-03 17:43:31 +00:00
|
|
|
def do_work(*args):
|
2020-05-08 21:22:33 +00:00
|
|
|
import snowballstemmer
|
|
|
|
stemmer = snowballstemmer.stemmer('english')
|
|
|
|
print(stemmer.stemWords('go goes going gone'.split()))
|
2019-05-03 17:43:31 +00:00
|
|
|
|
|
|
|
import micropip
|
|
|
|
micropip.install('snowballstemmer').then(do_work)
|
|
|
|
```
|
2020-05-08 21:22:33 +00:00
|
|
|
|
2020-05-11 08:30:25 +00:00
|
|
|
Micropip implements file integrity validation by checking the hash of the
|
|
|
|
downloaded wheel against pre-recorded hash digests from the PyPi JSON API.
|
2020-05-11 08:06:33 +00:00
|
|
|
|
2020-10-30 20:09:25 +00:00
|
|
|
(micropip-installing-from-arbitrary-urls)=
|
2020-05-11 08:06:33 +00:00
|
|
|
## Installing wheels from arbitrary URLs
|
|
|
|
|
|
|
|
Pure python wheels can also be installed from any URL with micropip,
|
|
|
|
```py
|
|
|
|
import micropip
|
|
|
|
micropip.install(
|
|
|
|
'https://example.com/files/snowballstemmer-2.0.0-py2.py3-none-any.whl'
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
The wheel name in the URL must follow [PEP 427 naming
|
|
|
|
convention](https://www.python.org/dev/peps/pep-0427/#file-format), which will
|
2020-05-11 08:34:34 +00:00
|
|
|
be the case if the wheels is made using standard python tools (`pip wheel`,
|
2020-05-11 08:06:33 +00:00
|
|
|
`setup.py bdist_wheel`).
|
|
|
|
|
2020-05-11 08:38:27 +00:00
|
|
|
All required dependencies need also to be previously installed with `micropip`
|
|
|
|
or `pyodide.loadPackage`.
|
|
|
|
|
2020-05-11 08:06:33 +00:00
|
|
|
The remote server must set Cross-Origin Resource Sharing (CORS) headers to
|
|
|
|
allow access. Otherwise, you can prepend a CORS proxy to the URL. Note however
|
|
|
|
that using third-party CORS proxies has security implications, particularly
|
|
|
|
since we are not able to check the file integrity, unlike with installs from
|
|
|
|
PyPi.
|
|
|
|
|
|
|
|
|
2020-05-08 21:22:33 +00:00
|
|
|
## Complete example
|
|
|
|
|
2020-10-30 20:09:25 +00:00
|
|
|
Adapting the setup from the section on {ref}`using_from_javascript`
|
|
|
|
a complete example would be,
|
2020-05-08 21:22:33 +00:00
|
|
|
|
|
|
|
```html
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
</head>
|
|
|
|
<body>
|
2020-05-20 16:58:43 +00:00
|
|
|
<script type="text/javascript">
|
|
|
|
// set the pyodide files URL (packages.json, pyodide.asm.data etc)
|
2020-05-20 19:11:50 +00:00
|
|
|
window.languagePluginUrl = 'https://pyodide-cdn2.iodide.io/v0.15.0/full/';
|
2020-05-20 16:58:43 +00:00
|
|
|
</script>
|
2020-05-20 19:11:50 +00:00
|
|
|
<script type="text/javascript" src="https://pyodide-cdn2.iodide.io/v0.15.0/full/pyodide.js"></script>
|
2020-05-08 21:22:33 +00:00
|
|
|
<script type="text/javascript">
|
|
|
|
pythonCode = `
|
|
|
|
def do_work(*args):
|
|
|
|
import snowballstemmer
|
|
|
|
stemmer = snowballstemmer.stemmer('english')
|
|
|
|
print(stemmer.stemWords('go goes going gone'.split()))
|
|
|
|
|
|
|
|
import micropip
|
|
|
|
micropip.install('snowballstemmer').then(do_work)
|
|
|
|
`
|
|
|
|
|
|
|
|
languagePluginLoader.then(() => {
|
|
|
|
return pyodide.loadPackage(['micropip'])
|
|
|
|
}).then(() => {
|
|
|
|
pyodide.runPython(pythonCode);
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
```
|