Skip to content

Allow clearing Pyodide text console with os.system("clear")#1498

Draft
adrian-rpf wants to merge 1 commit into
mainfrom
feature/pyodide-clear-console
Draft

Allow clearing Pyodide text console with os.system("clear")#1498
adrian-rpf wants to merge 1 commit into
mainfrom
feature/pyodide-clear-console

Conversation

@adrian-rpf

Copy link
Copy Markdown
Contributor

Summary

  • Patch Pyodide os.system("clear") / os.system("cls") to clear the on-screen text console mid-run.
  • Add a worker bridge (handleClear) and main-thread handler that wipes the <pre> output pane only.
  • Flush batched stdout/stderr (including forced newline) before clearing so print(..., end="") does not reappear after a clear.
  • Add Jest wiring tests and Cypress e2e coverage; allow overriding the e2e origin via EDITOR_ORIGIN.

Test plan

  • CI=true yarn run test --runTestsByPath src/components/Editor/Runners/PythonRunner/PyodideRunner/PyodideRunner.test.js src/components/Editor/Runners/PythonRunner/PyodideRunner/PyodideWorker.test.js
  • yarn exec eslint on touched files
  • Manual browser verification on alternate dev server (http://localhost:65442/web-component.html) with real Pyodide
  • yarn exec cypress run --spec cypress/e2e/spec-wc-pyodide.cy.js (Cypress binary failed to launch locally in agent environment)

Made with Cursor

Patch os.system("clear"/"cls") in the Pyodide worker so mid-run Python
output can wipe the on-screen text console, flushing batched stdout/stderr
first so no-newline prints do not reappear after clearing.

Co-authored-by: Cursor <cursoragent@cursor.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds support for clearing the Pyodide text console mid-run by intercepting os.system("clear") / os.system("cls") inside the Pyodide worker, bridging that to the main thread, and wiping the text output <pre> without affecting the rest of the UI.

Changes:

  • Patch os.system in the Pyodide runtime to detect clear/cls, flush output, and call a basthon.kernel.clear_console() bridge.
  • Add a handleClear worker → UI message and a PyodideRunner handler that clears the console output pane.
  • Extend Jest and Cypress coverage, including configurable Cypress origin via EDITOR_ORIGIN.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/PyodideWorker.js Adds clear_console bridge and patches os.system() to emit a clear-console message.
src/components/Editor/Runners/PythonRunner/PyodideRunner/PyodideWorker.test.js Adds unit tests asserting the patching/bridge behavior.
src/components/Editor/Runners/PythonRunner/PyodideRunner/PyodideRunner.test.js Adds UI test verifying previously printed output is removed on handleClear.
src/components/Editor/Runners/PythonRunner/PyodideRunner/PyodideRunner.jsx Handles handleClear messages and factors console clearing into clearConsole().
cypress/e2e/spec-wc-pyodide.cy.js Adds e2e coverage for os.system("clear") behavior and supports EDITOR_ORIGIN override.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +216 to +218
const clearConsole = () => {
output.current.innerHTML = "";
};
Comment on lines +99 to +103
test("it patches os.system to handle clear/cls", async () => {
expect(pyodide.runPythonAsync).toHaveBeenCalledWith(
expect.stringMatching(/os\.system/),
);
});
Comment on lines +105 to +112
test("it forces batched output to flush before clearing", async () => {
expect(pyodide.runPythonAsync).toHaveBeenCalledWith(
expect.stringMatching(/_sys\.stdout\.write\("\\n"\)/),
);
expect(pyodide.runPythonAsync).toHaveBeenCalledWith(
expect.stringMatching(/_sys\.stderr\.write\("\\n"\)/),
);
});
Comment on lines +114 to +120
test("the basthon clear_console bridge posts handleClear", () => {
const basthonCall = pyodide.registerJsModule.mock.calls.find(
([name]) => name === "basthon",
);
basthonCall[1].kernel.clear_console();
expect(global.postMessage).toHaveBeenCalledWith({ method: "handleClear" });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants