Comments (9)
would you want a feature that makes the first line of the docstring a label at the top?
from magicgui.
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.
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.
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](https://private-user-images.githubusercontent.com/1609449/258082887-9a408640-659b-40f4-a690-073d6429358b.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTAxMDE4NDcsIm5iZiI6MTcxMDEwMTU0NywicGF0aCI6Ii8xNjA5NDQ5LzI1ODA4Mjg4Ny05YTQwODY0MC02NTliLTQwZjQtYTY5MC0wNzNkNjQyOTM1OGIucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDMxMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDAzMTBUMjAxMjI3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MDUwMjU4N2Q3ZDdmNDE1MzUyNGU0NDk2ZWU5ODA0NDkyMDJiMTNlNWIyM2VlYzljMGVhNGQxOGEwOTUxMDFmNSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.TpAFWpsTgZ4ZzUQqErL4Ov6QQUmCt5deEMYu5G0_wWk)
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.
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.
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.
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.
nope, looks like it's only parameters at the moment
from magicgui.
If you think it suits the design of the project, then yes. Probably defaulting to disabled for backwards compatibility.
from magicgui.
Related Issues (20)
- Run button for widget does not work in jupyterlab
- Example scripts from docu do not open GUI HOT 2
- Having trouble with unresponsive magicgui dialog boxes in jupyter notebook
- RangeSlider labels cut off when used with Napari HOT 3
- Indeterminate progress bars HOT 3
- Styling Widgets HOT 7
- `Container.parent` type annotation incorrect HOT 1
- docs: pymdownx.inlinehilite (via mkdocs-gallery) breaking autorefs HOT 6
- `Literal` and `widget_type="RadioButtons"` not compatible HOT 1
- Add support for toolbars HOT 2
- Improve matplotlib example by implementing abstraction HOT 8
- MainWindow for ipywidgets HOT 5
- Errors in rendered widget image with 16-bit screen HOT 2
- File dialog for ipynb backend HOT 2
- How can we programatically intercept keystokes like `delete` in a magicgui table? HOT 3
- No parent with parent_changed HOT 1
- build_widget doesn't populate defaults when given a class object, rather than a class instance HOT 1
- Tests failing in tests
- user input validation HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from magicgui.