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: add great_tables (#2099)

Myles Scolnickfebbb87481f8
diff --git a/examples/requirements.txt b/examples/requirements.txt
index 8cb729259e..ddbc5401c5 100644
--- a/examples/requirements.txt
+++ b/examples/requirements.txt
@@ -42,3 +42,4 @@ pygwalker
maplibre
mosaic_widget
geopandas
+great_tables
diff --git a/examples/tables/coffee-sales.json b/examples/tables/coffee-sales.json
new file mode 100644
index 0000000000..cc23ec9209
--- /dev/null
+++ b/examples/tables/coffee-sales.json
@@ -0,0 +1,204 @@
+{
+ "columns": [
+ {
+ "name": "icon",
+ "datatype": "String",
+ "bit_settings": "",
+ "values": [
+ "grinder.png",
+ "moka-pot.png",
+ "cold-brew.png",
+ "filter.png",
+ "drip-machine.png",
+ "aeropress.png",
+ "pour-over.png",
+ "french-press.png",
+ "cezve.png",
+ "chemex.png",
+ "scale.png",
+ "kettle.png",
+ "espresso-machine.png",
+ null
+ ]
+ },
+ {
+ "name": "product",
+ "datatype": "String",
+ "bit_settings": "",
+ "values": [
+ "Grinder",
+ "Moka pot",
+ "Cold brew",
+ "Filter",
+ "Drip machine",
+ "AeroPress",
+ "Pour over",
+ "French press",
+ "Cezve",
+ "Chemex",
+ "Scale",
+ "Kettle",
+ "Espresso Machine",
+ "Total"
+ ]
+ },
+ {
+ "name": "revenue_dollars",
+ "datatype": "Float64",
+ "bit_settings": "",
+ "values": [
+ 904500.0, 2045250.0, 288750.0, 404250.0, 2632000.0, 2601500.0, 846000.0,
+ 1113250.0, 2512500.0, 3137250.0, 3801000.0, 756250.0, 8406000.0,
+ 29448500.0
+ ]
+ },
+ {
+ "name": "revenue_pct",
+ "datatype": "Float64",
+ "bit_settings": "",
+ "values": [
+ 0.03, 0.07, 0.01, 0.01, 0.09, 0.09, 0.03, 0.04, 0.09, 0.11, 0.13, 0.03,
+ 0.29, 1.0
+ ]
+ },
+ {
+ "name": "profit_dollars",
+ "datatype": "Float64",
+ "bit_settings": "",
+ "values": [
+ 567960.0, 181080.0, 241770.0, 70010.0, 1374450.0, 1293780.0, 364530.0,
+ 748120.0, 1969520.0, 817680.0, 2910290.0, 617520.0, 3636440.0,
+ 14793150.0
+ ]
+ },
+ {
+ "name": "profit_pct",
+ "datatype": "Float64",
+ "bit_settings": "",
+ "values": [
+ 0.04, 0.01, 0.02, 0.0, 0.09, 0.09, 0.02, 0.05, 0.13, 0.06, 0.2, 0.04,
+ 0.25, 1.0
+ ]
+ },
+ {
+ "name": "monthly_sales",
+ "datatype": { "List": "Int64" },
+ "bit_settings": "",
+ "values": [
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [521, 494, 596, 613, 667, 748, 765, 686, 607, 594, 568, 751]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 4726, 4741, 4791, 5506, 6156, 6619, 6868, 6026, 5304, 4884, 4648,
+ 6283
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 244, 249, 438, 981, 1774, 2699, 2606, 2348, 1741, 896, 499, 244
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 2067, 1809, 1836, 2123, 2252, 2631, 2562, 2367, 2164, 2195, 2070,
+ 2744
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 2137, 1623, 1971, 2097, 2580, 2456, 2336, 2316, 2052, 1967, 1837,
+ 2328
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 6332, 5199, 6367, 7024, 7906, 8704, 8693, 7797, 6828, 6963, 6877,
+ 9270
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 1562, 1291, 1511, 1687, 1940, 2177, 2141, 1856, 1715, 1806, 1601,
+ 2165
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 3507, 2880, 3346, 3792, 3905, 4095, 4184, 4428, 3279, 3420, 3297,
+ 4819
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 12171, 11469, 11788, 13630, 15391, 16532, 17090, 14433, 12985,
+ 12935, 11598, 15895
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 4938, 4167, 5235, 6000, 6358, 6768, 7112, 6249, 5605, 6076, 4980,
+ 7220
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 1542, 1566, 1681, 2028, 2425, 2549, 2569, 2232, 2036, 2089, 1693,
+ 3180
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 1139, 1023, 1087, 1131, 1414, 1478, 1456, 1304, 1140, 1233, 1193,
+ 1529
+ ]
+ },
+ {
+ "name": "",
+ "datatype": "Int64",
+ "bit_settings": "",
+ "values": [
+ 686, 840, 618, 598, 2148, 533, 797, 996, 1002, 668, 858, 2577
+ ]
+ },
+ null
+ ]
+ }
+ ]
+}
diff --git a/examples/tables/great_tables_example.py b/examples/tables/great_tables_example.py
new file mode 100644
index 0000000000..b6ef51264f
--- /dev/null
+++ b/examples/tables/great_tables_example.py
@@ -0,0 +1,109 @@
+import marimo
+
+__generated_with = "0.8.2"
+app = marimo.App(width="medium")
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md(
+ r"""
+ # [Great-tables](https://github.com/posit-dev/great-tables) + marimo
+
+ Adapted from https://github.com/machow/coffee-sales-data
+ """
+ )
+ return
+
+
+def __(__file__):
+ import polars as pl
+ import polars.selectors as cs
+ import os
+ import marimo as mo
+ from pathlib import Path
+ from great_tables import GT, loc, style
+
+ current_dir = os.path.dirname(os.path.realpath(__file__))
+ coffee_sales = pl.DataFrame.deserialize(
+ Path(current_dir) / "coffee-sales.json", format="json"
+ )
+ return GT, Path, coffee_sales, cs, current_dir, loc, mo, os, pl, style
+
+
+def __(coffee_sales, mo):
+ revenue = mo.ui.range_slider.from_series(coffee_sales["revenue_dollars"])
+ return revenue,
+
+
[email protected](hide_code=True)
+def __(mo, revenue):
+ mo.hstack(
+ [revenue, f"${revenue.value[0]:,.0f} - ${revenue.value[1]:,.0f}"]
+ ).left()
+ return
+
+
[email protected](hide_code=True)
+def __(coffee_sales, revenue):
+ lower = revenue.value[0]
+ upper = revenue.value[1]
+ filterered_coffee_sales = coffee_sales.filter(
+ (coffee_sales["revenue_dollars"] >= lower)
+ & (coffee_sales["revenue_dollars"] <= upper)
+ )
+ return filterered_coffee_sales, lower, upper
+
+
[email protected](hide_code=True)
+def __(GT, cs, filterered_coffee_sales, loc, pl, style):
+ sel_rev = cs.starts_with("revenue")
+ sel_prof = cs.starts_with("profit")
+
+ coffee_table = (
+ GT(filterered_coffee_sales)
+ .tab_header("Sales of Coffee Equipment")
+ .tab_spanner(label="Revenue", columns=sel_rev)
+ .tab_spanner(label="Profit", columns=sel_prof)
+ .cols_label(
+ revenue_dollars="Amount",
+ profit_dollars="Amount",
+ revenue_pct="Percent",
+ profit_pct="Percent",
+ monthly_sales="Monthly Sales",
+ icon="",
+ product="Product",
+ )
+ # formatting ----
+ .fmt_number(
+ columns=cs.ends_with("dollars"),
+ compact=True,
+ pattern="${x}",
+ n_sigfig=3,
+ )
+ .fmt_percent(columns=cs.ends_with("pct"), decimals=0)
+ # style ----
+ .tab_style(
+ style=style.fill(color="aliceblue"),
+ locations=loc.body(columns=sel_rev),
+ )
+ .tab_style(
+ style=style.fill(color="papayawhip"),
+ locations=loc.body(columns=sel_prof),
+ )
+ .tab_style(
+ style=style.text(weight="bold"),
+ locations=loc.body(rows=pl.col("product") == "Total"),
+ )
+ .fmt_nanoplot("monthly_sales", plot_type="bar")
+ .sub_missing(missing_text="")
+ )
+
+ coffee_table
+ return coffee_table, sel_prof, sel_rev
+
+
+if __name__ == "__main__":
+ app.run()