Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

example: fasthtml (#2688)

Myles Scolnicke4b140dc508f
diff --git a/examples/frameworks/README.md b/examples/frameworks/README.md
index 262a2223d8..a92ca7ffa7 100644
--- a/examples/frameworks/README.md
+++ b/examples/frameworks/README.md
@@ -1,6 +1,6 @@
# Frameworks 🧩
-These examples show how to use marimo with various web/ASGI frameworks.
+These examples show how to use marimo with various web/ASGI frameworks, such as FastAPI, Flask, and FastHTML.
> [!TIP]
> Submit a
diff --git a/examples/frameworks/fastapi/main.py b/examples/frameworks/fastapi/main.py
index 0d2da7a4e4..bb016dd401 100644
--- a/examples/frameworks/fastapi/main.py
+++ b/examples/frameworks/fastapi/main.py
@@ -37,7 +37,7 @@ templates_dir = os.path.join(os.path.dirname(__file__), "templates")
server = marimo.create_asgi_app()
app_names: list[str] = []
-for filename in os.listdir(ui_dir):
+for filename in sorted(os.listdir(ui_dir)):
if filename.endswith(".py"):
app_name = os.path.splitext(filename)[0]
app_path = os.path.join(ui_dir, filename)
diff --git a/examples/frameworks/fasthtml/.gitignore b/examples/frameworks/fasthtml/.gitignore
new file mode 100644
index 0000000000..c089b4e1f6
--- /dev/null
+++ b/examples/frameworks/fasthtml/.gitignore
@@ -0,0 +1 @@
+.sesskey
diff --git a/examples/frameworks/fasthtml/README.md b/examples/frameworks/fasthtml/README.md
new file mode 100644
index 0000000000..b151099878
--- /dev/null
+++ b/examples/frameworks/fasthtml/README.md
@@ -0,0 +1,8 @@
+# FastHTML + marimo
+
+This is a simple example of how to use FastHTML with marimo. This example programmatically creates multiple marimo apps from a directory, and then serves them as a single FastHTML app.
+
+## Running the app
+
+1. [Install `uv`](https://github.com/astral-sh/uv/?tab=readme-ov-file#installation)
+2. Run the app with `uv run --no-project main.py`
diff --git a/examples/frameworks/fasthtml/main.py b/examples/frameworks/fasthtml/main.py
new file mode 100644
index 0000000000..dc76a8a33a
--- /dev/null
+++ b/examples/frameworks/fasthtml/main.py
@@ -0,0 +1,70 @@
+# /// script
+# requires-python = ">=3.12"
+# dependencies = [
+# "python-fasthtml",
+# "marimo",
+# ]
+# ///
+from fasthtml.common import *
+import marimo
+import os
+
+ui_dir = os.path.join(os.path.dirname(__file__), "..", "..", "ui")
+templates_dir = os.path.join(os.path.dirname(__file__), "templates")
+
+server = marimo.create_asgi_app()
+app_names: list[str] = []
+
+for filename in sorted(os.listdir(ui_dir)):
+ if filename.endswith(".py"):
+ app_name = os.path.splitext(filename)[0]
+ app_path = os.path.join(ui_dir, filename)
+ server = server.with_app(path=f"/{app_name}", root=app_path)
+ app_names.append(app_name)
+
+
+# Loading tailwind and daisyui
+headers = (
+ Script(src="https://cdn.tailwindcss.com"),
+ Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css")
+)
+
+
+app = FastHTML()
+
+def home():
+ return (
+ *headers,
+ Title("marimo apps"),
+ Main(
+ Div(
+ H1("marimo dashboard", cls="text-4xl font-bold mb-8 text-center"),
+ Div(
+ *[
+ Div(
+ A(
+ Div(
+ H2(app_name, cls="text-xl font-semibold mb-2"),
+ P("Click to open app", cls="text-sm text-gray-500"),
+ cls="p-4"
+ ),
+ href=f"/{app_name}",
+ target="_blank",
+ cls="block w-full h-full"
+ ),
+ cls="bg-base-200 rounded-lg shadow-md hover:shadow-xl transition-shadow duration-300 w-64"
+ ) for app_name in app_names
+ ],
+ cls="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6"
+ ),
+ cls="container mx-auto px-4 py-16 bg-base-100 rounded-box shadow-xl"
+ ),
+ cls="min-h-screen bg-gradient-to-br from-base-200 to-base-300 p-8"
+ )
+ )
+
+app.mount("/", server.build())
+
+if __name__ == "__main__":
+ serve()
diff --git a/examples/frameworks/flask/main.py b/examples/frameworks/flask/main.py
index f511d53aee..14c68789db 100644
--- a/examples/frameworks/flask/main.py
+++ b/examples/frameworks/flask/main.py
@@ -43,7 +43,7 @@ users = {"admin": generate_password_hash("password123")}
app_names: list[str] = []
marimo_app = marimo.create_asgi_app()
-for filename in os.listdir(ui_dir):
+for filename in sorted(os.listdir(ui_dir)):
if filename.endswith(".py"):
app_name = os.path.splitext(filename)[0]
app_path = os.path.join(ui_dir, filename)