|
1 | 1 | import{loadedEnvironments,mode,pyodideLoaded}from'../stores';
|
2 | 2 | import{guidGenerator,addClasses}from'../utils';
|
3 | 3 | // Premise used to connect to the first available pyodide interpreter
|
4 |
| -letpyodideReadyPromise; |
| 4 | +letruntime; |
5 | 5 | letenvironments;
|
6 | 6 | letcurrentMode;
|
7 | 7 | letElement;
|
8 | 8 |
|
| 9 | + |
9 | 10 | pyodideLoaded.subscribe(value=>{
|
10 |
| -pyodideReadyPromise=value; |
| 11 | +runtime=value; |
11 | 12 | });
|
12 | 13 | loadedEnvironments.subscribe(value=>{
|
13 | 14 | environments=value;
|
@@ -63,7 +64,7 @@ export class BaseEvalElement extends HTMLElement {
|
63 | 64 | }
|
64 | 65 |
|
65 | 66 | asyncgetSourceFromFile(s:string):Promise<string>{
|
66 |
| -constpyodide=awaitpyodideReadyPromise; |
| 67 | +constpyodide=runtime; |
67 | 68 | constresponse=awaitfetch(s);
|
68 | 69 | this.code=awaitresponse.text();
|
69 | 70 | returnthis.code;
|
@@ -101,7 +102,7 @@ export class BaseEvalElement extends HTMLElement {
|
101 | 102 |
|
102 | 103 | asyncevaluate():Promise<void>{
|
103 | 104 | console.log('evaluate');
|
104 |
| -constpyodide=awaitpyodideReadyPromise; |
| 105 | +constpyodide=runtime; |
105 | 106 | letsource:string;
|
106 | 107 | letoutput;
|
107 | 108 | try{
|
@@ -154,7 +155,7 @@ export class BaseEvalElement extends HTMLElement {
|
154 | 155 |
|
155 | 156 | asynceval(source:string):Promise<void>{
|
156 | 157 | letoutput;
|
157 |
| -constpyodide=awaitpyodideReadyPromise; |
| 158 | +constpyodide=runtime; |
158 | 159 |
|
159 | 160 | try{
|
160 | 161 | output=awaitpyodide.runPythonAsync(source);
|
@@ -193,25 +194,39 @@ function createWidget(name: string, code: string, klass: string) {
|
193 | 194 | // ideally we can just wait for it to load and then run. To do
|
194 | 195 | // so we need to replace using the promise and actually using
|
195 | 196 | // the interpreter after it loads completely
|
196 |
| -setTimeout(()=>{ |
197 |
| -this.eval(this.code).then(()=>{ |
198 |
| -this.proxy=this.proxyClass(this); |
199 |
| -console.log('proxy',this.proxy); |
200 |
| -this.proxy.connect(); |
201 |
| -this.registerWidget(); |
202 |
| -}); |
203 |
| -},2000); |
| 197 | +// setTimeout(() => { |
| 198 | +// this.eval(this.code).then(() => { |
| 199 | +// this.proxy = this.proxyClass(this); |
| 200 | +// console.log('proxy', this.proxy); |
| 201 | +// this.proxy.connect(); |
| 202 | +// this.registerWidget(); |
| 203 | +// }); |
| 204 | +// }, 2000); |
| 205 | +pyodideLoaded.subscribe(value=>{ |
| 206 | +console.log("RUNTIME READY",value) |
| 207 | +if("runPythonAsync"invalue){ |
| 208 | +runtime=value; |
| 209 | +setTimeout(()=>{ |
| 210 | +this.eval(this.code).then(()=>{ |
| 211 | +this.proxy=this.proxyClass(this); |
| 212 | +console.log('proxy',this.proxy); |
| 213 | +this.proxy.connect(); |
| 214 | +this.registerWidget(); |
| 215 | +}); |
| 216 | +},1000); |
| 217 | +} |
| 218 | +}); |
204 | 219 | }
|
205 | 220 |
|
206 | 221 | asyncregisterWidget(){
|
207 |
| -constpyodide=awaitpyodideReadyPromise; |
| 222 | +constpyodide=runtime; |
208 | 223 | console.log('new widget registered:',this.name);
|
209 | 224 | pyodide.globals.set(this.id,this.proxy);
|
210 | 225 | }
|
211 | 226 |
|
212 | 227 | asynceval(source:string):Promise<void>{
|
213 | 228 | letoutput;
|
214 |
| -constpyodide=awaitpyodideReadyPromise; |
| 229 | +constpyodide=runtime; |
215 | 230 | try{
|
216 | 231 | output=awaitpyodide.runPythonAsync(source);
|
217 | 232 | this.proxyClass=pyodide.globals.get(this.klass);
|
@@ -306,14 +321,14 @@ export class PyWidget extends HTMLElement {
|
306 | 321 | }
|
307 | 322 |
|
308 | 323 | asyncgetSourceFromFile(s:string):Promise<string>{
|
309 |
| -constpyodide=awaitpyodideReadyPromise; |
| 324 | +constpyodide=runtime; |
310 | 325 | constresponse=awaitfetch(s);
|
311 | 326 | returnawaitresponse.text();
|
312 | 327 | }
|
313 | 328 |
|
314 | 329 | asynceval(source:string):Promise<void>{
|
315 | 330 | letoutput;
|
316 |
| -constpyodide=awaitpyodideReadyPromise; |
| 331 | +constpyodide=runtime; |
317 | 332 | try{
|
318 | 333 | output=awaitpyodide.runPythonAsync(source);
|
319 | 334 | if(output!==undefined){
|
|