Skip to content

Instantly share code, notes, and snippets.

@rikonor
Last active January 4, 2025 21:26
Show Gist options
  • Save rikonor/ffcec0055268856f893f66dbe74ffbb8 to your computer and use it in GitHub Desktop.
Save rikonor/ffcec0055268856f893f66dbe74ffbb8 to your computer and use it in GitHub Desktop.
Marimo Playground
import marimo
__generated_with = "0.10.9"
app = marimo.App()
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell
def _(mo):
mo.md(f"""
# Introduction
Testing { 5 }
""").callout(kind="info")
return
@app.cell
def _(mo):
mo.accordion(
{
"name": "Or",
"age": 36,
},
lazy=True,
)
return
@app.cell
def _(mo):
import numpy as np
# linear steps
slider = mo.ui.slider(steps=np.array([1, 2, 3, 4, 5]))
# log steps
log_slider = mo.ui.slider(steps=np.logspace(0, 3, 4))
# power steps
power_slider = mo.ui.slider(steps=np.power([1, 2, 3], 2))
return log_slider, np, power_slider, slider
@app.cell
def _(log_slider, mo, power_slider, slider):
mo.md(f"""
{slider} {slider.value}
{log_slider} {log_slider.value}
{power_slider} {power_slider.value}
""")
return
@app.cell
def _():
_a = 1
return
@app.cell
def _():
_a = 1
return
@app.cell
def _(mo):
_people = {
"Or": 36,
"Laura": 35,
"Sadie": 7,
"Jacob": 3,
}
_options = {
name: {
"name": name,
"age": age,
}
for name, age in _people.items()
}
dd = mo.ui.dropdown(
options=_options,
value="Or",
label="Pick a person",
)
return (dd,)
@app.cell
def _(dd, mo):
mo.md(f"""
# Person Selector
{ dd }
{ dd.value["name"] } is { dd.value["age"] } years old
""")
return
@app.cell
def _(mo):
print("message a")
print("message b")
print("message c")
el = mo.md("# Test")
print(el.text)
el
return (el,)
@app.cell(disabled=True)
def _():
# disable me
return
@app.cell
def _():
import functools
@functools.cache
def add(a, b):
print(f"Calling add with {a} and {b}")
return a + b
print(add(1, 2))
print(add(1, 3))
print(add(1, 4))
print(add(1, 2))
print(add(1, 3))
print(add(1, 4))
return add, functools
@app.cell
def _(mo):
# mo.ui.number()
# mo.ui.text()
# mo.ui.checkbox()
# mo.ui.text_area(placeholder="Positive Prompt...")
mo.ui.button(on_click=lambda _: print("clicked"))
return
@app.cell
def _(mo):
button = mo.ui.button(
value=0,
on_click=lambda n: n + 1,
label="Increment",
)
return (button,)
@app.cell
def _(button, mo):
mo.md(f"{button} {button.value}")
return
@app.cell
def _(mo):
file = mo.ui.file(kind="area")
file
return (file,)
@app.cell
def _(file):
file.value
return
@app.cell
def _(mo):
mo.ui.tabs(
{
"Tab 1": mo.ui.button(),
"Tab 2": mo.ui.text_area(),
}
)
return
@app.cell
def _(mo):
mo.ui.array(
[
mo.ui.text(label="Text 1"),
mo.ui.text(label="Text 2"),
mo.ui.text(label="Text 3"),
]
)
return
@app.cell
def _(mo):
mo.ui.dictionary(
{
"text-1": mo.ui.text(placeholder="Type something..."),
"text-2": mo.ui.text(),
"text-3": mo.ui.text(),
}
)
return
@app.cell
def _(mo):
mo.md(r"""$f : \mathbf{R} \to \mathbf{R}$""")
return
@app.cell
def _(mo):
mo.md(
r"""
\[
f: \mathbf{R} \to \mathbf{R}
\]
"""
)
return
@app.cell
def _(latex, mo, symbols):
def __():
x, y = symbols("x y")
expr = x**2 + y**2
return mo.md(f"""
Let's examine some common expressions, such as {latex(expr, mode="inline")}.
{latex(expr, mode="equation*")}
""")
__()
return
@app.cell
def _(Eq, latex, mo, symbols):
def __():
out = ""
x, y = symbols("x y")
lhs, rhs = x**2 + y**2, y
out += "\n\n" + latex(Eq(lhs, rhs), mode="equation*")
lhs, rhs = lhs / x, rhs / x
out += "\n\n" + latex(Eq(lhs, rhs), mode="equation*")
lhs, rhs = lhs + 3, rhs + 3
out += "\n\n" + latex(Eq(lhs, rhs), mode="equation*")
return out
mo.md(__())
return
@app.cell
def _(Eq):
class Equation:
def __init__(self, lhs, rhs):
self.lhs = lhs
self.rhs = rhs
def __iadd__(self, other):
return Equation(
self.lhs + other,
self.rhs + other,
)
def __isub__(self, other):
return Equation(
self.lhs - other,
self.rhs - other,
)
def __imul__(self, other):
return Equation(
self.lhs * other,
self.rhs * other,
)
def __itruediv__(self, other):
return Equation(
self.lhs / other,
self.rhs / other,
)
def apply(self, op):
return Equation(
op(self.lhs),
op(self.rhs),
)
def subs(self, src, dst):
return Equation(
self.lhs.subs(src, dst),
self.rhs.subs(src, dst),
)
def latex(self, mode="plain"):
return latex(Eq(self.lhs, self.rhs), mode=mode)
return (Equation,)
@app.cell
def _(Equation, mo, symbols):
def __():
x, y = symbols("x y")
out = []
mode = "equation*"
eq = Equation(x**2 + y**2, y)
out.append(eq.latex(mode))
eq += 3
out.append(eq.latex(mode))
eq -= 3
out.append(eq.latex(mode))
eq /= 4
out.append(eq.latex(mode))
eq = eq.apply(lambda expr: expr**2)
out.append(eq.latex(mode))
u, t = symbols("u t")
eq = eq.subs(x, 1 + u)
out.append(eq.latex(mode))
eq = eq.subs(y, 1 + t)
out.append(eq.latex(mode))
eq = eq.subs(u, 1)
out.append(eq.latex(mode))
return out
mo.md("\n\n".join(__()))
return
@app.cell
def _(Equation, Function, mo, symbols):
def __():
x = symbols("x")
f = Function("f")(x)
out = []
mode = "equation*"
eq = Equation(f, x**2 + x + 1)
out.append(eq.latex(mode))
out.append(eq.subs(x, 1).latex(mode))
out.append(eq.subs(x, 2).latex(mode))
out.append(eq.subs(x, 3).latex(mode))
return out
mo.md("\n\n".join(__()))
return
@app.cell
def _(Equation, Function, Integral, mo, symbols):
def __():
x, a, b = symbols("x a b")
f = Function("f")(a, b)
out = []
mode = "equation*"
expr = Integral(
x**2 + x + 1,
(x, 0, b),
(x, 0, a),
)
eq = Equation(f, expr)
out.append(eq.latex(mode))
return out
mo.md("\n\n".join(__()))
return
@app.cell
def _():
from sympy import symbols, latex, Eq, Function, Integral
return Eq, Function, Integral, latex, symbols
if __name__ == "__main__":
app.run()
import marimo
__generated_with = "0.9.31-dev0"
app = marimo.App()
@app.cell(hide_code=True)
def __():
# imports
import marimo as mo
import numpy as np
import matplotlib.pyplot as plt
return mo, np, plt
@app.cell(hide_code=True)
def __(a, b, c, np):
# grid evaluation
x = y = np.linspace(-6, 6, num=128, endpoint=True)
xx, yy = np.meshgrid(x, y, indexing="ij")
lhs = a * xx**2 + b * yy**2 + c * xx * yy
return lhs, x, xx, y, yy
@app.cell(hide_code=True)
def __(mo):
# sliders
a_slider = mo.ui.slider(start=-10, stop=10, step=0.05, label="a")
b_slider = mo.ui.slider(start=-10, stop=10, step=0.05, label="b")
c_slider = mo.ui.slider(start=-10, stop=10, step=0.05, label="c")
return a_slider, b_slider, c_slider
@app.cell(hide_code=True)
def __(a_slider, b_slider, c_slider):
# slider values for convenience
a = a_slider.value
b = b_slider.value
c = c_slider.value
rhs = c
return a, b, c, rhs
@app.cell(hide_code=True)
def __(lhs, plt, rhs, xx, yy):
# contour visualization
fig, ax = plt.subplots(figsize=(6, 6))
cs = ax.contour(xx, yy, lhs, levels=[rhs])
ax.clabel(cs, fontsize=10)
ax.set(aspect="equal", xticks=[], yticks=[])
None
return ax, cs, fig
@app.cell(hide_code=True)
def __(a_slider, b_slider, c_slider, fig, mo):
# layout
mo.vstack(
[
mo.hstack([a_slider, b_slider, c_slider]).center(),
mo.as_html(fig).center(),
]
)
return
@app.cell
def __():
return
if __name__ == "__main__":
app.run()
import marimo
__generated_with = "0.10.9"
app = marimo.App(width="medium")
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell
def _():
import qrcode
return (qrcode,)
@app.cell
def _(qrcode):
img = qrcode.make('https://withered-wind-4b59.rikonor.workers.dev/')
img
return (img,)
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment