Data and actions¶
InputSpec¶
InputSpec
dataclass
¶
Describe one named data input exposed to the web client.
Action parameters¶
ParameterSpec
dataclass
¶
BoolParameter
¶
IntParameter
¶
FloatParameter
¶
StringParameter
¶
ChoiceParameter
¶
AnnotationParameter
¶
Bases: ParameterSpec
Action parameter the user fills in by drawing in the viewer.
The callback receives an Annotation, not an id: the browser sends the
id and it is exchanged for the object when the action runs.
WebAppDataModel¶
WebAppDataModel
¶
A DataModel that mirrors the imfusion.DataModel API and syncs with the web client.
This class provides the same interface as imfusion.DataModel: - add(data, name='') - Add data to the model - remove(data) - Remove data from the model (by reference or index) - clear() - Remove all data - get(name) - Get data by name - contains(data) - Check if data is in the model - index(data) - Get index of data - size (property) - Number of items - getitem(index) - Get data by index - len() - Get number of items - iter() - Iterate over items
When data is modified, the changes are automatically synced to the web client.
Example
webapp = ImFusionWebApp() webapp.initial_data.add(my_image, "CT Scan") webapp.initial_data.remove(0) webapp.initial_data.clear()
Initialize the WebAppDataModel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
webapp
|
ImFusionWebApp
|
The ImFusionWebApp instance this data model belongs to |
required |
session
|
Optional[Session]
|
Optional Session instance (used for per-session isolation) |
None
|
add_change_listener
¶
Subscribe to model mutations without coupling the transport to features.
add_data_invalidation_listener
¶
Subscribe to a dataset's client-side copy being destroyed.
The callback receives the data about to be invalidated, or None when the
whole model is going. Distinct from :meth:add_change_listener because
it fires before the message reaches the browser: anything the client
keys by dataset, such as annotations, has to be torn down first or the
Web SDK is left holding a freed pointer.
__getitem__
¶
Get data by index.
Supports: - Single index: data_model[0] - Slice: data_model[0:3] - List of indices: data_model[[0, 2, 4]]
add
¶
Add data to the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The data to add (e.g., SharedImageSet) |
required |
name
|
str
|
Optional name for the data |
''
|
source_path
|
Optional[str]
|
Where the data was loaded from, for apps that export references to their inputs |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The added data (same reference, unlike imfusion.DataModel which copies) |
update
¶
Sync updated data at the given index back to the client.
Call this after modifying data in-place to push the changes to the web client's visualization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Index of the data that was modified |
required |
Example
imageset = app.data_model[0]
... modify imageset ...¶
app.data_model.update(0) # Sync changes to client
update_metadata
¶
Sync a metadata-only change at index without resending pixels.
The caller asserts that the pixels are untouched; they are never inspected, because comparing them would cost as much as sending them.
Returns True when the change travelled as metadata, and False when it had to fall back to a full transfer.
Example
imageset.modality = imfusion.Data.Modality.LABEL app.data_model.update_metadata(app.data_model.index(imageset))
replace
¶
Replace data at an existing index and refresh the client in place.
remove
¶
Remove data from the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_or_index
|
Union[Any, int]
|
Either the data object to remove, or its index |
required |
get
¶
Get data by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the data to find |
required |
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
The data if found, None otherwise |
index
¶
Return index of data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The data to find |
required |
Returns:
| Type | Description |
|---|---|
int
|
The index of the data |
Raises:
| Type | Description |
|---|---|
ValueError
|
If data is not in the model |
get_name
¶
Get the name of a data item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_or_index
|
Union[Any, int]
|
Either the data object or its index |
required |
Returns:
| Type | Description |
|---|---|
str
|
The name of the data |
get_source_path
¶
Where a data item was loaded from, or None when that is unknown.
Known for data the server loaded itself, through app.open() or
seeded into initial_data. Always None for anything the browser
supplied, including sample datasets, because a browser does not expose
a real filesystem path.
set_name
¶
Set the name of a data item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_or_index
|
Union[Any, int]
|
Either the data object or its index |
required |
name
|
str
|
The new name |
required |
Annotations¶
See the annotations guide for how these fit together.
AnnotationType
¶
Bases: str, Enum
Annotation shapes the browser can place.
Values are the Web SDK's own literals and are passed through to
annotationModel.add() unchanged, so supporting a new shape is an entry
here and nothing else. The Python SDK also defines CIRCLE and
POLY_LINE, which the Web SDK does not bind yet; see
docs/guides/annotations.md.
Annotation
¶
One annotation, created through :meth:WebAnnotationModel.create_annotation.
Points are world coordinates, matching the Web SDK. Use :meth:points_in
to express them in a dataset's own frame, which is what you want whenever
the value has to outlive a change to that dataset's transform.
max_points
property
¶
Points this shape holds, as reported by the SDK.
None until the browser has created the annotation, because the
value is read from its state rather than hardcoded per type.
angle
property
¶
Angle in degrees at the vertex of an :attr:AnnotationType.ANGLE.
The middle control point is taken as the vertex.
error
property
¶
Why placement failed, or an empty string.
Set when the browser cannot create the shape — typically an annotation
type its Web SDK build does not support. Populated asynchronously, so
it is empty immediately after :meth:start_editing.
points_in
¶
Return the points in data's own coordinate frame.
Defaults to the annotation's parent dataset. World coordinates are only meaningful while the dataset's transform stays put, so anything that survives a registration or a reslice should be stored in this frame.
on_editing_finished
¶
Register a callback for when the user finishes placing the annotation.
on_points_changed
¶
Register a callback for every change to the annotation's points.
WebAnnotationModel
¶
Browser-session equivalent of imfusion.AnnotationModel.
create_annotation
¶
Create an annotation belonging to data.
Nothing reaches the browser yet: the Web SDK's add() immediately
arms interactive placement, so creation is deferred until
:meth:Annotation.start_editing or until points are assigned.
data defaults to the session's selection, because the Web SDK
requires a parent dataset even though the Python SDK does not.
on_annotation_added
¶
Register a callback for annotations the user starts in the browser.
Called with the new annotation, which has no points yet: the browser
reports it as soon as the user picks a tool. Register
:meth:Annotation.on_editing_finished on it to hear the geometry.
Unlike the annotation's own callbacks, which mirror the Python SDK's no-argument signature, this one has no SDK counterpart and always passes the annotation — there is no other way to reach it.