Skip to main content

Manage command options in Python and create QWidgets for them.

Project description

auto-options-python

Mange command options in python and create QWidgets for them.

Introduction

A simple options dialog
Fig. 1.1 - A simple dialog created with autooptions

Autooptions allows to create options for operations in napari, in a simple way. Autooptions automatically saves and loads non transient options. It can create a dialog from the options, in which the user can change the values of the options. It updates combo-boxes containing layers of a given type when layers of that type are added or removed in napari. The user can provide callbacks that are called when the value of an option changes.

The option types currently supported are:

  • Image
  • FFT (specific to 3D-toolbox)
  • Labels
  • Points
  • Int
  • Float
  • Choice
  • String
  • Boolean

The layer types (FFT, Labels, Points and Image) and Choice will be represented via combo-boxes in the dialog. Boolean is using a checkbox and the others are using input fields.

The code to create the options-dialog in .1.1 is:

 import napari
 from autooptions.options import Options
 from autooptions.widget import OptionsWidget

 viewer = napari.Viewer()
 options = Options("3D Toolbox", "Convolution")
 options.addImage()
 options.addImage(name="kernel")
 options.addChoice("mode", choices=["same", "valid", "full"])
 options.addChoice("method", choices=["auto", "direct", "fft"])
 options.load()
 widget = OptionsWidget(viewer, options, layout_type="vertical")
 widget.addApplyButton(None)
 viewer.window.add_dock_widget(widget, name=options.optionsName)

Accessing the values of options

You can access the user input of the widgets via the options name:

    print(options.value("image"))
    print(options.value("method"))

The shortcut getImageLayer, allows to directly retrieve the layer selected in the widget, without retrieving the name first and then the layer.

kernelLayer = widget.getImageLayer("kernel")

Callbacks

You can register callbacks for options and for buttons. The callback of an option is called when the user input for the option changed. For the predifined buttons apply and ok the values are automatically copied from the widget into the option, before the user registered callback is called.

 options = Options("3D Toolbox", "Convolution")
 options.addImage(callback, callback=self.onInputImageChanged)
 self.widget = OptionsWidget(self.viewer, self.options, client=self)
 widget.addApplyButton(self.handleApplyButtonPressed)

Accessing widgets

You can access a widget, for example to check that only valid values can be entered:

    def onInputChanged(self, value):
        if not value.isnumeric():
            self.widget.widgets["size xy"][1].setText(str(self.options.value("size xy")))

Optional options

You can create optional options :) These are displayed with a checkbox to activate/deactivate them. When deactivated the option will be ignored. This allows to avoid the usage of special values in options that indicate for example that the user doesn't want to enter a value for the option in which case a default or on the fly calculated value is used.

options = Options("Filament Toolbox", "measure_labels")
options.addLabels()
options.addImage(optional=[True, False])

[True, False] indicates that the option is optional and by default deactivated. The value of a deactivated option will be None.

An optional option
Fig. 1.2 - The image option is optional

Layouts

Currently the vertical and the grid layout are supportet. If no layout is specified the grid layout is used. See Fig.1.1 for a vertical layout.

grid layout style=
Fig. 1.3 - The same option's dialog as in Fig. 1.1, but with a grid layout.
import napari
from autooptions.options import Options
from autooptions.widget import OptionsWidget

viewer = napari.Viewer()
options = Options("3D Toolbox", "Convolution")
options.addImage()
options.addImage(name="kernel")
options.addChoice("mode", choices=["same", "valid", "full"])
options.addChoice("method", choices=["auto", "direct", "fft"])
options.load()
widget = OptionsWidget(viewer, options, layout_type="grid")
widget.addApplyButton(None)
viewer.window.add_dock_widget(widget, name=options.optionsName)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

auto_options_python-1.1.4.tar.gz (31.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

auto_options_python-1.1.4-py3-none-any.whl (27.6 kB view details)

Uploaded Python 3

File details

Details for the file auto_options_python-1.1.4.tar.gz.

File metadata

  • Download URL: auto_options_python-1.1.4.tar.gz
  • Upload date:
  • Size: 31.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for auto_options_python-1.1.4.tar.gz
Algorithm Hash digest
SHA256 6032bb37874ff0576e3168aeef69ce8e7df3e8d75e78c823fb8913b80333bf4f
MD5 a30e59349cfd8980b3236876388a4098
BLAKE2b-256 50fcef3d0b4db5c2cc8c62ba9f121cd28785119efef1895db3134d38b06f3077

See more details on using hashes here.

File details

Details for the file auto_options_python-1.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for auto_options_python-1.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 81fb2ca293022d8457cf744c9d0c281e8ed0095abf4eb8f034ec9f95acf4f974
MD5 db1cacccb14361bfa2887763c6d3d871
BLAKE2b-256 f3dd77ac64a9b3a6348b837362e9c274891e1fe414a02f9130190824be46f084

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page