magicgui viewer#

Example showing how to access the current viewer from a function widget.

Tags: gui

magic viewer
/home/runner/work/docs/docs/.venv/lib/python3.12/site-packages/napari/_qt/qt_event_loop.py:50: UserWarning: System theme detection requires a Qt6 backend. Please switch to PyQt6 or PySide6 to use it.
  theme_type=get_system_theme(),
/home/runner/work/docs/docs/.venv/lib/python3.12/site-packages/napari/_qt/qt_event_loop.py:50: UserWarning: System theme detection requires a Qt6 backend. Please switch to PyQt6 or PySide6 to use it.
  theme_type=get_system_theme(),

import napari


# annotating a parameter as `napari.Viewer` will automatically provide
# the viewer that the function is embedded in, when the function is added to
# the viewer with add_function_widget.
def my_function(viewer: napari.Viewer):
    print(viewer, f'with {len(viewer.layers)} layers')


viewer = napari.Viewer()
# Add our magic function to napari
viewer.window.add_function_widget(my_function)

if __name__ == '__main__':
    napari.run()

Gallery generated by Sphinx-Gallery