Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60232bf

Browse files
committed
Ensure env is created before executing scripts
1 parent 9d64a9f commit 60232bf

File tree

3 files changed

+39
-46
lines changed

3 files changed

+39
-46
lines changed

pyscriptjs/public/bokeh.html

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,34 @@
1919
<py-env>
2020
- bokeh
2121
- numpy
22-
- pandas
2322
</py-env>
2423
<h1>Bokeh Example</h1>
2524
<div id="myplot"></div>
26-
<py-repl></py-repl>
25+
<py-repl id="my-repl"></py-repl>
2726

2827
<py-script>
29-
from bokeh.plotting import figure
30-
from bokeh.resources import CDN
3128
import json
32-
from bokeh.embed import json_item
33-
from js import Bokeh
29+
import pyodide
3430

31+
from js import Bokeh, console, JSON
3532

36-
from bokeh.io import output_notebook, show
33+
from bokeh.embed import json_item
3734
from bokeh.plotting import figure
38-
import bokeh.sampledata
35+
from bokeh.resources import CDN
3936

4037
# create a new plot with default tools, using figure
4138
p = figure(plot_width=400, plot_height=400)
4239

4340
# add a circle renderer with x and y coordinates, size, color, and alpha
4441
p.circle([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], size=15, line_color="navy", fill_color="orange", fill_alpha=0.5)
4542
p_json = json.dumps(json_item(p, "myplot"))
46-
print(p_json)
4743

48-
async def show(item):
49-
print("about to embed")
50-
Bokeh.embed.embed_item(item)
51-
print ("Done embedding...")
44+
print("about to embed")
5245

53-
pyscript.run_until_complete(show(p_json))
54-
#show(p) # show the results
46+
Bokeh.embed.embed_item(JSON.parse(p_json))
5547

48+
print ("Done embedding...")
5649
</py-script>
5750

5851
</body>
59-
</html>
52+
</html>

pyscriptjs/src/App.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
iconSize = 2;
2323
}
2424
25-
const initializePyodide = () =>{
25+
const initializePyodide = async () =>{
2626
// @ts-ignore
2727
pyodideReadyPromise = loadInterpreter();
2828
// @ts-ignore
@@ -44,7 +44,7 @@
4444
4545
// now we call all initializers before we actually executed all page scripts
4646
for (let initializer of $initializers){
47-
initializer();
47+
await initializer();
4848
}
4949
5050
// now we can actually execute the page scripts if we are in play mode

pyscriptjs/src/components/pyenv.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as jsyaml from 'js-yaml';
22

3-
import { pyodideLoaded, loadedEnvironments, mode, addPostInitializer } from '../stores';
3+
import { pyodideLoaded, loadedEnvironments, mode, addInitializer } from '../stores';
44
import { loadPackage } from '../interpreter';
55

66
// Premise used to connect to the first available pyodide interpreter
@@ -11,39 +11,39 @@ let currentMode;
1111
pyodideLoaded.subscribe(value => {
1212
pyodideReadyPromise = value;
1313
});
14+
1415
loadedEnvironments.subscribe(value => {
15-
environments = value;
16+
environments = value;
1617
});
1718

1819
mode.subscribe(value => {
1920
currentMode = value;
2021
});
2122

2223
export class PyEnv extends HTMLElement {
23-
shadow: ShadowRoot;
24-
wrapper: HTMLElement;
25-
code: string;
26-
environment: any;
27-
28-
constructor() {
29-
super();
30-
31-
// attach shadow so we can preserve the element original innerHtml content
32-
this.shadow = this.attachShadow({ mode: 'open'});
33-
this.wrapper = document.createElement('slot');
34-
}
35-
36-
connectedCallback() {
37-
this.code = this.innerHTML;
38-
this.innerHTML = '';
39-
40-
let env = this.environment = jsyaml.load(this.code);
41-
async function loadEnv(){
42-
let pyodide = await pyodideReadyPromise;
43-
loadPackage(env, pyodide);
44-
console.log("enviroment loaded")
45-
}
46-
addPostInitializer(loadEnv);
47-
console.log("enviroment loading...")
24+
shadow: ShadowRoot;
25+
wrapper: HTMLElement;
26+
code: string;
27+
environment: any;
28+
29+
constructor() {
30+
super();
31+
32+
this.shadow = this.attachShadow({ mode: 'open'});
33+
this.wrapper = document.createElement('slot');
34+
}
35+
36+
connectedCallback() {
37+
this.code = this.innerHTML;
38+
this.innerHTML = '';
39+
40+
let env = this.environment = jsyaml.load(this.code);
41+
async function loadEnv() {
42+
let pyodide = await pyodideReadyPromise;
43+
await loadPackage(env, pyodide);
44+
console.log("enviroment loaded")
4845
}
49-
}
46+
addInitializer(loadEnv);
47+
console.log("enviroment loading...", env)
48+
}
49+
}

0 commit comments

Comments
 (0)