|
| 1 | +# Use Python |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/usepython) |
| 4 | + |
| 5 | +A Python scripts runner composable. Run Python scripts in a [Pyodide](https://github.com/pyodide/pyodide) service worker |
| 6 | + |
| 7 | +## Install |
| 8 | + |
| 9 | +### As a package |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install usepython |
| 13 | +# or |
| 14 | +yarn add usepython |
| 15 | +``` |
| 16 | + |
| 17 | +Then use it: |
| 18 | + |
| 19 | +```ts |
| 20 | +import { usePython } from "usepython"; |
| 21 | + |
| 22 | +const py = usePython(); |
| 23 | +``` |
| 24 | + |
| 25 | +### As script src |
| 26 | + |
| 27 | +```html |
| 28 | + < script src= "https://unpkg.com/[email protected]/dist/py.min.js"></ script> |
| 29 | + <script> |
| 30 | + const py = $py.usePython(); |
| 31 | + </script> |
| 32 | +``` |
| 33 | + |
| 34 | +### As script type module |
| 35 | + |
| 36 | +```html |
| 37 | + <script type="module"> |
| 38 | + import { usePython } from "https://unpkg.com/[email protected]/dist/py.esm.js"; |
| 39 | + const py = usePython(); |
| 40 | + </script> |
| 41 | +``` |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +### Load the runtime |
| 46 | + |
| 47 | +Load the Python runtime: |
| 48 | + |
| 49 | +```ts |
| 50 | +await py.load() |
| 51 | +``` |
| 52 | + |
| 53 | +Listen to the install log: |
| 54 | + |
| 55 | +```ts |
| 56 | +const unbindInstallLog = py.installLog.listen((val) => { |
| 57 | + console.log(`Installing Python, stage ${val.stage}: ${val.msg}`) |
| 58 | +}) |
| 59 | +await py.load(); |
| 60 | +unbindInstallLog(); |
| 61 | +``` |
| 62 | + |
| 63 | +### Load libraries |
| 64 | + |
| 65 | +It is possible to install some Python packages: either [packages built for Pyodide](https://pyodide.org/en/stable/usage/packages-in-pyodide.html#packages-in-pyodide), standard pip packages that will be installed with Micropip, or custom wheels |
| 66 | + |
| 67 | +```ts |
| 68 | +const wheel = '/acustomwheel-0.0.1-py3-none-any.whl'; |
| 69 | +const pyodideLibs = ['pandas', 'numpy', 'bokeh']; |
| 70 | +await py.load(pyodideLibs, ['altair', wheel, 'vega_datasets']) |
| 71 | +``` |
| 72 | + |
| 73 | +### Run Python code |
| 74 | + |
| 75 | +Run some Python code: |
| 76 | + |
| 77 | +```ts |
| 78 | +const script = `a=1 |
| 79 | +b=2 |
| 80 | +a+b` |
| 81 | +const { result, error } = await py.run("script1", script); |
| 82 | +``` |
| 83 | + |
| 84 | +The result is the last line of the script, just like a return value |
| 85 | + |
| 86 | +### Listen to stdout |
| 87 | + |
| 88 | +Listen to the Python stdout output: |
| 89 | + |
| 90 | +```ts |
| 91 | +const script = `print('ok from python')`; |
| 92 | +py.log.listen((val) => { |
| 93 | + console.log("LOG", val.stdOut) |
| 94 | + // val.stdErr is also available |
| 95 | +}); |
| 96 | +await py.run("script2", script); |
| 97 | +``` |
| 98 | + |
| 99 | +## Examples |
| 100 | + |
| 101 | +- [Script src](examples/umd/) |
| 102 | +- [Script module](examples/esm/) |
| 103 | +- [Vuejs](examples/vuejs/) |
0 commit comments