1
1
import { assign , dedent } from "polyscript/exports" ;
2
2
3
- const invoke = ( name , args ) => `${ name } (code, ${ args . join ( ', ' ) } )` ;
3
+ const invoke = ( name , args ) => `${ name } (code, ${ args . join ( ", " ) } )` ;
4
4
5
5
export default ( options = { } ) => {
6
- const type = options . type || 'py' ;
7
- const args = options . persistent ? [ 'globals()' , '__locals__' ] : [ '{}' , '{}' ] ;
8
- const src = URL . createObjectURL ( new Blob ( [
9
- dedent ( `
6
+ const type = options . type || "py" ;
7
+ const args = options . persistent
8
+ ? [ "globals()" , "__locals__" ]
9
+ : [ "{}" , "{}" ] ;
10
+ const src = URL . createObjectURL (
11
+ new Blob ( [
12
+ dedent ( `
10
13
from pyscript import sync, config
11
14
__message__ = lambda e,v: f"\x1b[31m\x1b[1m{e.__name__}\x1b[0m: {v}"
12
15
__locals__ = {}
@@ -18,42 +21,44 @@ export default (options = {}) => {
18
21
else:
19
22
__error__ = lambda e: __message__(e.__class__, e.value)
20
23
def execute(code):
21
- try: return ${ invoke ( ' exec' , args ) } ;
24
+ try: return ${ invoke ( " exec" , args ) } ;
22
25
except Exception as e: print(__error__(e));
23
26
def evaluate(code):
24
- try: return ${ invoke ( ' eval' , args ) } ;
27
+ try: return ${ invoke ( " eval" , args ) } ;
25
28
except Exception as e: print(__error__(e));
26
29
sync.execute = execute
27
30
sync.evaluate = evaluate
28
- ` )
29
- ] ) ) ;
31
+ ` ) ,
32
+ ] ) ,
33
+ ) ;
30
34
31
- const script = assign ( document . createElement ( ' script' ) , { type, src } ) ;
35
+ const script = assign ( document . createElement ( " script" ) , { type, src } ) ;
32
36
33
- script . toggleAttribute ( 'worker' , true ) ;
34
- script . toggleAttribute ( 'terminal' , true ) ;
35
- if ( options . terminal ) script . setAttribute ( 'target' , options . terminal ) ;
36
- if ( options . config ) script . setAttribute ( 'config' , JSON . stringify ( options . config ) ) ;
37
+ script . toggleAttribute ( "worker" , true ) ;
38
+ script . toggleAttribute ( "terminal" , true ) ;
39
+ if ( options . terminal ) script . setAttribute ( "target" , options . terminal ) ;
40
+ if ( options . config )
41
+ script . setAttribute ( "config" , JSON . stringify ( options . config ) ) ;
37
42
38
- return new Promise ( resolve => {
39
- script . addEventListener ( `${ type } :done` , event => {
40
- event . stopPropagation ( ) ;
41
- URL . revokeObjectURL ( src ) ;
42
- const { xworker, process, terminal } = script ;
43
- const { execute, evaluate } = xworker . sync ;
44
- script . remove ( ) ;
45
- resolve ( {
46
- process,
47
- execute : code => execute ( dedent ( code ) ) ,
48
- evaluate : code => evaluate ( dedent ( code ) ) ,
49
- clear : ( ) => terminal . clear ( ) ,
50
- reset : ( ) => terminal . reset ( ) ,
51
- kill : ( ) => {
52
- xworker . terminate ( ) ;
53
- terminal . dispose ( ) ;
54
- } ,
55
- } ) ;
43
+ return new Promise ( ( resolve ) => {
44
+ script . addEventListener ( `${ type } :done` , ( event ) => {
45
+ event . stopPropagation ( ) ;
46
+ URL . revokeObjectURL ( src ) ;
47
+ const { xworker, process, terminal } = script ;
48
+ const { execute, evaluate } = xworker . sync ;
49
+ script . remove ( ) ;
50
+ resolve ( {
51
+ process,
52
+ execute : ( code ) => execute ( dedent ( code ) ) ,
53
+ evaluate : ( code ) => evaluate ( dedent ( code ) ) ,
54
+ clear : ( ) => terminal . clear ( ) ,
55
+ reset : ( ) => terminal . reset ( ) ,
56
+ kill : ( ) => {
57
+ xworker . terminate ( ) ;
58
+ terminal . dispose ( ) ;
59
+ } ,
60
+ } ) ;
61
+ } ) ;
62
+ document . body . append ( script ) ;
56
63
} ) ;
57
- document . body . append ( script ) ;
58
- } ) ;
59
64
} ;
0 commit comments