From 75bb7920855125322e9cd8d2d78bd4ad2e89f2fc Mon Sep 17 00:00:00 2001 From: johndoe6345789 Date: Sat, 17 Jan 2026 19:39:58 +0000 Subject: [PATCH] Generated by Spark: this is so rubbish, i want to type my own input like real CPython - Python Terminal Run Welcome to the greeting program! What is your name? What is your name? Hello, ! Nice to meet you! How old are you? How old are you? Wow, years old! That's awesome! What's your favorite color? What's your favorite color? is a great choice! I love that color too. Thanks for chatting, ! Have a wonderful day! --- src/lib/pyodide-runner.ts | 46 ++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/src/lib/pyodide-runner.ts b/src/lib/pyodide-runner.ts index 4022112..832278b 100644 --- a/src/lib/pyodide-runner.ts +++ b/src/lib/pyodide-runner.ts @@ -62,30 +62,6 @@ export async function runPythonCodeInteractive( ): Promise { const pyodide = await getPyodide() - const inputQueue: string[] = [] - let inputResolve: ((value: string) => void) | null = null - - const customInput = async (prompt = '') => { - if (callbacks.onOutput && prompt) { - callbacks.onOutput(prompt) - } - - if (callbacks.onInputRequest) { - const value = await callbacks.onInputRequest(prompt) - return value - } - - return new Promise((resolve) => { - if (inputQueue.length > 0) { - resolve(inputQueue.shift()!) - } else { - inputResolve = resolve - } - }) - } - - pyodide.globals.set('__custom_input__', customInput) - pyodide.runPython(` import sys from io import StringIO @@ -149,9 +125,29 @@ class InteractiveStderr: pyodide.runPython(` sys.stdout = InteractiveStdout(__output_callback__) sys.stderr = InteractiveStderr(__error_callback__) +`) + const customInput = async (prompt = '') => { + if (callbacks.onInputRequest) { + const value = await callbacks.onInputRequest(prompt) + return value + } + return '' + } + + pyodide.globals.set('__js_input__', customInput) + + pyodide.runPython(` import builtins -builtins.input = __custom_input__ +from js import __js_input__ + +def custom_input(prompt=""): + sys.stdout.write(prompt) + sys.stdout.flush() + result = __js_input__(prompt) + return result + +builtins.input = custom_input `) try {