Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- `Pyodide` `matplotlib` support (#1087)

### Fixed

- Dynamic runner switching with more than one `python` file (#1097)
- Pyodide running the correct file (`main.py`) when there are multiple `python` files (#1097)

## [0.27.1] - 2024-10-01

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ const PyodideRunner = ({ active }) => {
writeFile([name, extension].join("."), content);
}

const program = projectCode[0].content;
// program is the content of the component with name main and extension py
const program = projectCode.find(
(component) => component.name === "main" && component.extension === "py",
).content;

if (interruptBuffer.current) {
interruptBuffer.current[0] = 0; // Clear previous signals.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const initialState = {
editor: {
project: {
components: [
{ name: "a", extension: "py", content: "print('a')" },
{ name: "main", extension: "py", content: "print('hello')" },
],
image_list: [
Expand Down Expand Up @@ -67,6 +68,11 @@ describe("When a code run has been triggered", () => {
});

test("it writes the current files to the worker", () => {
expect(postMessage).toHaveBeenCalledWith({
method: "writeFile",
filename: "a.py",
content: "print('a')",
});
expect(postMessage).toHaveBeenCalledWith({
method: "writeFile",
filename: "main.py",
Expand Down
5 changes: 3 additions & 2 deletions src/components/Editor/Runners/PythonRunner/PythonRunner.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const PythonRunner = () => {
return imports;
};

project.components?.forEach((component) => {
for (const component of project.components || []) {
if (component.extension === "py" && !codeRunTriggered) {
try {
const imports = getImports(component.content);
Expand All @@ -57,14 +57,15 @@ const PythonRunner = () => {
);
if (hasSkulptOnlyModules || senseHatAlwaysEnabled) {
setUsePyodide(false);
break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good solution
Why did you decide to put "break" here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to break out of looping over the components when one is found containing skulpt-only modules. The problem we were having only happened if are multiple components ("files"). If there was a file with skulpt-only imports it would switch to skulpt, but then if it found another file without skulpt-only imports afterwards it would switch back to pyodide and then the code wouldn't run. So we should break out of the loop as soon as we find a file with a skulpt-only import, as we already have enough information to know we need to use skulpt.

} else {
setUsePyodide(true);
}
} catch (error) {
console.error("Error occurred while getting imports:", error);
}
}
});
}
}, [project, codeRunTriggered, senseHatAlwaysEnabled, t]);
return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const initialState = {
extension: "py",
content: "",
},
{
name: "amazing",
extension: "py",
content: "",
},
],
image_list: [],
},
Expand Down
Loading