Coder Social home page Coder Social logo

Comments (4)

hanjinliu avatar hanjinliu commented on June 18, 2024 1

Thanks for sharing! I understood your goals.
The point is not how to make class attribute accessible. What you want may be how to keep some functions interfaces (path reader, choose layer etc. in your widget) always displayed on the main widget, while convert other functions into buttons. This feature is definitely useful in many cases! I'll consider how to make it in the later versions.

Here I can show you a temporal solution that works with v0.1.0.

Since classes created by @magicclass inherits Container widget, you can append/insert widgets. Magic-class supports __post_init__ method that will be called after __init__ (just like dataclass), so the simplest way is to define the loader and parameter setter with @magicgui and add it to magic-class (of course you can add magicgui by yourself afterword instead of defining __post_init__).

from magicgui import magicgui
from magicclass import magicclass
from pathlib import Path

@magicclass
class C:
    def __post_init__(self):
        @magicgui(auto_call=True) # auto_call is True because we don't want to push call button every time!
        def loader(path:Path, param:int):
            self.path = path
            self.param = param
        self.insert(0, loader)

    def show_path(self):
        print(self.path)

    def show_param(self):
        print(self.param)

c = C()
c.show()

図1

from magic-class.

hanjinliu avatar hanjinliu commented on June 18, 2024

Thanks for your feedback, @VolkerH !
Yes, actually I've also thought about setting class attributes via other type of widgets.
However, I'm pending the feature for a few reasons:

  • You can define a kind of "setter" method to do this, like:
@magicclass
class C:
    data = 0
    def set_class_attr(self, data:int):
        self.__class__.data = data
  • I think the main reason why you want to set class attributes is to share same variants when you open multiple GUIs (otherwise you should prefer non-class attributes). In that case, you'll not often change their values so they should be set as shown in the example above.

Are there any other use cases you suppose? I know I should add some actions for class attributes, so I would be great if you have some suggestions.

from magic-class.

VolkerH avatar VolkerH commented on June 18, 2024

Just as an example of a use case. This is something we did with magic gui:
image

It allows stitching in 3 steps.

  1. the user selects input files and a few other parameters and presses "Generate Preview"
  2. once the previews have been generated, the user can select the correct preview from a layer drop-down list
  3. the user can then trigger fusion at full resolution or generation of a Big Stitcher project.

We had to do some very hacky things to generate 3 buttons and to keep the state (which sourece tiles were loaded in the first step etc.). It is not very clean code.

magic-class would offer a clean way to keep the state (i.e. file names of tiles, chosen symmetry transformation) and trigger different actions with different buttons. I just don't see how I can create the widgets for input such as filenames and integer with magic class.
This is just to give you an idea for a possible use case, I don't know how feasible it would be.

from magic-class.

VolkerH avatar VolkerH commented on June 18, 2024

Thanks for this example. This looks great, I will give it a try !

from magic-class.

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.