Comments (4)
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()
from magic-class.
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.
Just as an example of a use case. This is something we did with magic gui:
It allows stitching in 3 steps.
- the user selects input files and a few other parameters and presses "Generate Preview"
- once the previews have been generated, the user can select the correct preview from a layer drop-down list
- 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.
Thanks for this example. This looks great, I will give it a try !
from magic-class.
Related Issues (20)
- Thread unsafety of macro.
- The "testing" submodule does not appear.
- `dask_thread_worker` and `confirm` is not compatible
- Personalise Path widget HOT 1
- cannot import click error HOT 4
- MagicClassConstructionError is messy
- Return annotation is not supported
- Implement undo
- `@wraps` causes empty tabs or subwindows in some widget types
- Replace `macrokit` with `collections_undo`
- Table widget parameters and methods HOT 8
- move_to_center failing in certain cases HOT 2
- `Optional` in `Annotated` doesn't work
- Partialize macro recording if `partial` is used
- Widget (not value) is passed to callback functions if `vfield` creates container widget.
- `FieldGroup.connect` is not working
- Trouble creating magic-class with two functions, each decorated with @magicgui HOT 2
- `WidgetOptions` being removed HOT 3
- would like to remove `magicgui.signature` HOT 2
- Generic typing on field objects
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 magic-class.