|
"""A reactive image viewer that allows you to select a class and see 16 random |
|
images from that class. |
|
|
|
This is a tutorial on how to use `reactive` functions in Meerkat, to |
|
build complex reactive workflows. |
|
""" |
|
|
|
import meerkat as mk |
|
|
|
df = mk.get("imagenette", version="160px") |
|
IMAGE_COL = "img" |
|
LABEL_COL = "label" |
|
|
|
|
|
@mk.reactive() |
|
def random_images(df: mk.DataFrame): |
|
images = df.sample(16)[IMAGE_COL] |
|
formatter = images.formatters["base"] |
|
|
|
return [formatter.encode(img) for img in images] |
|
|
|
|
|
labels = list(df[LABEL_COL].unique()) |
|
class_selector = mk.gui.Select( |
|
values=list(labels), |
|
value=labels[0], |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
filtered_df = mk.reactive(lambda df, label: df[df[LABEL_COL] == label])( |
|
df, class_selector.value |
|
) |
|
|
|
images = random_images(filtered_df) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
grid = mk.gui.html.div( |
|
[ |
|
|
|
mk.gui.html.div(mk.gui.Image(data=img)) |
|
for img in images |
|
], |
|
classes="h-fit grid grid-cols-4 gap-1", |
|
) |
|
|
|
layout = mk.gui.html.div( |
|
[ |
|
mk.gui.html.div( |
|
[mk.gui.Caption("Choose a class:"), class_selector], |
|
classes="flex justify-center items-center mb-2 gap-2", |
|
), |
|
grid, |
|
], |
|
classes="h-full flex flex-col m-2", |
|
) |
|
|
|
page = mk.gui.Page(layout, id="reactive-viewer") |
|
page.launch() |
|
|