Skip to content

Commit 363a8e3

Browse files
authored
1 parent 18de632 commit 363a8e3

File tree

13 files changed

+58
-27
lines changed

13 files changed

+58
-27
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ REACT_APP_PLAUSIBLE_DATA_DOMAIN=''
66
REACT_APP_PLAUSIBLE_SOURCE=''
77
REACT_APP_SENTRY_DSN=''
88
REACT_APP_SENTRY_ENV='local'
9-
PUBLIC_URL='http://localhost:3010'
9+
PUBLIC_URL='http://localhost:3011'
1010
ASSETS_URL='http://localhost:3010'

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "chrome",
66
"request": "launch",
77
"name": "Launch Chrome against localhost",
8-
"url": "http://localhost:3010",
8+
"url": "http://localhost:3011",
99
"webRoot": "${workspaceFolder}"
1010
}
1111
]

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88

99
### Added
1010

11+
- PyodideWorker setup for the editor (#1104)
1112
- Enabling `pyodide` support in the web component (#1090)
1213
- `Pyodide` `matplotlib` support (#1087)
1314
- Tests for running simple programs in `pyodide` and `skulpt` (#1100)
File renamed without changes.
File renamed without changes.

src/PyodideWorker.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// Nest the PyodideWorker function inside a globalThis object so we control when its initialised.
44
const PyodideWorker = () => {
55
// Import scripts dynamically based on the environment
6-
importScripts(`${process.env.PUBLIC_URL}/_internal_sense_hat.js`);
7-
importScripts(`${process.env.PUBLIC_URL}/pygal.js`);
6+
importScripts(
7+
`${process.env.ASSETS_URL}/pyodide/shims/_internal_sense_hat.js`,
8+
);
9+
importScripts(`${process.env.ASSETS_URL}/pyodide/shims/pygal.js`);
810
importScripts("https://cdn.jsdelivr.net/pyodide/v0.26.2/full/pyodide.js");
911

1012
const supportsAllFeatures = typeof SharedArrayBuffer !== "undefined";
@@ -164,7 +166,7 @@ const PyodideWorker = () => {
164166
before: async () => {
165167
pyodide.registerJsModule("basthon", fakeBasthonPackage);
166168
await pyodide.loadPackage(
167-
`${process.env.ASSETS_URL}/packages/turtle-0.0.1-py3-none-any.whl`,
169+
`${process.env.ASSETS_URL}/pyodide/packages/turtle-0.0.1-py3-none-any.whl`,
168170
);
169171
},
170172
after: () =>
@@ -182,7 +184,7 @@ const PyodideWorker = () => {
182184
pyodide.registerJsModule("basthon", fakeBasthonPackage);
183185
await pyodide.loadPackage([
184186
"setuptools",
185-
`${process.env.ASSETS_URL}/packages/p5-0.0.1-py3-none-any.whl`,
187+
`${process.env.ASSETS_URL}/pyodide/packages/p5-0.0.1-py3-none-any.whl`,
186188
]);
187189
},
188190
after: () => {},
@@ -203,7 +205,7 @@ const PyodideWorker = () => {
203205
});
204206
await pyodide.loadPackage([
205207
"pillow",
206-
`${process.env.ASSETS_URL}/packages/sense_hat-0.0.1-py3-none-any.whl`,
208+
`${process.env.ASSETS_URL}/pyodide/packages/sense_hat-0.0.1-py3-none-any.whl`,
207209
]);
208210

209211
_internal_sense_hat.config.pyodide = pyodide;

src/components/Editor/Runners/PythonRunner/PyodideRunner/PyodideRunner.jsx

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/* eslint import/no-webpack-loader-syntax: off */
21
/* eslint-disable react-hooks/exhaustive-deps */
3-
42
import "../../../../../assets/stylesheets/PythonRunner.scss";
53
import React, { useContext, useEffect, useMemo, useRef, useState } from "react";
64
import { useDispatch, useSelector } from "react-redux";
@@ -20,27 +18,25 @@ import OutputViewToggle from "../OutputViewToggle";
2018
import { SettingsContext } from "../../../../../utils/settings";
2119
import RunnerControls from "../../../../RunButton/RunnerControls";
2220

23-
const PyodideRunner = ({ active }) => {
24-
const getWorkerURL = (url) => {
25-
const content = `
26-
/* global PyodideWorker */
27-
console.log("Worker loading");
28-
importScripts("${url}");
29-
const pyodide = PyodideWorker();
30-
console.log("Worker loaded");
31-
`;
32-
const blob = new Blob([content], { type: "application/javascript" });
33-
return URL.createObjectURL(blob);
34-
};
21+
const getWorkerURL = (url) => {
22+
const content = `
23+
/* global PyodideWorker */
24+
console.log("Worker loading");
25+
importScripts("${url}");
26+
const pyodide = PyodideWorker();
27+
console.log("Worker loaded");
28+
`;
29+
const blob = new Blob([content], { type: "application/javascript" });
30+
return URL.createObjectURL(blob);
31+
};
3532

36-
const workerUrl = getWorkerURL(`${process.env.PUBLIC_URL}/PyodideWorker.js`);
33+
const PyodideRunner = (props) => {
34+
const { active } = props;
3735

36+
// Blob approach + targeted headers - no errors but headers required in host app to interrupt code
37+
const workerUrl = getWorkerURL(`${process.env.PUBLIC_URL}/PyodideWorker.js`);
3838
const pyodideWorker = useMemo(() => new Worker(workerUrl), []);
3939

40-
if (!pyodideWorker) {
41-
console.error("PyodideWorker is not initialized");
42-
}
43-
4440
const interruptBuffer = useRef();
4541
const stdinBuffer = useRef();
4642
const stdinClosed = useRef();
@@ -312,6 +308,11 @@ const PyodideRunner = ({ active }) => {
312308
}
313309
};
314310

311+
if (!pyodideWorker) {
312+
console.error("PyodideWorker is not initialized");
313+
return;
314+
}
315+
315316
return (
316317
<div
317318
className={`pythonrunner-container pyodiderunner${

0 commit comments

Comments
 (0)