• Stars
    star
    131
  • Rank 266,861 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created over 3 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A library for building modern declarative desktop applications in WX.

A Python library for building modern declarative desktop applications


PyPI

Overview

re-wx is a library for building modern declarative desktop applications. It's built as a management layer on top of WXPython, which means you get all the goodness of a mature, native, cross-platform UI kit, wrapped up in a modern, React inspired API.

What is it?

It's a "virtualdom" for WX. You tell re-wx what you want to happen, and it'll do all the heavy lifting required to get WX to comply. It lets you focus on your state and business logic while leaving implentation details of WX's ancient API to re-wx.

Say goodbye to

  • Deep coupling of business logic to stateful widgets
  • Awkward auto-generated Python wrappers on old bloated C++ classes
  • Being forced to express UIs through low level A.GetLayout().addChild(B) style plumbing code

re-wx is:

  • Declarative
  • Component Based
  • 100% compatible with all WXPython code bases

Re-wx lets you build expressive, maintainable applications out of simple, testable, functions and components.

Alpha Note:

This is an early release and under active development. Expect a few bugs, feature gaps, and a bit of API instability. If you hit any snags, pop over to the issues and let me know!

Installation

The latest stable version is available on PyPi.

pip install re-wx 

Documentation

Quick Start: RE-WX in 5 minutes

re-wx has just a few core ideas: Elements, Components, and rendering. Everything else is achieved by combining these 3 ideas into larger and larger things.

All re-wx application consists of just a few steps.

  1. define your application view
  2. Rendering it to produce a wx object
  3. kick off the wx Main Loop.

Starting small: Hello World

import wx
from rewx import create_element, wsx, render     
from rewx.components import StaticText, Frame

if __name__ == '__main__':
    app = wx.App()    
    element = create_element(Frame, {'title': 'My Cool Application', 'show': True}, children=[
        create_element(StaticText, {'label': 'Howdy, cool person!'})
    ])
    frame = render(element, None)
    app.MainLoop()

Run this and you'll see the output on the right. While not glamorous yet, it lets us explore several of the main ideas.

At the heart of all re-wx applications is the humble Element. We used the function create_element to build them. Applications are built by composing trees of these elements together into larger and larger composite structures.

Here we've created two elements. A top-level Frame type, which is required by WXPython, and then an inner StaticText one, which displays text on the screen.

Elements all consist of three pieces of data: 1. the type of the entity we want to render into the UI, 3. the properties ("props" from here on out) we want that entity to have, and 3. any children, which are themselves Elements.

An important note is that Elements are plain data -- literally just a Python dict like this:

