forked from emscripten-forge/pyjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43b58b0
commit 242f426
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |