-
Notifications
You must be signed in to change notification settings - Fork 49
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
Enable magicgui decorator on class member functions #53
Comments
Yeah, that's a (good) legitimate feature request. possible, but will take a little doing. The reason you're getting that error is that when the (If this seems weird, recall that class MyObject:
def process(self):
print(self)
m = MyObject()
# this "usual" usage ...
m.process()
# is basically just shorthand for this
MyObject.process(m) so what you really need to decorate is the bound method... something like this: class MyObject:
def __init__(self):
self.counter = 0.0
def process_image(self, image: Image, sigma: float = 5):
self.counter = self.counter + sigma
print(self)
...
m = MyObject()
gui = magicgui(m.process_image, auto_call=True).Gui()
viewer.window.add_dock_widget(gui) however, that will fail with Anyway, I think it's a great feature, and I have some ideas on how to detect whether we're decorating a (pre-bound) class method... but it will take a little doing. side note: some reasonably big changes are coming in the next release with the merge of #43 ... so the |
omg! it already works on the current master!! 🎉 Try checking out the master branch here and using this code: import napari
from napari.layers import Image
from skimage.io import imread
from magicgui import magicgui
class MyObject:
def __init__(self):
self.counter = 0.0
def process_image(self, image: Image, sigma: float = 5):
self.counter += sigma
print(self.counter, sigma)
image = imread("https://samples.fiji.sc/blobs.png")
# start up napari
with napari.gui_qt():
viewer = napari.Viewer()
viewer.add_image(image, name="blobs")
m = MyObject()
gui = magicgui(m.process_image, auto_call=True)
viewer.window.add_dock_widget(gui) |
side note: for simple urls like that, you can use |
That sounds great! :-)
Hm. I just cloned magicgui master and installed it and then tried the code you posted. It shows napari, but doesn't show the magicgui. It throws this error instead:
Shall I update any other dependency? |
ah! yes you'll probably need the most recent napari master too (with napari/napari#1994) |
Yes, Now, it works! Thanks for implementing this feature request in negative time 🥳 |
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. |
Hi 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:
When executing it and increasing the sigma in the magic user interface, this error is thrown:
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
The text was updated successfully, but these errors were encountered: