Skip to main content

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!

Installation

  • use pip
pip install magic-class
  • from source
git clone https://github.com/hanjinliu/magic-class

Dependencies

magicgui>=0.2.11

numpy>=1.20.3

matplotlib>=3.4.2

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: ...

directly integrate magicgui:

@magicclass
class PlotData:
    @magicgui
    def load(self, path: Path): ...

set design on each button:

from magicclass import set_design
@magicclass
class PlotData:
    @set_design(text="Clear Data")
    def clear(self): ...

... and so on.

Other examples are in the "examples" folder.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magic-class-0.4.2.tar.gz (32.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

magic_class-0.4.2-py3-none-any.whl (39.3 kB view details)

Uploaded Python 3

File details

Details for the file magic-class-0.4.2.tar.gz.

File metadata

  • Download URL: magic-class-0.4.2.tar.gz
  • Upload date:
  • Size: 32.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.0

File hashes

Hashes for magic-class-0.4.2.tar.gz
Algorithm Hash digest
SHA256 7ec4e62fd1cd2e65861dec921bf0d513f310be3951a4a75af1491d0014073f7e
MD5 521775901425e4158c8a57451c53ce05
BLAKE2b-256 80a66329e40d83248064fe802f61835b917e21f668f8d98ae5cff5379c200539

See more details on using hashes here.

File details

Details for the file magic_class-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: magic_class-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 39.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.0

File hashes

Hashes for magic_class-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4cc8688f9b27535236b2da39fa22ab6140195263b1ec2696c9ab45ccff1e1a0d
MD5 37080067a4f930b2cba559d183188788
BLAKE2b-256 acbe11a91bb4e26144727cc2ec3b998941979b57932a8538173b3d14ae356b28

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page