(using_from_javascript)= # Using Pyodide from Javascript This document describes using Pyodide directly from Javascript. ## Startup To include Pyodide in your project you can use the following CDN URL, https://cdn.jsdelivr.net/pyodide/v0.16.1/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 ``` ## 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 {any}`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.get("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.runPython(`x=numpy.ones([3, 3])`); pyodide.globals.get("x"); // >>> [Float64Array(3), Float64Array(3), Float64Array(3)] // create the same 3x3 ndarray from js let x = pyodide.globals.get("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.set("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.set("alert", alert); // this new function will also be available in Python and will return the squared value. pyodide.globals.set("square", 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 global 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.