diff --git a/docs/_ext/__init__.py b/docs/_ext/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/docs/_ext/playground.py b/docs/_ext/playground.py new file mode 100644 index 00000000..7193ce47 --- /dev/null +++ b/docs/_ext/playground.py @@ -0,0 +1,42 @@ +# docs/_ext/playground.py + +import logging +from pathlib import Path +from typing import Any +from uuid import uuid4 + +from docutils import nodes +from docutils.parsers.rst import Directive +from jinja2 import Environment, FileSystemLoader +from typing_extensions import Self + +__all__ = ("WasmPlayground", "setup") + + +logger = logging.getLogger(__name__) + + +class WasmPlayground(Directive): + """ + A custom Sphinx directive to embed a Pyodide-powered code playground. + """ + + logger.info("Initializing WasmPlayground directive") + has_content = True + + def run(self: Self) -> list[Any]: + # Generate unique IDs for the HTML elements + id = uuid4().hex + env = Environment(loader=FileSystemLoader(Path(__file__).parent)) + template = env.get_template("playground_template.html") + rendered = template.render(id=id) + return [nodes.raw(text=rendered, format="html")] + + +def setup(app: Any) -> dict[str, Any]: + """ + Register the directive with Sphinx. + """ + app.add_js_file("https://cdn.jsdelivr.net/pyodide/v0.29.0/full/pyodide.js", priority=100) + app.add_directive("wasm-playground", WasmPlayground) + return {"version": "1.0", "parallel_read_safe": True, "parallel_write_safe": True} diff --git a/docs/_ext/playground_template.html b/docs/_ext/playground_template.html new file mode 100644 index 00000000..61cea4f1 --- /dev/null +++ b/docs/_ext/playground_template.html @@ -0,0 +1,92 @@ + +

🐍 Pyodide + SQLSpec Runner

+ +
+ + +

+
+  
+  
+
+  
+  
diff --git a/docs/conf.py b/docs/conf.py
index c3b47720..a88e59d6 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -61,6 +61,7 @@
     "sphinx_togglebutton",
     "sphinx_paramlinks",
     "sphinxcontrib.mermaid",
+    "docs._ext.playground",
 ]
 intersphinx_mapping = {
     "python": ("https://docs.python.org/3", None),
diff --git a/docs/index.rst b/docs/index.rst
index 5eac61cf..7c5dd52e 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -51,6 +51,7 @@ SQLSpec is **NOT an ORM**. It is a flexible connectivity layer that provides a c
     usage/index
     examples/index
     reference/index
+    playground
 
 .. toctree::
     :hidden:
diff --git a/docs/playground.rst b/docs/playground.rst
new file mode 100644
index 00000000..34cc46ef
--- /dev/null
+++ b/docs/playground.rst
@@ -0,0 +1,9 @@
+======================
+Live Playground
+======================
+
+You can try ``sqlspec`` live in your browser. The code block below
+is a full, interactive Python environment powered by Pyodide
+(WebAssembly).
+
+.. wasm-playground::