Skip to main content

Provides Plotly/Dash with support for a Redux style store of truth

Project description

dash-redux

Provides Plotly/Dash with support for a Redux style store of truth.

pip install dash-redux

In the ReduxStore component, state is derived from any number automatically generated surrogate stores. The surrogate stores are presented to the application for update via callbacks. When a callback completes, the updated surrogate is copied to a master store. This mechanism acts as a data multiplexor feeding application state changes from Dash UI events into an immutable single source of truth that can then be used to trigger additional activity in the UI.

The application interfaces with the store using the callbacks @Redux.update(...). This callback defines Dash IO to be used by the callback handler in the normal way. When triggered by a Dash event the associated function is called with the IO state together with a reference to a surrogate store that contains a copy of the master store. The callback modifies the state as required and returns the store state. ReduxStore then copies the new state into the master store. This, in turn, triggers an event on the master store that can be used by the application in standard Dash callbacks.

ReduxStore also supports an action_function execution model that enforces a formal separation of the Dash/UI from the model.

Update function:

Redux = ReduxStore(id='store', data = { "todo": []})

@Redux.update(button.input.n_clicks, input.state.value)
def new_activity(button_clicks, input, state):
    if button_clicks and input:
          state['todo'].append(input)
    return state

Action function:

from .todo_model import TODO_MODEL, ModelActions as action

Redux = ReduxStore(id='store', data=TODO_MODEL)

@Redux.action(button.input.n_clicks, input.state.value)
def _add(button_clicks, input):
    if button_clicks and input:
        return action.add, input
    else:
        NOUPDATE

...

todo_model.py:

class ModelActions:

    def add(state, input):
        current = state.todo.copy()
        state.past.append(current)
        state.todo.append(input)
        state.future = []
        return state

    def delete(state, index):
        state.past.append(state.todo.copy())
        state.todo.pop(index)
        return state

    def undo(state):
        if len(state.past) > 0:
            state.future.append(state.todo)
            state.todo = state.past.pop()
        return state

    def redo(state):
        if len(state.future) > 0:
            state.past.append(state.todo)
            state.todo = state.future.pop()
        return state

The snippets are taken from the todo example. See todo/todo.py.

TODO Example

An eighty line todo application demonstrates the ReduxStore. User input is added to the todo list. Elements from the list can be deleted. The example also provides UNDO and REDO buttons that demonstrate how to easily manage an applications state history.

To run the application clone this repository, then:

pip install -r requirements.txt

python app.py

Build the project

The dash-redux package is available on pypi. If needed, to create a local tarball, first change the release version in dash_spa/_version.py, then:

rm -rf dist dash_spa.egg-info build

python setup.py sdist bdist_wheel

The tarball is in dist/dash_spa-.tar.gz

To install the tarball in a dash project:

pip install dash_spa-<version>.tar.gz

Testing

Pytest and Dash Duo are used for testing. To run these tests both the Chrome browser and Chrome driver must be installed.

To run the tests:

pytest

Publish

twine upload dist/*

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

dash-redux-0.0.5.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

dash_redux-0.0.5-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file dash-redux-0.0.5.tar.gz.

File metadata

  • Download URL: dash-redux-0.0.5.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.12.0 pkginfo/1.8.3 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for dash-redux-0.0.5.tar.gz
Algorithm Hash digest
SHA256 f4bb72ac6e85a2cc437824c8872476c496c1e0be311c3a12433728b4346371fc
MD5 b43ccf59291762426af6f1b0ed204d26
BLAKE2b-256 ef98b689a5654c9af73770f64ee1be3a325e009d203c7a86be7ededfb8c0d081

See more details on using hashes here.

File details

Details for the file dash_redux-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: dash_redux-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.12.0 pkginfo/1.8.3 requests/2.28.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for dash_redux-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 85dfe2a536f99d6cbd5fb9264d5ff7df1a27d7fefedf9855dfa0ae19888a70f2
MD5 9b013850f1c7958b830525b78cfdd19f
BLAKE2b-256 d12b53dfa76aa4746a072bbc08110e255b106f5c8637928f259d2f761b046087

See more details on using hashes here.

Supported by

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