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.

examples: sidebar and slides examples (#2417)

Myles Scolnicka085b196ad41
diff --git a/examples/layouts/layouts/slides.slides.json b/examples/layouts/layouts/slides.slides.json
new file mode 100644
index 0000000000..3bcd0f545a
--- /dev/null
+++ b/examples/layouts/layouts/slides.slides.json
@@ -0,0 +1,4 @@
+{
+ "type": "slides",
+ "data": {}
+}
diff --git a/examples/layouts/sidebar.py b/examples/layouts/sidebar.py
new file mode 100644
index 0000000000..da8c2c914f
--- /dev/null
+++ b/examples/layouts/sidebar.py
@@ -0,0 +1,118 @@
+# /// script
+# requires-python = ">=3.11"
+# dependencies = [
+# "altair==5.4.1",
+# "marimo",
+# "vega-datasets==0.9.0",
+# ]
+# ///
+import marimo
+
+__generated_with = "0.8.20"
+app = marimo.App(width="full")
+
+
+def __():
+ # Imports
+ import marimo as mo
+ import altair as alt
+ from vega_datasets import data
+ return alt, data, mo
+
+
+def __(data):
+ # Data
+ gapminder = data.gapminder()
+ return (gapminder,)
+
+
+def __(gapminder, mo):
+ # Filters
+ year = mo.ui.slider.from_series(
+ gapminder["year"], full_width=True, label="Year", step=5
+ )
+ population = mo.ui.range_slider.from_series(
+ gapminder["pop"], full_width=True, label="Population"
+ )
+ return population, year
+
+
+def __(gapminder, population, year):
+ # Filter the dataset
+ filtered_data = gapminder[
+ (gapminder["year"] == year.value)
+ & (gapminder["pop"] > population.value[0])
+ & (gapminder["pop"] < population.value[1])
+ ]
+ return (filtered_data,)
+
+
+def __(alt, filtered_data, mo):
+ chart = mo.ui.altair_chart(
+ alt.Chart(filtered_data)
+ .mark_circle(opacity=0.7)
+ .encode(
+ x="fertility:Q",
+ y="life_expect:Q",
+ color="cluster:N",
+ size=alt.Size("pop:Q", scale=alt.Scale(range=[100, 2000])),
+ tooltip=[
+ "country:N",
+ "year:O",
+ "fertility:Q",
+ "life_expect:Q",
+ "pop:Q",
+ ],
+ )
+ .properties(height=400)
+ )
+ chart
+ return (chart,)
+
+
+def __(chart):
+ # Show the chart selection
+ chart.value if not chart.value.empty else None
+ return
+
+
+def __(mo, population, year):
+ mo.sidebar(
+ [
+ mo.md("# Gap Minder"),
+ mo.md(
+ "Scrub the year to see life expectancy go up and fertility go down"
+ ),
+ mo.vstack(
+ [
+ year,
+ mo.md(f"{int(year.value)}"),
+ population,
+ mo.md(
+ f"{int(population.value[0]):,} - {int(population.value[1]):,}"
+ ),
+ ]
+ ).style(padding="20px 10px"),
+ ],
+ footer=[
+ mo.md(
+ f"""
+ [{mo.icon("lucide:twitter")} Twitter](https://twitter.com/marimo_io)
+
+ [{mo.icon("lucide:github")} GitHub](https://github.com/marimo-team/marimo)
+ """
+ )
+ ],
+ )
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/examples/layouts/slides.py b/examples/layouts/slides.py
new file mode 100644
index 0000000000..21b51abc7d
--- /dev/null
+++ b/examples/layouts/slides.py
@@ -0,0 +1,234 @@
+# /// script
+# requires-python = ">=3.11"
+# dependencies = [
+# "duckdb==1.1.1",
+# "marimo",
+# ]
+# ///
+import marimo
+
+__generated_with = "0.8.20"
+app = marimo.App(width="medium", layout_file="layouts/slides.slides.json")
+
+
+def __(mo):
+ mo.md(
+ """
+ # DuckDB Tricks 🦆
+
+ ## We use a simple example data set to present a few tricks that are useful when using DuckDB.
+
+ >
+ > Turned into slides from <https://duckdb.org/2024/08/19/duckdb-tricks-part-1.html>
+ """
+ )
+ return
+
+
+def __(mo):
+ mo.md(
+ r"""
+ ## Installation
+
+ ```bash
+ uv pip install duckdb
+ ```
+ """
+ )
+ return
+
+
+def __(mo):
+ mo.md(r"""# Creating the example data set""")
+ return
+
+
+def __(duckdb, mo, print_and_run):
+ _SQL = """
+ CREATE OR REPLACE TABLE example (s STRING, x DOUBLE);
+ INSERT INTO example VALUES ('foo', 10/9), ('bar', 50/7), ('qux', 9/4);
+ COPY example TO 'example.csv';
+ """
+
+ duckdb.sql(_SQL)
+
+ mo.md(
+ f"""
+ Creating the example data set
+ We start by creating a data set that we'll use in the rest of the blog post. To this end, we define a table, populate it with some data and export it to a CSV file.
+
+ {print_and_run(_SQL)}
+
+ Wait a bit, that’s way too verbose! DuckDB’s syntax has several SQL shorthands including the “friendly SQL” clauses. Here, we combine the VALUES clause with the FROM-first syntax, which makes the SELECT clause optional. With these, we can compress the data creation script to ~60% of its original size. The new formulation omits the schema definition and creates the CSV with a single command:
+
+ ```sql
+ COPY (FROM VALUES ('foo', 10/9), ('bar', 50/7), ('qux', 9/4) t(s, x))
+ TO 'example.csv';
+ ```
+
+ Regardless of which script we run, the resulting CSV file will look like this:
+
+
+ {print_and_run("SELECT * from example")}
+ """
+ )
+ return
+
+
+def __(mo):
+ mo.md(r"""# Pretty-printing floating-point numbers""")
+ return
+
+
+def __(mo, print_and_run):
+ mo.md(
+ f"""
+ When printing a floating-point number to the output, the fractional parts can be difficult to read and compare. For example, the following query returns three numbers between 1 and 8 but their printed widths are very different due to their fractional parts.
+
+ {print_and_run("SELECT x FROM 'example.csv';")}
+
+ By casting a column to a DECIMAL with a fixed number of digits after the decimal point, we can pretty-print it as follows:
+
+ {print_and_run('''
+ SELECT x::DECIMAL(15, 3) AS x
+ FROM 'example.csv';
+ ''')}
+
+ A typical alternative solution is to use the printf or format functions, e.g.:
+
+ {print_and_run('''
+ SELECT printf('%.3f', x)
+ FROM 'example.csv';
+ ''')}
+
+ However, these approaches require us to specify a formatting string that's easy to forget. What's worse, the statement above returns string values, which makes subsequent operations (e.g., sorting) more difficult. Therefore, unless keeping the full precision of the floating-point numbers is a concern, casting to DECIMAL values should be the preferred solution for most use cases.
+ """
+ )
+ return
+
+
+def __(mo):
+ mo.md(r"""# Copying the schema of a table""")
+ return
+
+
+def __(mo, print_and_run):
+ mo.md(
+ f"""
+ To copy the schema from a table without copying its data, we can use LIMIT 0.
+
+ {print_and_run('''
+ CREATE OR REPLACE TABLE example AS
+ FROM 'example.csv';
+ CREATE OR REPLACE TABLE tbl AS
+ FROM example
+ LIMIT 0;
+ ''')}
+
+ This will result in an empty table with the same schema as the source table:
+
+ {print_and_run('DESCRIBE tbl;')}
+
+ This will return the schema of the table.
+
+ ```sql
+ CREATE TABLE example(s VARCHAR, x DOUBLE);
+ ```
+
+ After editing the table’s name (e.g., example to tbl), this query can be used to create a new table with the same schema.
+ """
+ )
+ return
+
+
+def __(mo):
+ mo.md(r"""# Shuffling data""")
+ return
+
+
+def __(mo):
+ rerun = mo.ui.button(label="Run again")
+ return (rerun,)
+
+
+def __(mo, print_and_run, rerun):
+ mo.md(
+ f"""
+ Sometimes, we need to introduce some entropy into the ordering of the data by shuffling it. To shuffle non-deterministically, we can simply sort on a random value provided the random() function:
+
+ {rerun}
+
+ {print_and_run('''
+ FROM 'example.csv' ORDER BY random();
+ ''')}
+
+ Shuffling deterministically is a bit more tricky. To achieve this, we can order on the hash, of the rowid pseudocolumn. Note that this column is only available in physical tables, so we first have to load the CSV in a table, then perform the shuffle operation as follows:
+
+ {rerun}
+
+ {print_and_run('''
+ CREATE OR REPLACE TABLE example AS FROM 'example.csv';
+ FROM example ORDER BY hash(rowid + 42);
+ ''')}
+
+ Note that the + 42 is only necessary to nudge the first row from its position – as hash(0) returns 0, the smallest possible value, using it for ordering leaves the first row in its place.
+ """
+ )
+ return
+
+
+def __(mo):
+ mo.md(
+ """
+ # Closing thoughts
+
+ That’s it for today. The tricks shown in this post are available on [duckdbsnippets.com](https://duckdbsnippets.com).
+ """
+ )
+ return
+
+
+def __(duckdb, mo):
+ # Utils
+
+
+ def print_and_run(sql: str):
+ result = duckdb.sql(sql)
+ sql = sql.strip()
+ if not result:
+ return f"""
+ ```sql
+ {sql}
+ ```
+ """
+ return f"""
+ ```sql
+ {sql}
+ ```
+ {mo.ui.table(result.df(), selection=None, pagination=None, show_column_summaries=False)}
+ """
+ return (print_and_run,)
+
+
+def __():
+ import marimo as mo
+ import duckdb
+ return duckdb, mo
+
+
+if __name__ == "__main__":
+ app.run()