Skip to content

Commit 70d4014

Browse files
authored
Merge pull request pyscript#150 from pyscript/quickfix/pyscript_loading_sequence
Hotfix for py-script/py-repl loading
2 parents b77b8ca + f957fda commit 70d4014

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

pyscriptjs/examples/pylist.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ def on_click(self, evt=None):
99

1010
class PyList(PyListTemplate):
1111
item_class = PyItem
12+
13+
def add(self, item):
14+
if isinstance(item, str):
15+
item = { "content": item, "done": False, "created_at": dt.now() }
16+
17+
super().add(item, labels=['content'], state_key="done")

pyscriptjs/examples/repl2.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width,initial-scale=1" />
66

7-
<title>Svelte app</title>
7+
<title>Custom REPL Example</title>
88

99
<link rel="icon" type="image/png" href="favicon.png" />
1010
<link rel="stylesheet" href="../build/pyscript.css" />
@@ -23,7 +23,7 @@
2323

2424
<body>
2525
<h1 class="font-semibold text-2xl ml-5">Custom REPL</h1>
26-
<py-box widths="2/3;1/3">
26+
<py-box widths="1/2;1/2">
2727
<py-repl id="my-repl" auto-generate="true" std-out="output" std-err="err-div"> </py-repl>
2828
<div id="output" class="p-4"></div>
2929
</py-box>

pyscriptjs/examples/simple_clock.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width,initial-scale=1" />
66

7-
<title>Svelte app</title>
7+
<title>Simple Clock Demo</title>
88

99
<link rel="icon" type="image/png" href="favicon.png" />
1010
<link rel="stylesheet" href="../build/pyscript.css" />

pyscriptjs/examples/todo-pylist.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
# add a new task to the list and tell it to use the `content` key to show in the UI
2424
# and to use the key `done` to sync the task status with a checkbox element in the UI
25-
myList.add(task, labels=['content'], state_key="done")
25+
myList.add(task)
2626

2727
# clear the inputbox element used to create the new task
2828
new_task_content.clear()

pyscriptjs/src/components/base.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { loadedEnvironments, mode, pyodideLoaded } from '../stores';
22
import { guidGenerator, addClasses } from '../utils';
33
// Premise used to connect to the first available pyodide interpreter
4-
let pyodideReadyPromise;
4+
let runtime;
55
let environments;
66
let currentMode;
77
let Element;
88

9+
910
pyodideLoaded.subscribe(value => {
10-
pyodideReadyPromise = value;
11+
runtime = value;
1112
});
1213
loadedEnvironments.subscribe(value => {
1314
environments = value;
@@ -63,7 +64,7 @@ export class BaseEvalElement extends HTMLElement {
6364
}
6465

6566
async getSourceFromFile(s: string): Promise<string> {
66-
const pyodide = await pyodideReadyPromise;
67+
const pyodide = runtime;
6768
const response = await fetch(s);
6869
this.code = await response.text();
6970
return this.code;
@@ -101,7 +102,7 @@ export class BaseEvalElement extends HTMLElement {
101102

102103
async evaluate(): Promise<void> {
103104
console.log('evaluate');
104-
const pyodide = await pyodideReadyPromise;
105+
const pyodide = runtime;
105106
let source: string;
106107
let output;
107108
try {
@@ -154,7 +155,7 @@ export class BaseEvalElement extends HTMLElement {
154155

155156
async eval(source: string): Promise<void> {
156157
let output;
157-
const pyodide = await pyodideReadyPromise;
158+
const pyodide = runtime;
158159

159160
try {
160161
output = await pyodide.runPythonAsync(source);
@@ -193,25 +194,39 @@ function createWidget(name: string, code: string, klass: string) {
193194
// ideally we can just wait for it to load and then run. To do
194195
// so we need to replace using the promise and actually using
195196
// 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+
});
204219
}
205220

206-
async registerWidget() {
207-
const pyodide = await pyodideReadyPromise;
221+
registerWidget() {
222+
const pyodide = runtime;
208223
console.log('new widget registered:', this.name);
209224
pyodide.globals.set(this.id, this.proxy);
210225
}
211226

212227
async eval(source: string): Promise<void> {
213228
let output;
214-
const pyodide = await pyodideReadyPromise;
229+
const pyodide = runtime;
215230
try {
216231
output = await pyodide.runPythonAsync(source);
217232
this.proxyClass = pyodide.globals.get(this.klass);
@@ -306,14 +321,14 @@ export class PyWidget extends HTMLElement {
306321
}
307322

308323
async getSourceFromFile(s: string): Promise<string> {
309-
const pyodide = await pyodideReadyPromise;
324+
const pyodide = runtime;
310325
const response = await fetch(s);
311326
return await response.text();
312327
}
313328

314329
async eval(source: string): Promise<void> {
315330
let output;
316-
const pyodide = await pyodideReadyPromise;
331+
const pyodide = runtime;
317332
try {
318333
output = await pyodide.runPythonAsync(source);
319334
if (output !== undefined) {

0 commit comments

Comments
 (0)