Code viewer for World: Python Environment [BASIC]...

// Clone by Akshat Sen of:
// "Python Environment [BASIC]" by Aaqid Masoodi
// https://ancientbrain.com/world.php?world=8826716490
// Please leave this clone trail here.



// Clone by Aaqid Masoodi of:
// "Python Environment [LATEST]" by Aaqid Masoodi
// https://ancientbrain.com/world.php?world=8775112567
// Please leave this clone trail here.

document.write(`
<style>
  body { margin:0; font-family:sans-serif; display:flex; height:100vh; }
  .editor, .console { width:50%; padding:10px; box-sizing:border-box; display:flex; flex-direction:column; }
  #code { flex:1; width:100%; font-family:monospace; font-size:16px; padding:10px; }
  #console { flex:1; background:#111; color:#0f0; padding:10px; font-family:monospace; overflow-y:auto; white-space:pre-wrap; }
  button { padding:8px 12px; margin-top:5px; cursor:pointer; font-size:15px; }
  #customPromptOverlay { position: fixed; top:0; left:0; width:100%; height:100%; background: rgba(0,0,0,0.6); display:flex; align-items:center; justify-content:center; z-index:9999; }
  #customPromptBox { background:white; padding:20px; border-radius:8px; text-align:center; min-width:250px; }
  #customPromptBox input { padding:6px; font-size:16px; width:80%; }
  #customPromptBox button { margin-top:10px; padding:6px 12px; font-size:16px; }
</style>

<div class="editor">
  <h2>Python Editor</h2>
  
  <textarea id="code">
# Example Python code using input
print("Created by Aaqid Masoodi.")
print("Hello! This Python code uses input().")
name = input("Enter your name: ")
print(f"Welcome, {name}")
  </textarea>
 
  <button id="runBtn">Run Python</button>
</div>

<div class="console">
  <h2>Console Output</h2>
  <div id="console"></div>
</div>

<div id="customPromptOverlay" style="display:none;">
  <div id="customPromptBox">
    <div id="customPromptMessage"></div>
    <input id="customPromptInput" type="text"/>
    <br/>
    <button onclick="customPromptSubmit()">OK</button>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/pyodide/v0.29.0/full/pyodide.js"></script>
<script>
const consoleEl = document.getElementById("console");
function write(msg) {
    consoleEl.innerHTML += msg + "\\n";
    consoleEl.scrollTop = consoleEl.scrollHeight;
}

// ---------- Custom Prompt ----------
let customPromptResolve;
function customPrompt(message) {
    document.getElementById('customPromptMessage').innerText = message;
    document.getElementById('customPromptInput').value = '';
    document.getElementById('customPromptOverlay').style.display = 'flex';
    document.getElementById('customPromptInput').focus();
    return new Promise(resolve => customPromptResolve = resolve);
}
function customPromptSubmit() {
    const val = document.getElementById('customPromptInput').value;
    document.getElementById('customPromptOverlay').style.display = 'none';
    if(customPromptResolve) customPromptResolve(val);
}
window.prompt = customPrompt; // override blocked prompt

// ---------- Load Pyodide ----------
let pyodideReadyPromise = loadPyodide().then(async pyodide => {
    // Redirect Python stdout/stderr to our console div
    await pyodide.runPythonAsync(\`
import sys
from js import write
class JSWriter:
    def write(self, s):
        if s.strip() != "":
            write(s)
    def flush(self):
        pass
sys.stdout = sys.stderr = JSWriter()
\`);

    // Override Python input() to use custom prompt
    await pyodide.runPythonAsync(\`
import builtins
from js import prompt
import asyncio
async def _async_input(msg=''):
    result = await prompt(msg)
    return result
def _input(msg=''):
    return asyncio.run(_async_input(msg))
builtins.input = _input
\`);

    write("Pyodide Ready! You can run Python code using the editor.");
    return pyodide;
});

// ---------- Run Python Code ----------
document.getElementById("runBtn").onclick = async () => {
    consoleEl.innerHTML = ""; // Clear console each run
    let code = document.getElementById("code").value;
    let pyodide = await pyodideReadyPromise;
    try {
        await pyodide.runPythonAsync(code);
    } catch(err) {
        write("Error: " + err);
    }
};
</script>
`);
print("nambi")