Enable magicgui decorator on class member functions
See original GitHub issueHi Talley @tlambert03 ,
thanks again for your support recently on the forum. I think I have a related feature request. To simplify my code, it would be nice if one could use magicgui on class member functions. In this way, the function could have access to a broader context, which is not handled in the magicgui.
To illustrate what I mean, take a look at this code:
import napari
from magicgui import magicgui
from napari.layers import Image
class MyObject:
def __init__(self):
self.counter = 0
@magicgui(auto_call=True)
def process_image(self, image : Image, sigma: float = 5):
self.counter = self.counter + sigma
print(self)
# load data
from skimage.io import imread
image = imread('https://samples.fiji.sc/blobs.png')
# start up napari
with napari.gui_qt():
viewer = napari.Viewer()
viewer.add_image(image, name='blobs')
# generate a Graphical User Interface from the function above magically
gui = MyObject().process_image.Gui()
viewer.window.add_dock_widget(gui)
When executing it and increasing the sigma in the magic user interface, this error is thrown:
ERROR:root:Unhandled exception:
Traceback (most recent call last):
File "C:\Users\rober\miniconda3\lib\site-packages\magicgui\core.py", line 532, in __call__
value = self.func(**_kwargs)
File "C:/structure/code/pyclesperanto_prototype/demo/napari_gui/napari_magicgui.py", line 17, in process_image
self.counter = self.counter + sigma
AttributeError: 'str' object has no attribute 'counter'
Thus, the self variable is not correctly set in such a context. Do you think this could be implemented? I’m also open to other solutions if you see some.
Thanks!
Cheers, Robert
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Can a decorator of an instance method access the class?
The method decorator marks the method as one that is of interest by adding a "use_class" attribute - functions and methods are also...
Read more >Problem accessing napari viewer with magic-class
Hi I have been using the magic-class library for generating guis in napari. I have been running into an error when using magic...
Read more >magicgui - PyPI
Enable decorator to be used on methods #56 (tlambert03) add application_name variable #55 (tlambert03)
Read more >The `persist` argument is ignored in `@magicgui(persist=True ...
I noticed that when I use @magicgui decorator on a class method, it becomes somewhat buggy and ignores some of its arguments. Noticeably,...
Read more >magicgui.widgets._bases.container_widget - napari
Wraps a widget that implements :class:`~magicgui.widgets. ... is tightly coupled to a specific function signature (as in the "classic" magicgui decorator), ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

omg! it already works on the current master!! 🎉 (one of those super happy moments when something you’ve worked on for a while just magically fixes a different problem)
Try checking out the master branch here and using this code:
I’m actually going to leave this issue open, since the decorator syntax still won’t work, but it could be made to work and I like the idea.