November 7, 2025
Model Risk Seminar
WEB
git diff changesgit diff changesmarimo notebooks run directly as Python scripts
marimo
example_notebook.py# /// script
# requires-python = ">=3.12"
# dependencies = [
# "altair==5.5.0",
# "duckdb==1.3.2",
# "marimo",
# "matplotlib==3.10.3",
# "numpy==2.3.2",
# "pandas==2.3.3",
# "polars==1.31.0",
# "pyarrow==21.0.0",
# "sqlglot==27.3.1",
# ]
# ///
import marimo
__generated_with = "0.17.7"
app = marimo.App(width="medium", css_file="", sql_output="native")
@app.cell
def _():
import marimo as mo
import polars as pl
import json
import altair as alt
return alt, mo, pl
@app.cell
def _(mo):
mo.md(r"""
## Example: No hidden-state
""")
return
@app.cell
def _():
x = 333
x
return (x,)
@app.cell
def _(x):
y = x + 15
y
return (y,)
@app.cell
def _(x, y):
z = x + y
z
return
@app.cell
def _(mo):
mo.md(r"""
## Example: No redefining variables
""")
return
@app.cell
def _():
# Try to redefine x
# x = 7
return
@app.cell(hide_code=True)
def _(mo):
mo.md(r"""
## Example: Reactive plot
""")
return
@app.cell
def _(mo):
mo.md(r"""
### Load data
""")
return
@app.cell
def _(pl):
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']
dataset = pl.read_csv(url, has_header=False, new_columns=names)
return (dataset,)
@app.cell
def _(dataset):
# Inspect data
dataset
return
@app.cell
def _(mo):
mo.md(r"""
### Iris dataset scatter-plot
""")
return
@app.cell
def _():
x_var = "petal_width"
return (x_var,)
@app.cell
def _():
y_var = "sepal_width"
return (y_var,)
@app.cell
def _(mo, x_var, y_var):
mo.md(rf"""
## Chart with <span class="highlight-red">x_var</span>={x_var} and <span class="highlight-red">y_var</span>={y_var}.
""")
return
@app.cell
def _(alt, dataset, x_var, y_var):
# Chart with parameterized x_var and y_var:
(
alt.Chart(dataset).mark_point(tooltip=True).encode(
x=x_var,
y=y_var,
color="class",
)
.properties(width=500)
.configure_scale(zero=False)
)
return
@app.cell
def _(mo):
mo.md(r"""
## Example: Interactive widgets
""")
return
@app.cell
def _():
import math
import numpy as np
import matplotlib.pyplot as plt
return np, plt
@app.cell(hide_code=True)
def _(mo):
b = mo.ui.slider(start=0.1, step=0.01, stop= 1, label="b")
a = mo.ui.slider(start=1, step=0.50, stop= 3, label="a")
return a, b
@app.cell
def _(a, b, mo):
mo.hstack([b, mo.md("b coefficient"), a, mo.md("a exponent") ])
return
@app.cell(hide_code=True)
def _(a, b, mo):
mo.md(rf"""
Let's plot the function: $f(x)=b x^{{a}} = {b.value:0.1f} x^{{{a.value:0.1f}}}$ over the interval $[0, 10]$.
""")
return
@app.cell(hide_code=True)
def _(a, b, np, plt):
v = np.linspace(0, 10)
plt.plot(v, b.value*v**(a.value))
plt.gca()
return
@app.cell
def _(mo):
mo.md(r"""
## Example: Using `SQL`
""")
return
@app.cell
def _(dataset, mo):
df_sql = mo.sql(
f"""
SELECT
class,
MEAN(sepal_length) AS avg_sepal_len,
SUM(sepal_width) AS sum_sepal_width
FROM
dataset
GROUP BY
class
ORDER BY
class;
"""
)
return (df_sql,)
@app.cell
def _(df_sql):
df_sql
return
@app.cell
def _(df_sql):
# Output of SQL query is DuckDb native
# Use .pl() method to obtain a polars dataframe
df_sql.pl()
return
@app.cell
def _(alt, df_sql):
# Altair bar chart
alt.Chart(df_sql.pl()).mark_bar(tooltip=True).encode(
x='class',
y='avg_sepal_len'
)
return
@app.cell
def _(alt, df_sql):
# Shorthand using polars plot method
df_sql.pl().plot.bar(x=alt.X("class"), y = "avg_sepal_len")
return
@app.cell
def _(alt, df_sql):
# Scatter plot
df_sql.pl().plot.point(x=alt.X("avg_sepal_len"), y = "sum_sepal_width", color = "class")
return
if __name__ == "__main__":
app.run()