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: duckdb (for duckdb docs) (#4500)

Myles Scolnick12e9338203f7
diff --git a/examples/sql/duckdb_example.py b/examples/sql/duckdb_example.py
new file mode 100644
index 0000000000..68ed1b44ba
--- /dev/null
+++ b/examples/sql/duckdb_example.py
@@ -0,0 +1,54 @@
+# /// script
+# requires-python = ">=3.12"
+# dependencies = [
+# "altair==5.5.0",
+# "polars[pyarrow]==1.27.1",
+# "marimo[sql]",
+# "duckdb==1.2.2",
+# "sqlglot==26.13.0",
+# ]
+# ///
+
+import marimo
+
+__generated_with = "0.12.8"
+app = marimo.App(width="medium", sql_output="polars")
+
+
+def _():
+ import marimo as mo
+ import altair as alt
+ return alt, mo
+
+
+def _(mo):
+ digits = mo.ui.slider(label="Digits", start=100, stop=10000, step=200)
+ digits
+ return (digits,)
+
+
+def _(digits, mo):
+ result = mo.sql(
+ f"""
+ CREATE TABLE random_data AS
+ SELECT i AS id, RANDOM() AS random_value,
+ FROM range({digits.value}) AS t(i);
+
+ SELECT * FROM random_data;
+ """
+ )
+ return random_data, result
+
+
+def _(alt, result):
+ # Plot the data using polars and altair
+ result.plot.bar(x=alt.X("random_value").bin(), y="count()")
+ return
+
+
+if __name__ == "__main__":
+ app.run()