Experimenting with Toga

I heard about Toga from twitter. Apparently it was introduced at PyConAU. I took an hour to whip up a small app that will let you touch buttons to launch fabric tasks.

None of them can take arguments, so it relies on defaults and a fully-stocked set of 'env' variables, but that's ok, this is just a silly hack to explore a new gui framework.

I'll say it's not exceptional in anything other than how easy it was to make the simple app work as advertised.

What I mean is, it seems to have a long way to go in the documentation front before I'd use it for anything serious, but I'm excited for the possibilities and it was impressive how easy it was writing a quick app as compared to my experiences with pyobjectivec and pygtk.

If you have an existing fabfile, you can pip install toga and run this little app.

from __future__ import print_function, unicode_literals, absolute_import
import os
import toga
from fabric import state
from fabric.main import load_fabfile
from fabric.tasks import execute

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
FABFILE = os.path.join(BASE_DIR, 'fabfile.py')

docstring, callables, default = load_fabfile(FABFILE)
state.commands.update(callables)


def button_handler(widget, *args, **kwargs):
    execute(
        widget.label,
        hosts=[],
        roles=[],
        exclude_hosts=[],
        *args, **kwargs
    )

if __name__ == '__main__':

    app = toga.App('Fabric App', 'com.issackelly.fabricapp')
    container = toga.Container()

    btn_height = 20
    this_height = container.TOP

    for name in callables:
        button = toga.Button(name, on_press=button_handler)
        container.add(button)
        container.constrain(button.TOP == this_height)
        container.constrain(button.HEIGHT == btn_height)
        container.constrain(button.WIDTH == 150)
        this_height += btn_height

    app.main_window.size = (150, btn_height * (len(callables) + 1))
    app.main_window.content = container
    app.main_loop()

Comments and Messages

I won't ever give out your email address. I don't publish comments but if you'd like to write to me then you could use this form.

Issac Kelly