Code Monkey home page Code Monkey logo

Comments (9)

tlambert03 avatar tlambert03 commented on July 18, 2024 1

would you want a feature that makes the first line of the docstring a label at the top?

from magicgui.

tlambert03 avatar tlambert03 commented on July 18, 2024

hi @multimeric,

if you want to add widgets to the layout that are not parameters to your function, then you're better off using the direct widget API

You could create a widgets.Container() then add two widgets to it: a widget.Label, and your my_function widget:
https://pyapp-kit.github.io/magicgui/widgets/#containerwidget

from magicgui.

multimeric avatar multimeric commented on July 18, 2024

Thanks for the suggestion! I just worry that that solution loses some of the elegance of a pure @magicgui function because it's no longer just a function definition, and instead I have to imperatively build a widget container somewhere, so I probably have to make a factory function etc. This makes it worse for magicclass which is where I'm using it.

from magicgui.

tlambert03 avatar tlambert03 commented on July 18, 2024

i'm really not sure there's an "elegant" solution here that avoids an unnatural/magic API. the @magicgui decorator provides a way to create a widget from a group of type annotated parameters. But if you want specifically want to have a widget for something that is not in those parameters, then we need some way of adding it.

If you don't want to make container combining your FunctionGui with a Label, you can also insert the Label into your FunctionGui (which, itself is also just a container):

from magicgui import magic_factory, widgets

def add_label(widget: widgets.FunctionGui):
    widget.insert(0, widgets.Label(value="Hello World!"))

@magic_factory(widget_init=add_label)
def my_func(x: int, y: str):
    ...

widget = my_func()
widget.show(run=True)
Screen Shot 2023-08-03 at 6 23 54 AM

I'm not sure I want overload the magic_factory/magicgui signature any more than that to make it less imperative, since the number of possible ways that someone might want to arrange new widgets in their container is so variable

from magicgui.

multimeric avatar multimeric commented on July 18, 2024

Thanks, point taken that there is no easy API for this. I like your widget_init suggestion. I wonder if it would be possible to do something even neater and define a add_label decorator that I can decorate my function with, and somehow return a copy of the FunctionGui but with a new widget added on at the top? e.g.

def add_label(label: str) -> Callable[[FunctionGui], FunctionGui]:
    def _decorator(f: FunctionGui) -> FunctionGui:
        # Somehow return a copy of `f` with a new widget added,
        # whose text is `label`, without actually modifying `f` itself
    return _decorator
      
@add_label("Hello world")
@magic_factory(widget_init=add_label)
def my_func(x: int, y: str):
    ...

One other suggestion is you could you use the docstring (ie .__doc__) of the function, and use the description part as a label. Maybe this would need to be locked behind a flag for backwards compatibility. I swear I've seen this done before, but I can't think of where it is.

from magicgui.

tlambert03 avatar tlambert03 commented on July 18, 2024

docs strings are already parsed and used for tooltips (see the tooltips parameter to magicgui, which defaults to True). So if you're using numpy style docstrings, you should already be getting your __doc__ in a tooltip for each parameter.

But I do agree that it would be good to have a more immediately obvious visual indicator of that functionality (like a little info button icon you could hover on, or perhaps a flag to show them as labels)

from magicgui.

multimeric avatar multimeric commented on July 18, 2024

Cool, thanks, I didn't realise that. Is the docstring description part (ie the function's overall description, not the argument descriptions) used for anything though?

from magicgui.

tlambert03 avatar tlambert03 commented on July 18, 2024

nope, looks like it's only parameters at the moment

from magicgui.

multimeric avatar multimeric commented on July 18, 2024

If you think it suits the design of the project, then yes. Probably defaulting to disabled for backwards compatibility.

from magicgui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.