Skip to content

Commit 9e9ef0c

Browse files
committed
Add docs for injectPython et al in notebook/README
1 parent a1d3f44 commit 9e9ef0c

File tree

1 file changed

+75
-15
lines changed

1 file changed

+75
-15
lines changed

wasm/notebook/README.md

+75-15
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
The RustPython Notebook is a **toy** notebook inspired by the now inactive Iodide project ([https://alpha.iodide.io/](https://alpha.iodide.io/)).
44

5-
Here is how it looks like:
5+
Here is how it looks like:
66

77
![notebook](./screenshot.png)
88

9-
You can use the notebook to experiment with using Python and Javascript in the browser together.
9+
You can use the notebook to experiment with using Python and Javascript in the browser together.
1010

1111
The main use case is for scientific communication where you can have:
12-
- text or thesis in markdown,
13-
- math with Tex,
14-
- a model or analysis written in python,
15-
- a user interface and interactive visualization with JS.
12+
13+
- text or thesis in markdown,
14+
- math with Tex,
15+
- a model or analysis written in python,
16+
- a user interface and interactive visualization with JS.
1617

1718
The Notebook loads python in your browser (so you don't have to install it) then let yous play with those languages.
1819

@@ -24,18 +25,77 @@ To read more about the reasoning behind certain features, check the blog on [htt
2425

2526
Sample notebooks are under `snippets`
2627

27-
- `snippets/python-markdown-math.txt`: python, markdown and math
28-
- `snippets/python-js.txt`, adds javascript
29-
- `snippets/python-js-css-md/` adds styling with css in separate, more organized files.
28+
- `snippets/python-markdown-math.txt`: python, markdown and math
29+
- `snippets/python-js.txt`, adds javascript
30+
- `snippets/python-js-css-md/` adds styling with css in separate, more organized files.
3031

3132
## How to use
3233

33-
- Run locally with `npm run dev`
34-
- Build with `npm run dist`
34+
- Run locally with `npm run dev`
35+
- Build with `npm run dist`
36+
37+
## JS API
38+
39+
```typescript
40+
// adds `values[name]` to the python scope under `name`
41+
function injectPython(values: { [name: string]: PythonValue });
42+
type PythonValue =
43+
// null -> None
44+
| null
45+
| undefined
46+
// n -> int(n) if Number.isInteger(n) else float(n)
47+
| number
48+
// s -> str(s)
49+
| string
50+
// typedArray -> bytes(typedArray)
51+
| Uint8Array
52+
// arr -> list(arr)
53+
| Array<PythonValue>
54+
// obj -> dict(Object.entries(obj))
55+
| { [k: string]: PythonValue }
56+
// js callback in python: positional args are passed as
57+
// arguments, kwargs is the `this` variable
58+
// f -> lambda *args, **kwargs: f.apply(kwargs, args)
59+
//
60+
// python callback in js: pass the positional args an array and
61+
// kwargs as an object
62+
// f -> (args, kwargs) => f(*args, **kwargs)
63+
| Function;
64+
65+
// appends an element to the notebook
66+
function pushNotebook(el: HTMLElement);
67+
68+
// find and displays the traceback of a python error from a callback
69+
function handlePyError(err: any);
70+
// e.g.
71+
try {
72+
pyCb([], {});
73+
} catch (err) {
74+
handlePyError(err);
75+
}
76+
```
77+
78+
`injectPython` demo:
79+
80+
```js
81+
injectPython({
82+
foo(a, b) {
83+
console.log(a);
84+
if (this.x != null) {
85+
console.log(`got kwarg! x=${this.x}`);
86+
}
87+
return (y) => y + 1 + b;
88+
},
89+
});
90+
```
91+
92+
```py
93+
adder = foo("hy from python", 3, x=[1, 2, 3])
94+
assert adder(5) == 9
95+
```
3596

3697
## Wish list / TO DO
3798

38-
- Better javascript support
39-
- Collaborative peer-to-peer editing with WebRTC. Think Google Doc or Etherpad editing but for code in the browser
40-
- `%%load` command for dynamically adding javascript libraries or css framework
41-
- Clean up and organize the code. Seriously rethink if we want to make it more than a toy.
99+
- Collaborative peer-to-peer editing with WebRTC. Think Google Doc or Etherpad editing but for code in the browser
100+
- `%%load` command for dynamically adding javascript libraries or css framework
101+
- Clean up and organize the code. Seriously rethink if we want to make it more than a toy.

0 commit comments

Comments
 (0)