Coder Social home page Coder Social logo

pydux's Introduction

pydux

Redux reimplemented in Python.

PyPI version

To see it in action, try the todos demo. (Mouse-click needed, as in the original demo)

pip install urwid_todos ; python -m urwid_todos

What is it? Why would I want it?

The Redux Readme is a good place to start.

The Gist

The whole state of your app is stored in an object tree inside a single store. The only way to change the state tree is to emit an action, an object describing what happened. To specify how the actions transform the state tree, you write pure reducers.

That's it!

from __future__ import print_function
import pydux

# This is a reducer, a pure function with (state, action) => state signature.
# It describes how an action transforms the state into the next state.
#
# The shape of the state is up to you: it can be a primitive, an array, an object,
# or even an frozendict data structure [1]. The only important part is that you should
# not mutate the state object, but return a new object if the state changes.
#
# [1]: https://pypi.python.org/pypi/frozendict
#
# In this example, we use a `if` statement and strings, but you can use a
# helper that follows a different convention if it makes sense for your
# project.
def counter(state, action):
    if state is None:
        state = 0
    if action is None:
        return state
    elif action['type'] == 'INCREMENT':
        return state + 1
    elif action['type'] == 'DECREMENT':
        return state - 1
    return state

# Create a Redux store holding the state of your app.
# Its API is { subscribe, dispatch, get_state }.
store = pydux.create_store(counter)

# You can use subscribe() to update the UI in response to state changes.
store.subscribe(lambda: print(store.get_state()))

# The only way to mutate the internal state is to dispatch an action.
# The actions can be serialized, logged or stored and later replayed.
store.dispatch({ 'type': 'INCREMENT' })
# 1
store.dispatch({ 'type': 'INCREMENT' })
# 2
store.dispatch({ 'type': 'DECREMENT' })
# 1
Further examples of Python usage

urwid_pydux provides a React-Redux Component API for text/console GUIs made with Urwid.

urwid_todos is a reimplementation of the Redux todos example made with urwid_pydux.

canute-ui is the user interface for a multi-line electronic Braille e-book reader.

Acknowledgements

The initial test suite was imported from python-redux, a Redux implementation for Python 3.4+.

pydux's People

Contributors

usrlocalben avatar kasbah avatar chriskiehl avatar isnifer avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.