Allow clearing Pyodide text console with os.system("clear")#1498
Draft
adrian-rpf wants to merge 1 commit into
Draft
Allow clearing Pyodide text console with os.system("clear")#1498adrian-rpf wants to merge 1 commit into
adrian-rpf wants to merge 1 commit into
Conversation
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>
Contributor
There was a problem hiding this comment.
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.systemin the Pyodide runtime to detectclear/cls, flush output, and call abasthon.kernel.clear_console()bridge. - Add a
handleClearworker → UI message and aPyodideRunnerhandler 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" }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
os.system("clear")/os.system("cls")to clear the on-screen text console mid-run.handleClear) and main-thread handler that wipes the<pre>output pane only.print(..., end="")does not reappear after a clear.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.jsyarn exec eslinton touched fileshttp://localhost:65442/web-component.html) with real Pyodideyarn exec cypress run --spec cypress/e2e/spec-wc-pyodide.cy.js(Cypress binary failed to launch locally in agent environment)Made with Cursor