Skip to content

Add initial documentation about widget communication #721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Czaki
Copy link
Contributor

@Czaki Czaki commented May 25, 2025

References and relevant issues

Depends on napari/napari#7965

Description

This PR adds documentation about possible way to communicate or share state between widgets.

@Czaki Czaki added this to the 0.6.2 milestone May 25, 2025
@Czaki Czaki added the documentation Improvements or additions to documentation label May 25, 2025
## Widget name

When widget is added to the viewer as plugin contribution (by using a menu or `add_plugin_dock_widget`), it is assigned a name.
The name is created by concatenating the widget name and the plugin name in brackets, like this: `"widget_name (plugin_name)"`.
Copy link
Member

@psobolewskiPhD psobolewskiPhD May 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The name is created by concatenating the widget name and the plugin name in brackets, like this: `"widget_name (plugin_name)"`.
The name is created by concatenating the widget `display_name` from the plugin manifest and the plugin name in parenthesis, like this: `"Widget name (plugin_name)"`. Note: this is the same name that is shown in the napari menus and the title bar of the widget.

I was confused by the underscore, thinking it could be the Python name.

Comment on lines +35 to +38
### Warnings

Many plugins use the `viewer.window._dock_widgets` attribute to access `QtViewerDockWidget` widgets.
It is an internal API and may stop working on any release
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Warnings
Many plugins use the `viewer.window._dock_widgets` attribute to access `QtViewerDockWidget` widgets.
It is an internal API and may stop working on any release
```{important}
We don't recommend using the `viewer.window._dock_widgets` attribute to access `QtViewerDockWidget` widgets. This is a private, internal API and may stop working on any release. Please use the above described public API instead.
```

How about making this an admonition?

Copy link
Member

@psobolewskiPhD psobolewskiPhD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really like this! Thank you for writing this up!
I made some small-ish suggestions.

Copy link
Contributor

@willingc willingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Czaki for adding this helpful documentation. I made a few suggestions for clarity.

@@ -0,0 +1,146 @@
(widget-communication)=

# Accessing docked widgets
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to level ## and make title # Widget communication


# Accessing docked widgets

Sometimes more complex workflows require access to other docked widgets, sometimes from other plugins, created by other developers.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Sometimes more complex workflows require access to other docked widgets, sometimes from other plugins, created by other developers.
Sometimes complex workflows require access to other docked widgets, information from other plugins, which may be created by other developers.

Comment on lines +7 to +13
## Accessing plugin widgets `viewer.window.add_plugin_dock_widget`

If a plugin widget is already docked in the viewer,
calling the `add_plugin_dock_widget` method will return the existing widget instance.

This is the most convenient way to access a plugin widget that is required by your plugin.
Because if the widget is absent, it will be created and added to the viewer.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Accessing plugin widgets `viewer.window.add_plugin_dock_widget`
If a plugin widget is already docked in the viewer,
calling the `add_plugin_dock_widget` method will return the existing widget instance.
This is the most convenient way to access a plugin widget that is required by your plugin.
Because if the widget is absent, it will be created and added to the viewer.
## Access another plugin widget with `viewer.window.add_plugin_dock_widget`
If a desired plugin widget is already docked in the viewer,
calling the `add_plugin_dock_widget` method will return the existing widget instance.
If the desired widget is absent, it will be created and added to the viewer.
`add_plugin_dock_widget` is the most convenient way to access a plugin widget that is required by your plugin.

Comment on lines +15 to +27
## Accessing widget by name `viewer.window.get_dock_widget`

If you need to access a widget, but without creating it, you can use the `get_dock_widget` method.
The `get_dock_widget` method allows you to retrieve a docked widget by its name.

This method returns the `QtViewerDockWidget` associated with the widget, or `None` if the widget is not found.

To access the original widget, you can use the `inner_widget` method of the `QtViewerDockWidget`.

This method allows accessing non-plugin widgets added to viewer using the `add_dock_widget` method.


*The `get_dock_widget` and `inner_widget` were added in napari 0.6.2.*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Accessing widget by name `viewer.window.get_dock_widget`
If you need to access a widget, but without creating it, you can use the `get_dock_widget` method.
The `get_dock_widget` method allows you to retrieve a docked widget by its name.
This method returns the `QtViewerDockWidget` associated with the widget, or `None` if the widget is not found.
To access the original widget, you can use the `inner_widget` method of the `QtViewerDockWidget`.
This method allows accessing non-plugin widgets added to viewer using the `add_dock_widget` method.
*The `get_dock_widget` and `inner_widget` were added in napari 0.6.2.*
## Access a widget by name with `viewer.window.get_dock_widget`
If access a target widget, but without creating it, use the `get_dock_widget` method.
The `get_dock_widget` method returns a docked widget by its name.
This method returns the `QtViewerDockWidget` associated with the widget, or `None` if the widget is not found or does not exist.
To access the target widget, you can use the `inner_widget` method of the `QtViewerDockWidget`.
This method allows accessing non-plugin widgets added to viewer using the `add_dock_widget` method.
*The `get_dock_widget` and `inner_widget` were added in napari 0.6.2.*


### Warnings

Many plugins use the `viewer.window._dock_widgets` attribute to access `QtViewerDockWidget` widgets.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Many plugins use the `viewer.window._dock_widgets` attribute to access `QtViewerDockWidget` widgets.
Many plugins use the private `viewer.window._dock_widgets` attribute to access `QtViewerDockWidget` widgets.


## Shared state between widgets

If you need for multiple widgets to share the state, you can use a shared global object.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you need for multiple widgets to share the state, you can use a shared global object.
If you need for multiple widgets to share state, you can use a shared global object.

return GLOBAL_STATE[viewer]
```

To store state between sessions, you can enhance the `get_global_state` to load state from drive or database.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To store state between sessions, you can enhance the `get_global_state` to load state from drive or database.
To store state between sessions, you can enhance the `get_global_state` to load state from persistent storage, such as a drive or database.



def save_global_state(event):
"""Save the global state to a file.""" # Serialize the state to a file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Save the global state to a file.""" # Serialize the state to a file
"""Save the global state by serializing it to a file."""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants