@@ -6,12 +6,14 @@ import { keymap, ViewUpdate } from "@codemirror/view";
6
6
import { defaultKeymap } from "@codemirror/commands" ;
7
7
import { oneDarkTheme } from "@codemirror/theme-one-dark" ;
8
8
9
- import { pyodideLoaded , loadedEnvironments , componentDetailsNavOpen , currentComponentDetails } from '../stores' ;
9
+ import { pyodideLoaded , loadedEnvironments , componentDetailsNavOpen , currentComponentDetails , mode , addToScriptsQueue } from '../stores' ;
10
10
import { addClasses } from '../utils' ;
11
11
12
12
// Premise used to connect to the first available pyodide interpreter
13
13
let pyodideReadyPromise ;
14
14
let environments ;
15
+ let currentMode ;
16
+
15
17
pyodideLoaded . subscribe ( value => {
16
18
pyodideReadyPromise = value ;
17
19
} ) ;
@@ -24,6 +26,10 @@ componentDetailsNavOpen.subscribe(value => {
24
26
propertiesNavOpen = value ;
25
27
} ) ;
26
28
29
+ mode . subscribe ( value => {
30
+ currentMode = value ;
31
+ } ) ;
32
+
27
33
function createCmdHandler ( el ) {
28
34
// Creates a codemirror cmd handler that calls the el.evaluate when an event
29
35
// triggers that specific cmd
@@ -127,20 +133,31 @@ export class PyScript extends HTMLElement {
127
133
{ key : "source" , value : "self" }
128
134
] )
129
135
}
130
-
136
+
131
137
addClasses ( this . btnConfig , buttonClasses ) ;
132
138
addClasses ( this . btnConfig , [ "bg-blue-500" ] )
133
139
eDiv . appendChild ( this . btnConfig ) ;
134
-
135
- // Editor Div
136
- this . editorOut = document . createElement ( 'div' ) ;
137
- this . editorOut . classList . add ( "output" ) ;
138
- this . editorOut . hidden = true ;
139
140
140
141
mainDiv . appendChild ( eDiv ) ;
141
142
mainDiv . appendChild ( this . editorNode ) ;
142
- mainDiv . appendChild ( this . editorOut ) ;
143
- this . appendChild ( mainDiv ) ;
143
+
144
+ if ( this . hasAttribute ( 'target' ) ) {
145
+ this . editorOut = document . getElementById ( this . getAttribute ( 'target' ) ) ;
146
+ } else {
147
+ // Editor Output Div
148
+ this . editorOut = document . createElement ( 'div' ) ;
149
+ this . editorOut . classList . add ( "output" ) ;
150
+ this . editorOut . hidden = true ;
151
+
152
+ // add the output div id there's not target
153
+ mainDiv . appendChild ( this . editorOut ) ;
154
+ }
155
+
156
+ if ( currentMode == "edit" ) {
157
+ this . appendChild ( mainDiv ) ;
158
+ } else {
159
+ addToScriptsQueue ( this ) ;
160
+ }
144
161
145
162
console . log ( 'connected' ) ;
146
163
}
@@ -156,7 +173,14 @@ export class PyScript extends HTMLElement {
156
173
// debugger
157
174
try {
158
175
// @ts -ignore
159
- let output = pyodide . runPython ( this . editor . state . doc . toString ( ) ) ;
176
+ let source = this . editor . state . doc . toString ( ) ;
177
+ let output ;
178
+ if ( source . includes ( "asyncio" ) ) {
179
+ output = pyodide . runPythonAsync ( source ) ;
180
+ } else {
181
+ output = pyodide . runPython ( source ) ;
182
+ }
183
+
160
184
if ( output !== undefined ) {
161
185
this . addToOutput ( output ) ;
162
186
}
@@ -176,5 +200,3 @@ export class PyScript extends HTMLElement {
176
200
177
201
}
178
202
}
179
-
180
-
0 commit comments