Generate multifunctional GUIs from classes
Project description
magic-class
In magicgui you can make simple GUIs from functions. However, we usually have to create GUIs that are composed of several buttons, and each button is connected with a class method. You may also want a menu bar on the top of the GUI, or sometimes a magicgui
widget docked in it.
Decorate your classes with @magicclass
and you can use the class both in GUI and from console. They are easy to maintain and minimize the time spent on debugging of GUI implementation.
magic-class
is work in progress. Feel free to report issues and make suggestions!
Documentation
Documentation is available here.
Installation
- use pip
pip install magic-class
- from source
git clone https://github.com/hanjinliu/magic-class
Example
Let's make a simple GUI that can load 1-D data and plot it.
from magicclass import magicclass
from pathlib import Path
@magicclass
class PlotData:
"""
Load 1D data and plot it.
"""
def __init__(self, title=None):
self.title = title
self.data = None
self.path = None
def load(self, path:Path):
"""
Load file.
"""
self.path = str(path)
self.data = np.loadtxt(path)
def plot(self):
"""
Plot data.
"""
if self.title:
plt.title(self.title)
plt.plot(self.data)
plt.show()
Classes decorated with @magicclass
are converted to magicgui
's Container
widgets. GUI starts with show
method.
widget = PlotData(title="Title")
widget.show()
You can continue analysis in console.
widget.plot()
magic-class
is also compatible with napari. You can add them to viewers as dock widgets.
import napari
viewer = napari.Viewer()
viewer.window.add_dock_widget(widget)
Another outstanding feature of magic-class
is its macro recorder functionalities. After you pushed "load" → "plot" you can make an executable Python script like below.
print(widget.create_macro())
ui = PlotData(title='Title')
ui.load(path=r'...')
ui.plot()
To make nicer GUI, you can also nest magic-class
:
@magicclass
class PlotData:
@magicclass
class Menu: ...
add menus with @magicmenu
decorator:
@magicclass
class PlotData:
@magicmenu
class File: ...
@magicmenu
class Edit: ...
add context menu with @magiccontext
decorator:
@magicclass
class PlotData:
@magiccontext
class context: ...
def Copy(self): ...
def Paste(self): ...
directly integrate magicgui
and its widgets:
@magicclass
class PlotData:
line = LineEdit()
@magicgui
def load(self, path: Path): ...
... and so on.
Other examples are in the "examples" folder.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for magic_class-0.5.11-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae9fe837519bab26ba6c7789215f765cbcc5cfdbcedf882a52b082830f252d5f |
|
MD5 | ee26bcbbd9a376bbf1bf5ecdba025d8b |
|
BLAKE2b-256 | a9e1723a3d484640b96cc366408da723fd3ef5a4c4a5b777feb3de77c447b0ae |