@@ -8,6 +8,7 @@ import { oneDarkTheme } from "@codemirror/theme-one-dark";
8
8
9
9
import { pyodideLoaded , loadedEnvironments , componentDetailsNavOpen , currentComponentDetails , mode , addToScriptsQueue , addInitializer , addPostInitializer } from '../stores' ;
10
10
import { addClasses } from '../utils' ;
11
+ import { BaseEvalElement } from './base' ;
11
12
12
13
// Premise used to connect to the first available pyodide interpreter
13
14
let pyodideReadyPromise ;
@@ -50,6 +51,8 @@ type PyodideInterface = {
50
51
registerJsModule ( name : string , module : object ) : void
51
52
}
52
53
54
+ // TODO: This should be used as base for generic scripts that need exectutoin
55
+ // from PyScript to initializers, etc...
53
56
class Script {
54
57
source : string ;
55
58
state : string ;
@@ -90,30 +93,13 @@ class Script {
90
93
}
91
94
}
92
95
93
- export class PyScript extends HTMLElement {
94
- shadow : ShadowRoot ;
95
- wrapper : HTMLElement ;
96
- editor : EditorView ;
97
- editorNode : HTMLElement ;
98
- code : string ;
99
- cm : any ;
100
- btnConfig : HTMLElement ;
101
- btnRun : HTMLElement ;
102
- editorOut : HTMLElement ; //HTMLTextAreaElement;
103
- source : string ;
96
+ export class PyScript extends BaseEvalElement {
104
97
// editorState: EditorState;
105
98
106
99
constructor ( ) {
107
100
super ( ) ;
108
101
109
- // attach shadow so we can preserve the element original innerHtml content
110
- this . shadow = this . attachShadow ( { mode : 'open' } ) ;
111
-
112
- this . wrapper = document . createElement ( 'slot' ) ;
113
-
114
102
// add an extra div where we can attach the codemirror editor
115
- this . editorNode = document . createElement ( 'div' ) ;
116
- addClasses ( this . editorNode , [ "editor-box" ] )
117
103
this . shadow . appendChild ( this . wrapper ) ;
118
104
}
119
105
@@ -140,11 +126,6 @@ export class PyScript extends HTMLElement {
140
126
]
141
127
} )
142
128
143
- this . editor = new EditorView ( {
144
- state : startState ,
145
- parent : this . editorNode
146
- } )
147
-
148
129
let mainDiv = document . createElement ( 'div' ) ;
149
130
addClasses ( mainDiv , [ "parentBox" , "flex" , "flex-col" , "border-4" , "border-dashed" , "border-gray-200" , "rounded-lg" ] )
150
131
// add Editor to main PyScript div
@@ -190,18 +171,17 @@ export class PyScript extends HTMLElement {
190
171
eDiv . appendChild ( this . btnConfig ) ;
191
172
192
173
mainDiv . appendChild ( eDiv ) ;
193
- mainDiv . appendChild ( this . editorNode ) ;
194
174
195
175
if ( this . hasAttribute ( 'target' ) ) {
196
- this . editorOut = document . getElementById ( this . getAttribute ( 'target' ) ) ;
176
+ this . outputElement = document . getElementById ( this . getAttribute ( 'target' ) ) ;
197
177
} else {
198
178
// Editor Output Div
199
- this . editorOut = document . createElement ( 'div' ) ;
200
- this . editorOut . classList . add ( "output" ) ;
201
- this . editorOut . hidden = true ;
179
+ this . outputElement = document . createElement ( 'div' ) ;
180
+ this . outputElement . classList . add ( "output" ) ;
181
+ this . outputElement . hidden = true ;
202
182
203
183
// add the output div id there's not target
204
- mainDiv . appendChild ( this . editorOut ) ;
184
+ mainDiv . appendChild ( this . outputElement ) ;
205
185
}
206
186
F438
207
187
if ( currentMode == "edit" ) {
@@ -217,35 +197,6 @@ export class PyScript extends HTMLElement {
217
197
}
218
198
}
219
199
220
- addToOutput ( s : string ) {
221
- this . editorOut . innerHTML = s ;
222
- this . editorOut . hidden = false ;
223
- }
224
-
225
- async loadFromFile ( s : string ) {
226
- let pyodide = await pyodideReadyPromise ;
227
- let response = await fetch ( s ) ;
228
- this . code = await response . text ( ) ;
229
-
230
- await pyodide . runPythonAsync ( this . code ) ;
231
- await pyodide . runPythonAsync ( `
232
- from pyodide.http import pyfetch
233
- from pyodide import eval_code
234
- response = await pyfetch("` + s + `")
235
- content = await response.bytes()
236
-
237
- with open("todo.py", "wb") as f:
238
- print(content)
239
- f.write(content)
240
- print("done writing")
241
- ` )
242
- // let pkg = pyodide.pyimport("todo");
243
- // pyodide.runPython(`
244
- // import todo
245
- // `)
246
- // pkg.do_something();
247
- }
248
-
249
200
protected async _register_esm ( pyodide : PyodideInterface ) : Promise < void > {
250
201
const imports : { [ key : string ] : unknown } = { }
251
202
@@ -278,43 +229,8 @@ export class PyScript extends HTMLElement {
278
229
pyodide . registerJsModule ( "esm" , imports )
279
230
}
280
231
281
- async evaluate ( ) : Promise < void > {
282
- console . log ( 'evaluate' ) ;
283
-
284
- if ( this . source ) {
285
- this . loadFromFile ( this . source )
286
- } else {
287
- const pyodide = await pyodideReadyPromise ;
288
- await this . _register_esm ( pyodide )
289
- // debugger
290
- try {
291
- // @ts -ignore
292
- const source = htmlDecode ( this . editor . state . doc . toString ( ) ) ;
293
- let output ;
294
- if ( source . includes ( "asyncio" ) ) {
295
- output = await pyodide . runPythonAsync ( source ) ;
296
- } else {
297
- output = pyodide . runPython ( source ) ;
298
- }
299
- if ( output !== undefined ) {
300
- this . addToOutput ( output ) ;
301
- }
302
-
303
- if ( this . hasAttribute ( 'auto-generate' ) && this . parentElement . lastChild === this ) {
304
- const newPyscript = document . createElement ( "py-script" ) ;
305
- newPyscript . setAttribute ( 'auto-generate' , null ) ;
306
- this . parentElement . appendChild ( newPyscript ) ;
307
- }
308
- } catch ( err ) {
309
- this . addToOutput ( err ) ;
310
- console . log ( err ) ;
311
- }
312
- }
313
- }
314
-
315
- render ( ) {
316
- console . log ( 'rendered' ) ;
317
-
232
+ getSourceFromElement ( ) : string {
233
+ return this . code ;
318
234
}
319
235
}
320
236
0 commit comments