-
Notifications
You must be signed in to change notification settings - Fork 41
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
base: main
Are you sure you want to change the base?
Conversation
## 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)"`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
### 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### 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?
There was a problem hiding this 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.
There was a problem hiding this 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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## 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. |
## 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.* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Save the global state to a file.""" # Serialize the state to a file | |
"""Save the global state by serializing it to a file.""" |
References and relevant issues
Depends on napari/napari#7965
Description
This PR adds documentation about possible way to communicate or share state between widgets.