FAQ pickle to send objects from server to client (#1451)

This commit is contained in:
Hood Chatham 2021-04-12 12:34:02 -04:00 committed by GitHub
parent 8f3f6bda20
commit 6fd605e436
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -57,8 +57,8 @@ web-based notebook environment for literate scientific computing and
communication.
Iodide is no longer maintained. If you want to use Pyodide in an interactive
client-side notebook, see [Pyodide Notebook
environments](https://pyodide.org/en/0.17.0a2/project/related-projects.html#notebook-environements-ides-repls).
client-side notebook, see [Pyodide notebook
environments](https://pyodide.org/en/0.17.0a2/project/related-projects.html#notebook-environments-ides-repls).
## Contributing

View File

@ -101,7 +101,7 @@ if platform.system() == 'Emscripten':
This however will not work at build time (i.e. in a `setup.py`) due to the way
the Pyodide build system works. It first compiles packages with the host compiler
(e.g. gcc) and then re-runs the compilation commands with emsdk. So the `setup.py` is
never run inside the Pyodide environement.
never run inside the Pyodide environment.
To detect Pyodide, **at build time** use,
```python
@ -144,3 +144,26 @@ assert my_js_module.f(7) == 50
assert h(9) == 80
assert c == 2
```
## How can I send a Python object from my server to Pyodide?
The best way to do this is with pickle. If the version of Python used in the
server exactly matches the version of Python used in the client, then objects
that can be successfully pickled can be sent to the client and unpickled in
Pyodide. If the versions of Python are different then for instance sending AST
is unlikely to work since there are breaking changes to Python AST in most
Python minor versions.
Similarly when pickling Python objects defined in a Python package, the package
version needs to match exactly between the server and pyodide.
Generally, pickles are portable between architectures (here amd64 and wasm32).
The rare cases when they are not portable, for instance currently tree based
models in scikit-learn, can be considered as a bug in the upstream library.
```{admonition} Security Issues with pickle
:class: warning
Unpickling data is similar to `eval`. On any public-facing server it is a really
bad idea to unpickle any data sent from the client. For sending data from client
to server, try some other serialization format like JSON.
```