Skip to content

Commit

Permalink
changed docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DerThorsten committed Feb 6, 2024
1 parent 43b58b0 commit 242f426
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/js_api_tour.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//

//
// %% Javascript API tour

//
// %% Instantiate the pyjs wasm module

importScripts("./pyjs_runtime_browser.js");
let pyjs = await createModule({});

packages_json_url = origin + "/lite/xeus/kernels/xpython/empack_env_meta.json"
package_tarballs_root_url = origin + "/lite/xeus/kernel_packages/"

await pyjs.bootstrap_from_empack_packed_environment(
packages_json_url,
package_tarballs_root_url
)


//
// %% Hello world

pyjs.eval("print('hello world')");

//
// %% Import a module

pyjs.exec(`
import numpy
print(numpy.random.rand(3))
`);
49 changes: 49 additions & 0 deletions examples/py_api_tour.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
A tour trough pyjs python API
==============================
Here we will show you some of the features of pyjs.
"""

import pyjs



# %%
# Accessing the the JavaScript global object
# ------------------------------------------
# The global object in javascript is accessible via `pyjs.js`.
# Since this example runs **not** in the main thread, but only
# in a worker thread, we can not acces the window object, but
# only whats available in the workers global scope / globalThis.
# We can for instance print the page origin like this:
#

pyjs.js.location.origin # equivalent to the javascript expression `location.origin` / `globalThis.location.origin`





# %%
# Create JavaScript functions on the fly
# --------------------------------------
#
# To create a javascript fuction like the following
#
# .. code-block:: JavaScript
#
# my_function = function(a, b) {
# return a + b;
# }
#
# we can do the following:

# define the function
js_function = pyjs.js.Function("a", "b", "return a + b")

# call the function
result = js_function(1, 2)
result

0 comments on commit 242f426

Please sign in to comment.