2
2
3
3
The RustPython Notebook is a ** toy** notebook inspired by the now inactive Iodide project ([ https://alpha.iodide.io/ ] ( https://alpha.iodide.io/ ) ).
4
4
5
- Here is how it looks like:
5
+ Here is how it looks like:
6
6
7
7
![ notebook] ( ./screenshot.png )
8
8
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.
10
10
11
11
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.
16
17
17
18
The Notebook loads python in your browser (so you don't have to install it) then let yous play with those languages.
18
19
@@ -24,18 +25,77 @@ To read more about the reasoning behind certain features, check the blog on [htt
24
25
25
26
Sample notebooks are under ` snippets `
26
27
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.
30
31
31
32
## How to use
32
33
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
+ ```
35
96
36
97
## Wish list / TO DO
37
98
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