forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
43 lines (41 loc) · 1.31 KB
/
javascript.js
File metadata and controls
43 lines (41 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Javascript code goes here
injectPython({
// injectPython functions take the positional arguments as
// normal function args, and kwargs as the `this` variable
add_text_input() {
const input = document.createElement('input');
pushNotebook(input);
return () => input.value;
},
add_button(buttonText, cb) {
const do_button = (callback) => {
const btn = document.createElement('button');
btn.innerHTML = buttonText;
btn.addEventListener('click', () => {
try {
// python functions passed to js have a signature
// of ([args...], {kwargs...}) => any
callback([], {});
} catch (err) {
// puts the traceback in the error box
handlePyError(err);
}
});
pushNotebook(btn);
};
if (cb == null) {
// to allow using as a decorator
return do_button;
} else {
do_button(cb);
}
},
add_output() {
const resultDiv = document.createElement('div');
resultDiv.classList.add('result');
pushNotebook(resultDiv);
return (value) => {
resultDiv.innerHTML = value;
};
},
});