Skip to content
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

Create a widget from inspect.Signature #694

Open
1 task done
jacopoabramo opened this issue Mar 26, 2025 · 1 comment
Open
1 task done

Create a widget from inspect.Signature #694

jacopoabramo opened this issue Mar 26, 2025 · 1 comment

Comments

@jacopoabramo
Copy link

jacopoabramo commented Mar 26, 2025

❓ Questions and Help

I'm currently trying to create a widget dynamically with a function like this:

def live_count(detectors: Sequence[str], toggle: bool) -> None:

The idea is that I would like to provide a series of choices from detectors and be able to select one or multiple of the items reported in the sequence.

As far as I've understood it's possible to do so via the FunctionGui, but the catch is that I don't actually want to execute live_count. What I would like to do is:

  • get the inspect.Signature object of the function;
  • create the widget from it;
  • emit a signal that sends the information currently selected in the widget to another object.

Additional I would like to add an information button that I would like to connect to a custom dialog which displays the docstring of the function.

I have been reading the documentation a bit and I believe it should be possible, but I think I'm missing something. I also attempted to write a custom Container but wasn't very good with it.

@tlambert03
Copy link
Member

As far as I've understood it's possible to do so via the FunctionGui, but the catch is that I don't actually want to execute live_count

then probably don't use @magicgui/FunctionGui ... since the main point of that is to execute a function based on parameters. I would also not suggest first getting an inspect.Signature: if you're really only creating the function just to create the widget... don't bother with the function. That's a too-high-level API for what you're doing.

If you just want the widget, use the direct widget API: https://pyapp-kit.github.io/magicgui/widgets/

If you want magicgui to pick the appropriate widget for you, then use create_widget as described on that page:

from magicgui.widgets import create_widget

container = Container(
    widgets=[
        create_widget(annotation=...),
        create_widget(annotation=bool),
    ]
)
container.show()

see https://pyapp-kit.github.io/magicgui/type_map/ to understand how types match to widgets... or, just use widgets directly (and omit the types)

from magicgui.widgets import ComboBox, CheckBox, Container

container = Container(
    widgets=[
        ComboBox(choices=["Option 1", "Option 2", "Option 3"]),
        CheckBox(),
    ]
)
container.show()

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

No branches or pull requests

2 participants