Skip to main content

kivyredux - Redux for Kivy

Project description

Kivy Redux

kivyredux is a python package, which allows to implement the Redux's like implementation done on Kivy inspired from Kovak's redux implementation

Installation

Use the pip package manager to install the kivy-redux package

pip install kivyredux

Requirement

kivy-redux requires a python of version on or later 3.x i.e., python>=3.x

Components

  • State

    • State object lets you create a simple State similar to dictionary except it has stores them as attributes
      • get to get a requested attribute from state (instance method)
      • update to update a particular attribute within the state (instance method)
      • Static methods

        • get_key get a common attribute inside a State object
        • update_key update a common attribute within the state object
  • Action

    • Action class lets you create actions for every dispatch to be mapped
      • Action(action_type, **data_to_be_used_by_action)
        • data_to_be_used_by_action are created as an dictionary of data used by the Action instance - action.data @property --- Defaults to empty dictionary
        • action_type - identifier of action
  • Reducer

    • Reducer class lets you create a reducer object to be passed to Store for updating the state
      • Reducer(reducer_id, reducer_cb)
        • reducer_id unique identifier for the reducer --- Defaults to a random string
        • reducer_cb Callback function to be associated with the reducer instance --- Defaults to None
  • Store

    • Store is the common store class which allows to handle connection with all the widgets and their properties being binded and mapped with state for updates
    • Store(reducers, state)
      • reducers is the collection of reducers that are associated with callback -- Defaults to an empty collection
      • state is the state which the store will handle with the dispatch of action within widgets and maps the state of widget properties with updated state --- Defaults to and empty State object
      • Instance Methods

        • connect(mapper, dispatcher, widget)
          • mapper -> mapping function for widget props whenever state is updated --- Defaults to None
          • dispatcher -> binded with the widget's property and dispatches the action for any property changes --- Defaults to None
            • Function parameter -> dispatch_function, widget
            • Function to return
                {
                    bind:{
                        prop_name:lambda *largs, **kwargs: dispatch_function(prop_action)
                    },
                    init:{
                        prop_name:initial_value
                    } // new properties for the widget
                }
            
          • widget -> Widget to bind with store
        • add_mapping_binding(widget, new_mapper, new_dispatcher, replace_mapping, replace_binding)
          • widget -> Widget to insert additional mapping and binding function
          • new_mapper -> new mapping function -- Defaults to None
          • new_dispatcher -> new dispatch function Similar to connect function -- Defaults to None
          • replace_mapping -> replace the exisiting mapping function with new_mapping --- Defaults to False
          • replace_binding -> unbind all initially binded properties and bind newly dispatched proeprties --- Defaults to False
        • add_reducer(new_reducer) *new_reducer -> new reducer to be added to reducer's connection --- Must be of Reducer type

Usage

#store.py
from kivyredux import State, Store, Action, Reducer
common_state = State(saying_hi=False, saying_bye=True) #Can also user [common_state={}]
def sample_reducer(action, state=common_state):
    if action.type == 'HI':
        previous_value = state.get('saying_hi')
        state.update('saying_hi', not previous_value)
    elif action.type == 'BYE':
        previous_value =state.get('saying_bye')
        state.update('saying_bye', not previous_value)
    else:
        pass
    return state
hi_bye_reducer = Reducer(reducer_cb=sample_reducer)
hi_action = Action(action_type='HI')
bye_action = Action(action_type='BYE')
common_store = Store(reducers=[hi_bye_reducer], state=common_state)
#sample widget
#hi_bye.kv
'''
#:kivy 1.1.0
<HiClass>:
    text:'HI' if self.hi else 'Bye'
#Functional component cannot be specified with basic props
#i.e.,
#<ByeFunction>:
#    color:[1,1,1,1] #doesn't work
'''
#hi.py
from kivy.uix.label import Label
from kivy.lang.builder import Builder
from .store import common_store, hi_action, bye_action
#class components
Builder.load_file('hi_bye.kv')
class Hi(Label):
    '''
        Class component which inherits Label widget
    '''
    __widget__ = 'HiClass' # [IMPORTANT] to map with .kv file's name
    pass

def mapper(state, widget):
    #Maps Hi
    widget.hi = state.get('saying_hi')

def dispatcher(dispatch, widget):
    #dispatches hi action
    return {
        'bind':{
            'hi':lambda *largs, **kwargs: dispatch(hi_action)
        },
        'init':{
            'hi':False
        }
    }

HiClass= common_store.connect(mapper, dispatcher, Hi)#class component created

def bye_mapper(state,widget):
    #Maps bye
    widget.bye = state.get('saying_bye')

def bye_dispatcher(dispatch, widget):
    #Dispatches bye action
    return {
        'bind':{
            'bye':lambda *largs, **kwargs: dispatch(bye_action)
        },
        'init':{
            'bye':True
        }
    }

def ByeFunction(*largs, **kwargs):
    '''
        Functional component which returns a Label widget
    '''
    return Label(**kwargs)

ByeFunction = cs.connect(bye_mapper, bye_dispatcher, ByeFunction)#Functional component created
#main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from .hi import HiClass, ByeFunction
from kivy.factory import Factory
class HBContainer(BoxLayout):
    pass

class HBApp(App):
    def build(self):
        Factory.register('ByeFunction', cls=ByeFunction)
        Factory.register('HiClass', cls=HiClass)
        return HBContainer()

#hb.kv
'''
<HBContainer>:
    HiClass:
    ByeFunction:
        text:'Bye' if self.bye else 'Hi'
'''

License

MIT

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

kivyredux-1.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kivyredux-1.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file kivyredux-1.1.tar.gz.

File metadata

  • Download URL: kivyredux-1.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.1

File hashes

Hashes for kivyredux-1.1.tar.gz
Algorithm Hash digest
SHA256 c4b3c3e293174728d45ce1e16433693ff8caf23b1a5fd70b1310ad719c4cf158
MD5 fb10b1321e4c2e6ab2213eaa35dee9e7
BLAKE2b-256 ff126f58b7942fa0850054b0dd71eea6762b75ec86807ec94bcd66dcf77b8dcd

See more details on using hashes here.

File details

Details for the file kivyredux-1.1-py3-none-any.whl.

File metadata

  • Download URL: kivyredux-1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.1

File hashes

Hashes for kivyredux-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 761f408366cd1f9278011271dedb1263a419aaf7ba3712410fb88ed9a8d6450d
MD5 a55402067449a194c272f59884867f86
BLAKE2b-256 303a99b361331b40697c6d0bd23d005b63275be123fd1758227c3aefcd68d4ce

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