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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
add example notebook using columns (#3530)
Akshay Agrawalcde1fd52b64c
diff --git a/examples/layouts/README.md b/examples/layouts/README.mdindex b1248f2e28..41851237a0 100644--- a/examples/layouts/README.md+++ b/examples/layouts/README.md@@ -1,8 +1,8 @@# Layouts 📽️These examples show how to use marimo's built-in features for laying out-notebooks in interesting ways, such as presenting notebooks as slides and-adding sidebars.+notebooks in interesting ways, such as presenting notebooks as slides,+adding sidebars, and arranging cells into columns while editing.> [!TIP]> Submit adiff --git a/examples/layouts/columns.py b/examples/layouts/columns.pynew file mode 100644index 0000000000..96572dc3f5--- /dev/null+++ b/examples/layouts/columns.py@@ -0,0 +1,175 @@+# /// script+# requires-python = ">=3.12"+# dependencies = [+# "altair==5.5.0",+# "marimo",+# "matplotlib==3.10.0",+# "pandas==2.2.3",+# "polars==1.20.0",+# "scikit-learn==1.6.1",+# ]+# ///++import marimo++__generated_with = "0.10.15"+app = marimo.App(width="columns")++[email protected](column=0)+def _():+ import marimo as mo+ return (mo,)+++async def _():+ import sys++ if "pyodide" in sys.modules:+ import micropip++ await micropip.install("altair")++ import altair as alt+ return alt, micropip, sys+++def _():+ import sklearn+ import sklearn.datasets+ import sklearn.manifold+ return (sklearn,)+++def _():+ import polars as pl+ return (pl,)+++def _(alt):+ def scatter(df):+ return (+ alt.Chart(df)+ .mark_circle()+ .encode(+ x=alt.X("x:Q").scale(domain=(-2.5, 2.5)),+ y=alt.Y("y:Q").scale(domain=(-2.5, 2.5)),+ color=alt.Color("digit:N"),+ )+ .properties(width=500, height=500)+ )+ return (scatter,)+++def _(raw_digits):+ def show_images(indices, max_images=10):+ import matplotlib.pyplot as plt++ indices = indices[:max_images]+ images = raw_digits.reshape((-1, 8, 8))[indices]+ fig, axes = plt.subplots(1, len(indices))+ fig.set_size_inches(12.5, 1.5)+ if len(indices) > 1:+ for im, ax in zip(images, axes.flat):+ ax.imshow(im, cmap="gray")+ ax.set_yticks([])+ ax.set_xticks([])+ else:+ axes.imshow(images[0], cmap="gray")+ axes.set_yticks([])+ axes.set_xticks([])+ plt.tight_layout()+ return fig+ return (show_images,)++[email protected](column=1, hide_code=True)+def _(mo):+ mo.md("""# Embedding Visualizer""")+ return++[email protected](hide_code=True)+def _(mo):+ mo.md(+ """+ Here's a PCA **embedding of numerical digits**: each point represents a+ digit, with similar digits close to each other. The data is from the UCI+ ML handwritten digits dataset.++ This notebook will automatically drill down into points you **select with+ your mouse**; try it!+ """+ )+ return+++def _(sklearn):+ raw_digits, raw_labels = sklearn.datasets.load_digits(return_X_y=True)+ return raw_digits, raw_labels+++def _(pl, raw_digits, raw_labels, sklearn):+ X_embedded = sklearn.decomposition.PCA(+ n_components=2, whiten=True+ ).fit_transform(raw_digits)++ embedding = pl.DataFrame(+ {+ "x": X_embedded[:, 0],+ "y": X_embedded[:, 1],+ "digit": raw_labels,+ "index": list(range(X_embedded.shape[0])),+ }+ )+ return X_embedded, embedding+++def _(embedding, mo, scatter):+ chart = mo.ui.altair_chart(scatter(embedding))+ chart+ return (chart,)+++def _(chart, mo):+ table = mo.ui.table(chart.value)+ return (table,)+++def _(chart, mo, show_images, table):+ # show 10 images: either the first 10 from the selection, or the first ten+ # selected in the table+ mo.stop(not len(chart.value))++ selected_images = (+ show_images(list(chart.value["index"]))+ if not len(table.value)+ else show_images(list(table.value["index"]))+ )++ mo.md(+ f"""+ **Here's a preview of the images you've selected**:++ {mo.as_html(selected_images)}++ Here's all the data you've selected.++ {table}+ """+ )+ return (selected_images,)+++if __name__ == "__main__":+ app.run()