lovebl1
};
const handleInput = (input: string) => {
if (inputResolver) {
addConsoleMessage('input', input);
inputResolver(input);
setInputResolver(null);
setIsWaitingForInput(false);
}
};
const runCode = async () => {
if (!pyodide || isRunning) return;
setIsRunning(true);
addConsoleMessage('output', '--- Running your code ---');
try {
// Replace input() calls with our custom input function
const modifiedCode = code.replace(/input\s*\(/g, 'await custom_input(');
// Wrap in async function for proper input handling
const wrappedCode = `
import sys
from io import StringIO
import asyncio
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = mystdout = StringIO()
sys.stderr = mystderr = StringIO()
async def run_user_code():
try:
${modifiedCode.split('\n').map(line => ' ' + line).join('\n')}
except Exception as e:
import traceback
print(f"❌ Error: {e}", file=sys.stderr)
# Student-friendly error explanations
error_type = type(e).__name__
if error_type == "SyntaxError":
print("💡 Tip: Check your code for missing colons (:), parentheses (), or incorrect indentation!", file=sys.stderr)
elif error_type == "NameError":
print("💡 Tip: Make sure you've defined all your variables before using them!", file=sys.stderr)
elif error_type == "TypeError":
print("💡 Tip: Check if you're using the right data types (numbers, strings, etc.)!", file=sys.stderr)
elif error_type == "ValueError":
print("💡 Tip: Make sure your input matches what the program expects!", file=sys.stderr)
elif error_type == "IndentationError":
print("💡 Tip: Check your spacing! Python is picky about indentation.", file=sys.stderr)
await run_user_code()
output = mystdout.getvalue()
error = mystderr.getvalue()
sys.stdout = old_stdout
sys.stderr = old_stderr
`;
await pyodide.runPython(wrappedCode);
const output = pyodide.globals.get('output');
const error = pyodide.globals.get('error');
if (error) {
addConsoleMessage('error', error);
}
if (output) {
addConsoleMessage('output', output);
}
addConsoleMessage('output', '--- Code execution completed ---');
} catch (error: any) {
addConsoleMessage('error', `Unexpected error: ${error.message}`);
addConsoleMessage('error', '💡 Tip: Try checking your code syntax and try again!');
} finally {
setIsRunning(false);
}
};
const clearCode = () => {
setCode('# Start coding here! 🚀\n\n');
};
const clearConsole = () => {
setConsoleMessages([]);
};
return (
);
};
{/* Toolbar */}
{isRunning ? 'Running...' : 'RUN'}
Clear Code
Clear Console
{theme === 'light' ? (
) : (
)}
{theme === 'light' ? 'Dark' : 'Light'} Mode
{/* Main Content */}
{/* Code Editor */}
{/* Console */}
Comments
Post a Comment