Skip to content

Exporting Objects from Python to JS

In the same way that the 'js module' can be used to import JavaScript objects into Python, the same 'module' can be assigned to to create JavaScript objects from Python.

If we have some Python objects that we'd like to export as a JavaScript objects:

my_string = "This sure is a string"
my_list = [1,2,3]

def square(x):
    return x * x

class SimpleClass:
    def __init__(self, name):
        self.name = name

my_instance = SimpleClass("some name")

We can use the following Python syntax to create corresponding JavaScript objects:

import js

js.a_string = my_string
js.a_list = [1,2,3]
js.square_func_from_python = square

class SimpleClass:
    def __init__(self, name):
        self.name = name

my_instance = SimpleClass("some name")

js.some_instance = my_instance
js.some_class = SimpleClass

Notice that we can translate simple objects like strings, and increasingly more complex objects like lists, functions, class instances, and classes. Simple objects like ints, strs, and bools get converted to their corresponding JavaScript types; anything more complex appears in JavaScript as a proxy for the corresponding Python object. For a complete description of how objects are translated and proxied, see the Pyodide Documentation.