(using_from_javascript)= # Using Pyodide from Javascript This document describes using Pyodide directly from Javascript. For information about using Pyodide from Iodide, see {ref}`using_from_iodide`. ## Startup To include Pyodide in your project you can use the following CDN URL, https://pyodide-cdn2.iodide.io/v0.15.0/full/pyodide.js You can also download a release from [Github releases](https://github.com/iodide-project/pyodide/releases) (or build it yourself), include its contents in your distribution, and import the `pyodide.js` file there from a ` Pyodide test page
Open your browser console to see pyodide output ``` ## Loading packages Only the Python standard library and `six` are available after importing Pyodide. To use other libraries, you'll need to load their package using `pyodide.loadPackage`. This downloads the file data over the network (as a `.data` and `.js` index file) and installs the files in the virtual filesystem. Packages can be loaded by name, for those included in the official pyodide repository (e.g. `pyodide.loadPackage('numpy')`). It is also possible to load packages from custom URLs (e.g. `pyodide.loadPackage('https://foo/bar/numpy.js')`), in which case the URL must end with `.js`. When you request a package from the official repository, all of that package's dependencies are also loaded. Dependency resolution is not yet implemented when loading packages from custom URLs. Multiple packages can also be loaded in a single call, ```js pyodide.loadPackage(['cycler', 'pytz']) ``` `pyodide.loadPackage` returns a `Promise`. ```javascript pyodide.loadPackage('matplotlib').then(() => { // matplotlib is now available }); ``` ## Alternative way to load packages and run Python code Alternatively you can run Python code without manually pre-loading packages. You can do this with {ref}`pyodide.runPythonAsync `) function, which will automatically download all packages that the code snippet imports. Note: although the function is called Async, it still blocks the main thread. To run Python code asynchronously see {ref}`using_from_webworker`. ## Alternative Example ```html

You can execute any Python code. Just enter something in the box below and click the button.



Output:
``` ## Accessing Python scope from JavaScript You can also access from JavaScript all functions and variables defined in Python using the {ref}`pyodide.globals `) object. For example, if you initialize the variable `x = numpy.ones([3,3])` in Python, you can access it from JavaScript in your browser's developer console as follows: `pyodide.globals.x`. The same goes for functions and imports. See {ref}`type_conversions` for more details. You can try it yourself in the browser console: ```js pyodide.globals.x // >>> [Float64Array(3), Float64Array(3), Float64Array(3)] // create the same 3x3 ndarray from js let x = pyodide.globals.numpy.ones(new Int32Array([3, 3])) // x >>> [Float64Array(3), Float64Array(3), Float64Array(3)] ``` Since you have full scope access, you can also re-assign new values or even JavaScript functions to variables, and create new ones from JavaScript: ```js // re-assign a new value to an existing variable pyodide.globals.x = 'x will be now string' // create a new js function that will be available from Python // this will show a browser alert if the function is called from Python pyodide.globals.alert = msg => alert(msg) // this new function will also be available in Python and will return the squared value. pyodide.globals.squer = x => x*x ``` Feel free to play around with the code using the browser console and the above example. ## Accessing JavaScript scope from Python The JavaScript scope can be accessed from Python using the `js` module (see {ref}`type_conversions_using_js_obj_from_py`). This module represents the gloabal object `window` that allows us to directly manipulate the DOM and access global variables and functions from Python. ```python import js div = js.document.createElement("div") div.innerHTML = "

This element was created from Python

" js.document.body.prepend(div) ``` See {ref}`serving_pyodide_packages` to distribute pyodide files locally.