{
  'type': Frame, 
  'props': {
      'title': 'My Cool Application', 
      'show': True,
      'children': [{
        'type': StaticText,
        'props': {'label': 'Howdy, cool person!'}
      }]
  }

Together, these elements make up the "virtualdom" used by re-wx uses to drive the underlying WXWidgets components. Creating an element does not actually instantiate any WX elements. That job falls to render

rewx.render is how we transform our tree of Elements into a live UI. It handles all of the lifting required to instantiate the WX Objects, associate them all together, and put them in the state specified by your tree. The output of render is a WX Object, which in our example, is our top level frame.

With the frame now happily created, we just have to tell WXPython to start its main loop, which will launch the GUI, and we've officially built our first re-wx app!

A brief detour for WSX:

Writing all those create_element statements can get really tedious and creates a lot of visual noise which can make getting a feel for your UI's structure at a glance difficult. An alternative and recommended approach is to use wsx, which lets you use nested lists to express parent child relationships between components. It uses the exact same [type, props, *children] arguments as create_element, but with a terser more compact syntax. Here's the same example using wsx.

from rewx import wsx 
...
element = wsx(
  [Frame, {'title': 'My Cool Application', 'show': True}, 
    [StaticText, {'label': 'Howdy, cool person!'}]]
)

For the rest of this guide, we'll be using the wsx form, but you can use create_element if you prefer.


A Stateful component

Components are how you store and manage state in re-wx.

class Clock(Component):
    def __init__(self, props):
        super().__init__(props)
        self.timer = None
        self.state = {
            'time': datetime.datetime.now()
        }

    def component_did_mount(self):
        self.timer = wx.Timer()
        self.timer.Notify = self.update_clock
        self.timer.Start(milliseconds=1000)

    def update_clock(self):
        self.set_state({'time': datetime.datetime.now()})

    def render(self):
        return wsx(
          [c.Block, {},
           [c.StaticText, {'label': self.state['time'].strftime('%I:%M:%S'),
                           'name': 'ClockFace',
                           'foreground_color': '#51acebff',
                           'font': big_ol_font(),
                           'proporton': 1,
                           'flag': wx.CENTER | wx.ALL,
                           'border': 60}]]
        )
        
if __name__ == '__main__':
    app = wx.App()
    frame = wx.Frame(None, title='Clock')
    clock = render(create_element(Clock, {}), frame)
    frame.Show()
    app.MainLoop()        

Here we've setup a Component which keeps track of the current time and displays it nice and bold in the center of our frame.

There's lot going on here, so we'll take if from the top!

You define your own components by inheriting from rewx.Component. This gives you access to all the lifecycle and state management options provided by the base class. You can checkout the Main Concepts for the full details of the life cycle methods.

Components have a few notable methods:

Method Usage
__init__ This gets called when re-wx instantiates your class. This is where you specify your initial state. Note that this is called before the actual GUI elements are available. This method should be used only to initialize data, not deal with presentational concerns
render This is where you'll create your element tree which defines your UI.
component_did_mount This method is called once all of your Component's elements have been rendered and mounted onto a wx.Window. It's here that you can kick off any work which requires the GUI to be up and running
set_state This method is used update your components state and kick off a re-render of its visuals.

Still just an element

You use your component like any other Element we've encountered so far. Meaning, you don't instantiate it directly, you put in in your Element tree and let re-wx handle all the details.

That's what we're doing down at the bottom of the file where we wire the app together. We create an Element from our Component just like normal: create_element(Clock, {}) and pass it to our render function.

if __name__ == '__main__':
    app = wx.App()
    frame = wx.Frame(None, title='Clock')
    clock = render(create_element(Clock, {}), frame)
    frame.Show()
    app.MainLoop()  

An Application

Our final example will pull it all together. It combines plain Elements, Components, and business logic into a complete application.

def TodoList(props):
    return create_element(c.Block, {}, children=[
        create_element(c.StaticText, {'label': f" * {item}"})
        for item in props['items']
    ])


class TodoApp(Component):
    def __init__(self, props):
        super().__init__(props)
        self.state = {'items': ['Groceries', 'Laundry'], 'text': ''}

    def handle_change(self, event):
        self.set_state({**self.state, 'text': event.String})

    def handle_submit(self, event):
        self.set_state({
            'text': '',
            'items': [*self.state['items'], self.state['text']]
        })

    def render(self):
        return wsx(
            [c.Frame, {'title': 'My First TODO app'},
             [c.Block, {'name': 'main-content'},
              [c.StaticText, {'label': 'What needs to be done?'}],
              [c.TextCtrl, {'value': self.state['text']}],
              [c.Button, {'label': 'Add', 'on_click': self.handle_submit}],
              [c.StaticText, {'label': 'TO DO:'}],
              [TodoList, {'items': self.state['items'], 'on_click': self.handle_complete}]]]
        )

if __name__ == '__main__':
    app = wx.App()
    frame = render(create_element(TodoApp, {}), None)
    frame.Show()
    app.MainLoop()

Where to go from here?

Checkout the docs folder for more detailed guides and walk throughs

Philosophy

It's a library first. re-wx is "just" a library, not a framework. Beacuse it's a library, you can use as much or as little of as you need. It requires no application-level total buy in like a framwork would. You don't have to do everything the "re-wx way. Further, the output from a re-wx render is a plain old WXPython component. Meaning, all re-wx components ARE WX components, and thus require no special handling to integrate with your existing code base.

It's intended to be symbiotic with WXPython re-wx is not trying to be an general purpose abstraction over multiple backend UI kits. It's lofty goals begin and end with it being a way of making writing native, cross-platform UIs in WXPython easier. As such, it doesn't need reconcilers, or generic transactions, or any other abstraction related bloat. As a result, re-wx's core codebase is just a handful of files and can be understood in an afternoon.

Given the symbiotic nature, practicality is favored over purity of abstraction. You'll mix and match WXPython code and re-wx code as needed. A good example of this is for transient dialogs (confirming actions, getting user selectsions, etc..). In React land, you'd traditionally have a modal in your core markup, and then conditionally toggle its visibility via state. However, in re-wx, you'll just use the dialog directly rather than embedding it in the markup and handling its lifecycle via is_open style state flags. This is practical to do because, unlike React in Javascript, WX handles managing the UI thread thus allowing us to block in place without any negative effects. Which enables writing straight forward in-line Dialog code.

def handle_choose_dir(self, event): 
    dlg = wx.DirDialog(None)
    if dlg.Show() == wx.ID_OK:
        self.setState({'directory': dlg.GetPath()})

Compromises and caveats in the design

While you'll program in a declarative style and enjoy the benefits that one-way data flows bring, a caveat is that not all components technically follow the unidirectional dataflow. The design of WX and the native APIs means that certain events are only fired after internal states have been updated. So, for components like wx.ComboBox and wx.TextCtrl, handlers don't have a chance to operate until the widgets themseves have completed their work.

The good news is that in practice, this is generally something you'll never notice or need to worry about. All updates are all done inside of a Freeze/Thaw transaction, thus hiding any visual quirks or flicker which may have come from re-wx forcing WX back into the state you specify rather than its own internally managed one.

API Surface area:

Only the most common attributes are currently managed by declarative props (basically, most of what falls under wx.Control). For example, specifics such as InsertionPoints in TextCtrls are considered out of scope for rewx. Refs act as a handy escape-hatch for when you need access to the full WX API. Be sure to checkout the Componet Docs for the full list of supported props.

Stubborn Widgets:

Some WXPython widget, like the prefab RadioGroup, cannot have its number of options changed after creation. So, updating the choices prop will have no effect. Luckily, these components are few and far between, and usually have easy work arounds or alternatives. See the Componet Docs for more info.

Stuck? Need some help? Just have a question?

Open an issue here, or feel free to hit me up directly at [email protected] and we'll get it sorted out!

Contributing

All contributions are welcome! Just make sure you follow the Contributing Guidelines.

License

re-wx is MIT licensed.

More Repositories

1

Gooey

Turn (almost) any Python command line program into a full GUI application with one line
Python
20,313
star
2

GooeyExamples

Example programs to Demonstrate Gooey's functionality
300
star
3

pyrobot

A pure python windows automation library loosely modeled after Java's Robot Class.
Python
221
star
4

Burrito-Bot

Python bot to play Burrito Bison
Python
86
star
5

home-theater-calculator

Set yo shiz up right
PureScript
79
star
6

Deoplice

Very spicy additions to Lombok's annotations
Java
49
star
7

Dropler

A drag and drop Image upload plugin for CKEditor
JavaScript
34
star
8

Clever-Skype

Turing test (or troll) your friends! CleverSkype connects the output of Skype's chat to cleverbot.com
Python
19
star
9

ArgDoc

A documentation generator (of the aesthetic variety) for command line programs
18
star
10

OldBlog

Code dump from various blog posts.
Python
15
star
11

GooeyVideo

A small collection of FFMPEG tools which I use while working on Gooey
Python
14
star
12

DoNotStarveBackup

Backup Utility for automatically managing and restoring the Don't Starve save files
Java
10
star
13

MyBlog

Personal blog
Clojure
8
star
14

bitminer

Bitcoin Mining for Fun and No Profit
Clojure
7
star
15

GooeyPackager

The easiest way to transform your Gooey powered scripts into standalone executables
Python
4
star
16

SavingOverIt

A degenerate tool for saving your progress in the game Getting Over It
Python
3
star
17

Panelizomaticizer3000

Surely, It goes without saying
Java
3
star
18

Gooey-Builder

Generate Build Specs for Gooey!
JavaScript
2
star
19

Google-DoodleBots

Fun lunch break projects :)
Python
1
star
20

react-tabs

Simple tab components for React
JavaScript
1
star
21

google-appengine-wx-launcher

Automatically exported from code.google.com/p/google-appengine-wx-launcher
Python
1
star
22

Whombot

All the pogo.com points you never wanted..!
Python
1
star
23

smartkeys.js

IntelliJ style Smart Keys in the Browser!
JavaScript
1
star
24

PythonMeetup

Repo for the meetup puzzle
Python
1
star
25

color-distance-tool

A hyper-specific tool for selecting arbitrary colors from the screen and comparing them in various color spaces.
Python
1
star
26

quick-validate

One click HTML validation for all files in your project folder
Python
1
star
27

DoNotStarveBackupExecutable

Executable file for the Don't Starve Backup Util
1
star