Py IDE
The <py-ide> component is an in-browser Python IDE powered by Pyodide. It provides:
- Multi-file editing with tabs
- CodeMirror editor with Python syntax highlighting
- Run/Save/Clear controls and resizable panes
- Live output including inline images for
matplotlibplots - Local persistence via
localStorage
Import
import "essential-components/src/py-ide";
Basic Usage
<py-ide></py-ide>
Usage with Passing Files
<py-ide>
<script type="text/plain" data-filename="main.py">
print("Hello from PyIDE 👋")
for i in range(3):
print(f"Line {i+1}")
</script>
<script type="text/plain" data-filename="utils.py">
def square(x): return x*x
</script>
</py-ide>
Live Example
Local Storage (storage-key)
- what: A unique key used to persist the IDE state in
localStorage(files, open tabs, active file, and last output) - default:
"py-ide" - why: Use different keys to prevent pages/widgets from overwriting each other
Set a custom key (HTML)
<py-ide storage-key="docs-pyide-intro">
<script type="text/plain" data-filename="main.py">
print("Hello")
</script>
</py-ide>
Set a custom key (React/MDX)
import { PyIDE } from "@site/src/components/py-ide";
<PyIDE storageKey="docs-pyide-intro" />;
Clear saved state
localStorage.removeItem("docs-pyide-intro");
Tip: If you need truly isolated sessions for demos, give each demo a different
storage-key.
Programmatic API
Access properties and methods from JavaScript:
const ide = document.querySelector("py-ide");
console.log(ide.code); // [{ name, content }, ...]
console.log(ide.output); // string
// Replace all files
ide.setCode([{ name: "example.py", content: 'print("hi")' }]);
// Listen to events
ide.addEventListener("input", (e) => console.log("input", e.detail));
ide.addEventListener("change", (e) => console.log("change", e.detail));
ide.addEventListener("submit", (e) => console.log("submit", e.detail));
ide.addEventListener("save", (e) => console.log("save", e.detail));
Notes
- First load installs common packages (
requests,numpy,matplotlib) viamicropipin Pyodide. - For multi-file initialization, pass multiple
<script type="text/plain" data-filename="...">blocks as children. - Images printed by
matplotlibare embedded directly into the output.