1+ // %% [markdown]
2+ // # pyjs JavaScript API Tour
3+ // Welcome to the tour of the pyjs JavaScript API. This notebook demonstrates how to use the PYJS JavaScript API to run Python code in the browser.
4+
5+ // %% [markdown]
6+ // # Loading the pyjs module
7+
8+ // %% [code]
9+ // load the pyjs runtime by importing the pyjs_runtime_browser.js file
10+ // the url differs depending on the deployment
11+ importScripts ( "../../../../xeus/bin/pyjs_runtime_browser.js" ) ;
12+
13+ // the locateFile function is used to locate the wasm file
14+ // which sits next to the pyjs_runtime_browser.js file
15+ // in thism deployment
16+ let locateFile = function ( filename ) {
17+ if ( filename . endsWith ( 'pyjs_runtime_browser.wasm' ) ) {
18+ return `../../../../xeus/bin/pyjs_runtime_browser.wasm` ;
19+ }
20+ } ;
21+
22+ // the createModule function in from the pyjs runtime
23+ // is used to create the pyjs module
24+ let pyjs = await createModule ( { locateFile :locateFile } ) ;
25+
26+ // load the python packages (includung the python standard library)
27+ // from the empack environment
28+ packages_json_url = "../../../../xeus/kernels/xpython/empack_env_meta.json"
29+ package_tarballs_root_url = "../../../../xeus/kernel_packages/"
30+ await pyjs . bootstrap_from_empack_packed_environment (
31+ packages_json_url ,
32+ package_tarballs_root_url
33+ ) ;
34+
35+
36+ // %% [markdown]
37+ // # Evaluating Python expressions:
38+ // From now on, you can use the pyjs module to run python code.
39+ // Here we use "eval" to evaluate a python expression
40+
41+ // %% [code]
42+ pyjs . eval ( "print('hello world')" ) ;
43+
44+ // %% [markdown]
45+ // # Executing Python code
46+ // Here we execute a python code block using "exec" function
47+
48+ // %% [code]
49+ pyjs . exec ( `
50+ import numpy
51+ print(numpy.random.rand(3))
52+ ` ) ;
53+
54+ // %% [markdown]
55+ // # Executing Python code and returning the last expression
56+ // Here we execute a python code block using "exec" function and return the last expression.
57+
58+
59+ // %% [code]
60+ let rand_arr = pyjs . exec_eval ( `
61+ import numpy
62+ numpy.random.rand(2,4,3)
63+ ` ) ;
64+ rand_arr . delete ( )
65+
66+ // %% [markdown]
67+
68+ // # Using the pyobject class
69+ // When a python object is returned, it is wrapped in a pyobject class.
70+ // This class provides methods to interact with the python object.
71+ // Any created instance of the pyobject class needs to be deleted using the "delete" method.
72+
73+ // %% [code]
74+ // create a numpy array with [0,1,2,3] as value
75+ let arr = pyjs . exec_eval ( `
76+ import numpy
77+ numpy.arange(0,4)
78+ ` ) ;
79+
80+ // get the shape
81+ let arr_shape = arr . shape
82+
83+ // get the square function
84+ const square_func = pyjs . eval ( 'numpy.square' )
85+
86+ // any function call / __call__ like operator on the python side
87+ // is called via "py_call"
88+ const res = square_func . py_call ( arr )
89+
90+ // print the result
91+ console . log ( res )
92+
93+ // delete all the created pyobjects
94+ res . delete ( )
95+ square_func . delete ( )
96+ arr_shape . delete ( )
97+ arr . delete ( )
98+
99+ // %% [markdown]
100+ // # Type Conversion
101+ // pyjs provides methods to convert between JavaScript and Python types.
102+ // ## Explicit conversion
103+
104+ // %% [code]
105+ // python list to javascript array
106+ const py_list = pyjs . eval ( "[1,2,3]" )
107+ const js_arr = pyjs . to_js ( py_list )
108+ py_list . delete ( )
109+ console . log ( js_arr )
110+
111+ // python dict to js map
112+ const py_dict = pyjs . eval ( "dict(a=1, b='fobar')" )
113+ const js_map = pyjs . to_js ( py_dict )
114+ py_dict . delete ( )
115+
116+ // values
117+ console . log ( Array . from ( js_map . keys ( ) ) )
118+ // keys
119+ console . log ( Array . from ( js_map . values ( ) ) )
120+
121+ // %% [markdown]
122+ // ## Implicit conversion
123+ // Fundamental types are automatically converted between Python and JavaScript.
124+ // This includes numbers, strings, booleans and null.
125+
126+ // %% [code]
127+ // sum is a plain javascript number
128+ const sum = pyjs . eval ( "sum([i for i in range(0,101)])" )
129+ sum
130+
131+ // %% [code]
132+ // is_true is a plain javascript boolean
133+ const is_true = pyjs . eval ( "sum([i for i in range(0,101)]) == 5050" )
134+ is_true
135+
136+ // %% [code]
137+ // none will be undefined
138+ let none = pyjs . eval ( 'None' )
139+ console . log ( none )
140+
141+ // %% [markdown]
142+ // # Asynchronous execution
143+ // The pyjs module provides a way to run python code asynchronously using the "exec_async" function.
144+
145+ // %% [code]
146+ const py_code = `
147+ import asyncio
148+ await asyncio.sleep(2)
149+ sum([i for i in range(100)])
150+ `
151+ result = await pyjs . async_exec_eval ( py_code )
152+ console . log ( result ) ;
0 commit comments