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.3.tar.gz (31.2 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.3-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: auto_options_python-1.1.3.tar.gz
  • Upload date:
  • Size: 31.2 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.3.tar.gz
Algorithm Hash digest
SHA256 305180331923d76c3f5b5bd8e922a7ddc658ff8f31090ff6bc9d9997e0ac8afd
MD5 c853f586a740deecb28754c58adf56d3
BLAKE2b-256 b768fa45e66c5bed07220b81ba404d333ec975560cb5a9797112071a35d4b764

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for auto_options_python-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 39ed7dd847ce100ccfcbaef7f969946c8599177fb65aabe87054fc4b62a6d480
MD5 47db89f67473e7954f1578e47aaef1f1
BLAKE2b-256 8397703f096f27d0e12f415638d3c8fb9db017d613c4804cbbc4fbd656b2b9c3

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