Skip to content
Feeding a clanker? Grab this page as raw .md

ImageRefreshWidget API#

ImageRefreshWidget renders one <img> element and repoints it at a new src whenever that traitlet changes, so a running loop can redraw a single image slot rather than emit a fresh picture per iteration. src is normally a base64 data URI; the wigglystuff.utils.refresh_matplotlib decorator turns a plotting function into exactly that, which is the usual way this widget gets fed.

See also: HTMLRefreshWidget for the same in-place trick with arbitrary markup, ProgressBar for reporting how far the loop has got, and FramePlayer for replaying frames once the loop has finished.

Bases: AnyWidget

A widget that displays an image and refreshes when the source changes.

This widget creates an image element that automatically updates whenever the src attribute is modified, making it ideal for displaying dynamically generated images in Jupyter notebooks.

Attributes:

Name Type Description
src str

The image source, typically a base64-encoded data URI.

Example:

import marimo as mo
from wigglystuff import ImageRefreshWidget
from wigglystuff.utils import refresh_matplotlib
import matplotlib.pylab as plt
import numpy as np

@refresh_matplotlib
def plot_data(data):
    plt.plot(np.arange(len(data)), np.cumsum(data))

widget = mo.ui.anywidget(ImageRefreshWidget(src=plot_data([1, 2, 3, 4])))

# Update the widget with new data
widget.src = plot_data([1, 2, 3, 4, 5, 6])

Synced traitlets#

Traitlet Type Notes
src str The image source, typically a base64-encoded data URI.