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 magicgui widgets 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 has built-in real-time macro recorder and website-like help widget creator. You can quickly make a reproducible and well-documented GUI without disturbing readability of Pythonic codes.

Magic-class is work in progress. Feel free to report issues, make suggestions and contribute!

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 load(self, path: Path):
        """
        Load file.

        Parameters
        ----------
        path : Path
            File path
        """
        self.data = np.loadtxt(str(path))

    def plot(self):
        """
        Plot data.
        """
        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)

After you pushed "load" → "plot" you can make an executable Python script like below.

print(widget.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


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.5.21.tar.gz (92.1 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.5.21-py3-none-any.whl (109.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: magic-class-0.5.21.tar.gz
  • Upload date:
  • Size: 92.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for magic-class-0.5.21.tar.gz
Algorithm Hash digest
SHA256 7ce5ecfe2d587f441496faf076c032fcd6843b42dc21480cf7a0ad4e2045181f
MD5 2c67cd167ef82f629e97d4c680e6998a
BLAKE2b-256 0d5e53de02f26d0f9cc219ce506614f6ce3981e5c15b05d86ac025e0c28b53d9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: magic_class-0.5.21-py3-none-any.whl
  • Upload date:
  • Size: 109.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for magic_class-0.5.21-py3-none-any.whl
Algorithm Hash digest
SHA256 48d673f62922fcd7a5c611cc98b88dcda14d8591d3518025c27375a0cebb6a1d
MD5 a785f2c94337f5668d7dfcd3767f4af8
BLAKE2b-256 e28e782284742eefa601c7bc4daf4d0d95f0f4b2608ba2cf452df39b27ee9a70

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