-
Notifications
You must be signed in to change notification settings - Fork 3
/
tldraw_colorpicker.py
71 lines (55 loc) · 1.79 KB
/
tldraw_colorpicker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "marimo",
# "matplotlib",
# "numpy",
# "tldraw",
# ]
# ///
import marimo
__generated_with = "0.8.9"
app = marimo.App(width="medium")
@app.cell
def __():
import marimo as mo
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('_mpl-gallery')
return mo, np, plt
@app.cell
def __(mo):
from tldraw import ReactiveColorPicker
widget = mo.ui.anywidget(ReactiveColorPicker())
return ReactiveColorPicker, widget
@app.cell(hide_code=True)
def __(mo):
mo.md(
"""
This example by [Jan-Hendrik Muller](https://x.com/kolibril13?lang=en) uses [anywidget](https://anywidget.dev) to make
a reactive colorpicker! Drag the arrow across the boxes, and watch the scatterplot change colors.
anywidget is a Python library that makes it easy to make portable widgets; marimo has standardized on anywidget
for its plugin API. Learn more at [our docs](https://docs.marimo.io/guides/integrating_with_marimo/custom_ui_plugins.html#custom-widget).
"""
)
return
@app.cell
def __(np):
# make the data
np.random.seed(3)
x = 4 + np.random.normal(0, 2, 24)
y = 4 + np.random.normal(0, 2, len(x))
# size and color:
sizes = np.random.uniform(15, 80, len(x))
opacity = np.random.uniform(0, 1, len(x))
return opacity, sizes, x, y
@app.cell
def __(mo, np, opacity, plt, sizes, widget, x, y):
fig, ax = plt.subplots()
fig.set_size_inches(3, 3)
ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8))
ax.scatter(x, y, s=sizes*5, color=widget.color or None, alpha=opacity)
mo.hstack([widget, plt.gca()], justify="start", widths=[1, 1])
return ax, fig
if __name__ == "__main__":
app.run()