|
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 |
| -let pyodideReadyPromise; |
| 4 | +let runtime; |
5 | 5 | let environments;
|
6 | 6 | let currentMode;
|
7 | 7 | let Element;
|
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 | async getSourceFromFile(s: string): Promise<string> {
|
66 |
| - const pyodide = await pyodideReadyPromise; |
| 67 | + const pyodide = runtime; |
67 | 68 | const response = await fetch(s);
|
68 | 69 | this.code = await response.text();
|
69 | 70 | return this.code;
|
@@ -101,7 +102,7 @@ export class BaseEvalElement extends HTMLElement {
|
101 | 102 |
|
102 | 103 | async evaluate(): Promise<void> {
|
103 | 104 | console.log('evaluate');
|
104 |
| - const pyodide = await pyodideReadyPromise; |
| 105 | + const pyodide = runtime; |
105 | 106 | let source: string;
|
106 | 107 | let output;
|
107 | 108 | try {
|
@@ -154,7 +155,7 @@ export class BaseEvalElement extends HTMLElement {
|
154 | 155 |
|
155 | 156 | async eval(source: string): Promise<void> {
|
156 | 157 | let output;
|
157 |
| - const pyodide = await pyodideReadyPromise; |
| 158 | + const pyodide = runtime; |
158 | 159 |
|
159 | 160 | try {
|
160 | 161 | output = await pyodide.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" in value){ |
| 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 |
| - async registerWidget() { |
207 |
| - const pyodide = await pyodideReadyPromise; |
| 221 | + registerWidget() { |
| 222 | + const pyodide = 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 | async eval(source: string): Promise<void> {
|
213 | 228 | let output;
|
214 |
| - const pyodide = await pyodideReadyPromise; |
| 229 | + const pyodide = runtime; |
215 | 230 | try {
|
216 | 231 | output = await pyodide.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 | async getSourceFromFile(s: string): Promise<string> {
|
309 |
| - const pyodide = await pyodideReadyPromise; |
| 324 | + const pyodide = runtime; |
310 | 325 | const response = await fetch(s);
|
311 | 326 | return await response.text();
|
312 | 327 | }
|
313 | 328 |
|
314 | 329 | async eval(source: string): Promise<void> {
|
315 | 330 | let output;
|
316 |
| - const pyodide = await pyodideReadyPromise; |
| 331 | + const pyodide = runtime; |
317 | 332 | try {
|
318 | 333 | output = await pyodide.runPythonAsync(source);
|
319 | 334 | if (output !== undefined) {
|
|
0 commit comments