{ "interpreter" : "/static/js/pyscript-offline/public/pyodide/pyodide.mjs", "packages": ["pandas", "numpy", "matplotlib"], "autoclose_loader": "true", "runtimes": "None", "src": "/pyodide.js", "name": "pyodide-0.20", "lang": "python" }

Classzy Labs - Learn by Doing

from pyodide.ffi import create_proxy from pyscript import window, document #from pyscript import Element #from pyscript.web import page def run_code(): """Python function to trigger code execution.""" try: py_script_element = document.querySelector("#editor") editor = py_script_element.editor # Get the code from the editor (JavaScript) code = editor.getValue() # Execute the code (Pyodide) result = pyodide.runPython(code) # or pyodide.runPythonAsync(code) for async # Handle output (Python) print("Python execution result:", result) if result: # Example: setting an output element output_element = document.querySelector("#output") output_element.innerText = result # Access the underlying DOM element except Exception as e: print(f"Error running code: {e}") output_element = document.querySelector("#output") output_element.innerText = f"Error: {e}" return # Create a proxy for the Python function so it can be called from JavaScript run_code_proxy = create_proxy(run_code) # Attach the click event listener (JavaScript) run_button = document.querySelector("#run-button") run_button.addEventListener("click", run_code_proxy) # .element is important! #run_button.onclick = run_code_proxy def run_code2(): """Python function to trigger code execution.""" try: # Get the py-script element and the CodeMirror editor instance (JavaScript) #py_script_elements = page["#editor"] py_script_element = document.querySelector("#editor") #py_script_element = Element('editor') # Using pyscript.Element API #for py_script_element in py_script_elements: #py_script_element.element.addEventListener("click", my_function) # .element is important! #editor = py_script_element.element.editor editor = py_script_element.editor # Get the code from the editor (JavaScript) code = editor.getValue() # Execute the code (Pyodide) result = pyodide.runPython(code) # or pyodide.runPythonAsync(code) for async # Handle output (Python) print("Python execution result:", result) if result: # Example: setting an output element #output_elements = page["#output"] output_element = document.querySelector("#output") #output_element = Element("output") #for output_element in output_elements: #py_script_element.element.addEventListener("click", my_function) # .element is important! #editor = output_element.editor #output_element.element.innerText = result # Access the underlying DOM element output_element.innerText = result # Access the underlying DOM element except Exception as e: print(f"Error running code: {e}") #output_elements = page["#output"] output_element = document.querySelector("#output") #output_element = Element("output") #for output_element in output_elements: #py_script_element.element.addEventListener("click", my_function) # .element is important! #editor = py_script_element.editor #run_button.element.onclick = run_code_proxy #output_element.element.innerText = f"Error: {e}" output_element.innerText = f"Error: {e}" # Create a proxy for the Python function so it can be called from JavaScript run_code_proxy = create_proxy(run_code) # Attach the click event listener (JavaScript) #run_buttons = page["#run-button"] run_button = document.querySelector("#run-button") #run_button = Element('run-button') #for run_button in run_buttons: #run_button.element.addEventListener("click", run_code_proxy) # .element is important! run_button.addEventListener("click", run_code_proxy) # .element is important! #editor = py_script_element.editor #run_button.element.onclick = run_code_proxy #run_button.onclick = run_code_proxy

Online Editor

Interactive Console