Display Widgets¶
Image¶
Image display widget with optional interactive pointer/keyboard events.
Constructor: new Widgets.Image({url, interactive, use_animation_frame})
Options:
url– initial image URLinteractive– enable pointer and keyboard eventsuse_animation_frame– throttle updates to animation frame rate
Methods:
Method |
Description |
|---|---|
|
Set image from URL or data URI. |
|
Set image from a raw |
|
Return a 2D canvas drawing context for overlay drawing. |
|
Flush pending draw operations. |
Callbacks:
pointer-down,pointer-up,pointer-move– mouse/touchenter,leave– pointer enters/leavesclick,dblclick– click eventsscroll– scroll wheelkey-down,key-up,key-press– keyboardfocus-in,focus-out– focus changesdrop-start,drop-end,drop-progress,drag-over– file drag-and-dropcontextmenu– right-click
let img = new Widgets.Image({interactive: true});
img.set_image("photo.jpg");
img.add_callback('click', (w, ev) => {
console.log("Clicked at:", ev.data_x, ev.data_y);
});
Canvas¶
HTML5 canvas for custom drawing with interactive events.
Constructor: new Widgets.Canvas({use_animation_frame, interactive})
Options:
use_animation_frame– throttle updates to animation frame rateinteractive– enable pointer and keyboard events
Methods:
Method |
Description |
|---|---|
|
Draw an image onto the canvas. |
|
Return the 2D canvas drawing context. |
|
Flush pending draw operations. |
Callbacks:
pointer-down,pointer-up,pointer-move– mouse/touchenter,leave– pointer enters/leavesclick,dblclick– click eventsscroll– scroll wheelkey-down,key-up,key-press– keyboardfocus-in,focus-out– focus changesdrop-start,drop-end,drag-over– file drag-and-dropcontextmenu– right-clickactivated– general activation
let canvas = new Widgets.Canvas({interactive: true});
canvas.resize(640, 480);
let ctx = canvas.get_draw_context();
ctx.fillStyle = "red";
ctx.fillRect(10, 10, 100, 50);
canvas.update();
ColorWidget¶
Inline color swatch with optional picker.
Constructor: new Widgets.ColorWidget({color})
Options:
color– initial color as hex string (e.g."#ff0000")
Methods:
Method |
Description |
|---|---|
|
Return current color as hex string. |
|
Set color. |
Callbacks:
pick– fired when a color is selected.
let cw = new Widgets.ColorWidget({color: "#3366cc"});
cw.add_callback('pick', (w) => {
console.log("Color:", w.get_color());
});
VideoWidget¶
Video display widget with playback controls. Supports video files via URL
and live streams via WebRTC or getUserMedia.
Constructor: new Widgets.VideoWidget({url, autoplay, controls, muted, loop})
Options:
url– initial video URLautoplay– start playback automaticallycontrols– show native browser playback controlsmuted– start mutedloop– loop playback
Methods:
Method |
Description |
|---|---|
|
Set video source from a URL (mp4, webm, etc.). |
|
Set a MediaStream as the source (WebRTC, getUserMedia). |
|
Return the underlying |
|
Playback controls. Stop resets to the beginning. |
|
Mute state. |
|
Volume (0.0 to 1.0). |
|
Loop state. |
|
Show/hide native browser controls. |
|
Seek position. |
|
Total duration in seconds. |
|
Whether playback is paused. |
|
Request fullscreen display. |
Callbacks:
play– playback started.pause– playback paused.ended– playback reached the end.error– a playback error occurred.timeupdate– playback position changed (receives current time, duration).volumechange– volume or mute state changed.
// Video file
let video = new Widgets.VideoWidget({url: "demo.mp4", controls: true});
vbox.add_widget(video, 1);
// WebRTC stream
let cam = new Widgets.VideoWidget({autoplay: true, muted: true});
navigator.mediaDevices.getUserMedia({video: true})
.then(stream => cam.set_stream(stream));
ExternalWidget¶
A container for embedding third-party library content (Plotly charts, Bokeh plots, Leaflet maps, etc.) into pgwidgets layouts. See Using External Widgets for full examples.
Constructor: new Widgets.ExternalWidget()
Methods:
Method |
Description |
|---|---|
|
Return the inner DOM element for third-party libraries to render into. |
|
Set inner HTML content directly. |
|
Remove all content. |
Callbacks: None (inherits resize from Widget).
let chart = new Widgets.ExternalWidget();
vbox.add_widget(chart, 1);
Plotly.newPlot(chart.get_content_element(), data, layout,
{responsive: true});
// If the library needs explicit resize notification:
chart.add_callback('resize', () => {
Plotly.Plots.resize(chart.get_content_element());
});
TreeView¶
Hierarchical tree/table with columns, sorting, icons, and multi-selection. Tree data is stored as a hierarchy of dicts keyed by stable string identifiers; paths are arrays of those keys, so a path stays valid no matter how the visible tree is sorted.
Constructor: new Widgets.TreeView({columns, show_header,
selection_mode, alternate_row_colors, show_grid, show_row_numbers,
sortable, allow_text_selection})
selection_mode is one of "single" / "multiple" (rows)
or "single-cell" / "multiple-cell" (cells). See
“Cell-level selection” below.
Column descriptors: each column is a string (label only) or an object with the following keys:
label– header text.key– stable string identifier for the column. All per-column methods take a key. Auto-generated if omitted (_col0,_col1, …).type– one of"string"(alias"str"),"integer"(alias"int"),"float"(alias"number"),"boolean"(renders ✓ when truthy), or"icon"(cell value is a URL ordata:URL used as the image source).halign–"left"/"center"/"right". Default depends on type: numeric → right, boolean / icon → center, otherwise left.editable– whether cells in the column can be edited via double-click.icon_size– pixel size for icon columns (default 16).colwidth– initial column width. Numbers are pixels; strings pass through as CSS grid track values (e.g."10em","1fr"). Default is"1fr".widget– one of"checkbox"/"combobox"/"progress"/"button". Replaces the column’s plain-text cells with a real DOM input. See “Widget cells” below.choices–Arrayof option strings (combobox only).min/max– numeric bounds (progress only).text– default button label when the row value isnull(button only).enabled_key– name of a row-dict field whose truthiness gates the embedded widget’s enabled state per row.visible_key– name of a row-dict field whose truthiness gates whether the widget appears on a given row. When falsy, the cell stays empty.
Tree shape (set_tree):
The tree is a JS object keyed by stable string identifiers. Each sub-object is detected as either:
a leaf – all values primitive; the dict IS the row’s column-key → value mapping;
an interior – any value is a nested object; the entries become children, keyed by their dict key. Primitive entries in the same dict are taken as the interior’s own column values;
an interior with explicit values – when ambiguity could arise, an
__values__sentinel can hold the interior’s column values explicitly;an empty interior –
{}(a folder with no contents).
The first column auto-displays the node’s dict key when the row
supplies no value for it, so most interiors need no
__values__ at all.
Methods:
Method |
Description |
|---|---|
|
Set column definitions. |
|
Replace the tree with a dict-shaped hierarchy. Selection is cleared. |
|
Merge a dict-tree under the given parent path. Existing same-key children are replaced subtree-deep. Selections whose paths still resolve survive; vanished paths are dropped. |
|
Replace the tree but preserve selections by path. |
|
Flat-table data. Each row is a dict (preferred) or an
array; arrays are mapped to column keys in order. Synthetic
row keys ( |
|
Add a single child under a parent path. |
|
Remove by path (array of keys) or in batch. |
|
Remove all rows (preserves columns). |
|
Expand or collapse the entire tree. |
|
Expand or collapse a single node by key path. |
|
Get or set selection. |
|
Drop all selection. Fires the |
|
Select or deselect by key path(s). |
|
Select or deselect all nodes. |
|
Return a dict-tree containing a subset. |
|
Sort by column key. The underlying tree retains insertion order; only the displayed view is sorted. |
|
Scroll to a row. |
|
Update a single cell. |
|
Top-level row insert with optional explicit key and optional sibling-key for placement. |
|
Row manipulation. |
|
Column manipulation. |
|
Set column width. |
|
Auto-size all columns. |
|
Set the vertical (row) / horizontal (column) cell padding in
pixels. |
|
Toggle grid lines, row numbers, and click-to-sort. |
|
Make a column editable. |
|
Per-cell / row / column / table style overrides. Each channel cascades cell → row → column → table. See “Colour overrides” below. |
|
Drop overrides at the matching layer. |
|
Cell-mode selection API. |
|
Programmatic equivalents of Ctrl/Cmd+C/X/V. |
Auto-spanning: within a row, a column whose key is missing is
“absent” and the preceding present cell extends across it via CSS
grid spans. Explicit empty strings render as their own (empty)
cell. This lets parent rows be terse: {NAME: "Documents"}
(with the rest of the columns omitted) renders as a single cell
across the row.
Callbacks:
activated– handler signature(widget, values, path, col_key); fires on row double-click or Enter.col_keyis the column the click landed on (nullfor keyboard-triggered activation).selected– selection changed; handler receives an array of{path, values}objects.expanded/collapsed– handler(widget, values, path).cell_edited–(widget, path, col_key, oldValue, newValue).cell_selected– cell-mode selection changed; handler receives(widget, cells)where each cell is{path, col_key, value}.cell_action–(widget, row_values, col_key); fires when the user clicks awidget: "button"cell.copy/cut/paste–(widget, tsv); fire from Ctrl/Cmd+C/X/V and from the programmatic helpers.sorted–(widget, col_key, ascending).scrolled–(widget, h_pct, v_pct).
Widget cells. A column with widget set hosts a real
DOM input per cell:
"checkbox"– bound to a boolean cell value; toggling firescell_edited."combobox"–<select>populated fromchoices; changing firescell_edited."progress"– read-only<progress>driven bymin/maxand the numeric cell value."button"–<button>whose label is the cell value (or the column’stextfallback when null); clicking firescell_action.
enabled_key and visible_key name row-dict fields that
gate the per-row widget. An empty row value of null /
false on a button column suppresses that row’s button if
visible_key is also unset.
Colour overrides. Each of the four set_*_color methods
takes fg (text), bg (background), and bold. All
three are optional; passing all-null clears that layer’s
override. Resolved style cascades per-channel cell → row →
column → table, so a row-level fg can compose with a
column-level bg.
Cell-level selection. Setting selection_mode to
"single-cell" or "multiple-cell" switches the
behaviour from row selection to cell selection. Selection
state is reported via the new cell_selected callback;
select_cell / select_cells / clear_cell_selection
mutate it programmatically. The widget-level
copy_selection / cut_selection honour cell-mode and
emit a TSV payload.
let tree = new Widgets.TreeView({
columns: [
{label: "Name", key: "NAME", type: "string"},
{label: "Type", key: "TYPE", type: "string"},
{label: "Size", key: "SIZE", type: "integer"},
],
sortable: true,
});
tree.set_tree({
"Documents": {
"report.pdf": {TYPE: "PDF", SIZE: 2400},
"notes.txt": {TYPE: "Text", SIZE: 12},
},
"Pictures": {
"__values__": {TYPE: "Folder"},
"photo.jpg": {TYPE: "JPEG", SIZE: 3200},
},
});
tree.add_callback('activated',
(w, values, path) => console.log("opened", path, values));
TableView¶
Flat table (no hierarchy). Subclass of TreeView with table-style defaults (header on, grid lines on). Shares the full TreeView API including key-based per-column / per-row methods, sortable toggling, and the same column descriptors.
Constructor: new Widgets.TableView({columns, show_header,
selection_mode, alternate_row_colors, show_grid, show_row_numbers,
sortable, allow_text_selection})
set_data (or its alias set_rows) accepts either a list of
dicts (preferred) or a list of positional arrays:
let table = new Widgets.TableView({
columns: [
{label: "Name", key: "NAME", type: "string"},
{label: "Department", key: "DEPT", type: "string"},
{label: "Salary", key: "SALARY", type: "integer"},
],
sortable: true,
});
table.set_data([
{NAME: "Alice", DEPT: "Engineering", SALARY: 95000},
{NAME: "Bob", DEPT: "Marketing", SALARY: 72000},
]);
table.append_row({NAME: "Carol", DEPT: "Sales", SALARY: 71000});
table.sort_by_column("SALARY", false); // descending
Callbacks: identical to TreeView, including the new
activated 4-arg signature with col_key, the
cell_selected / cell_action / copy / cut /
paste callbacks, and the per-cell cell_edited
signature.
TableView set_columns accepts the same column descriptor
fields as TreeView, including widget / choices /
min / max / text / enabled_key /
visible_key / colwidth. Colour overrides
(set_*_color / clear_*_color) work identically.
For per-row Mute checkbox + Reset button – the canonical “widget cells” use case – declare two columns:
table.set_columns([
{label: "Time", key: "TIME", type: "string"},
{label: "ID", key: "ID", type: "string"},
{label: "Mute", key: "MUTE", type: "bool",
widget: "checkbox", enabled_key: "CAN_MUTE"},
{label: "", key: "RESET", type: "string",
widget: "button", text: "Reset",
visible_key: "CAN_RESET"},
]);