Package: kivyredux
Kivy Reduxkivyreduxis a python package, which allows to implement theRedux's like implementation done on Kivy inspired fromKovak's redux implementationInstallationUse thepippackage manager to install the kivy-redux packagepipinstallkivyreduxRequirementkivy-redux requires a python of version on or later 3.x i.e., python>=3.xComponentsStateStateobject lets you create a simple State similar to dictionary except it has stores them as attributesgetto get a requested attribute from state (instance method)updateto update a particular attribute within the state (instance method)Static methodsget_keyget a common attribute inside a State objectupdate_keyupdate a common attribute within the state objectActionActionclass lets you create actions for every dispatch to be mappedAction(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 dictionaryaction_type - identifier of actionReducerReducerclass lets you create a reducer object to be passed to Store for updating the stateReducer(reducer_id, reducer_cb)reducer_id unique identifier for the reducer
--- Defaults to a random stringreducer_cb Callback function to be associated with the reducer instance
--- Defaults toNoneStoreStoreis the common store class which allows to handle connection with all the widgets and their properties being binded and mapped with state for updatesStore(reducers, state)reducers is the collection of reducers that are associated with callback
-- Defaults to an empty collectionstate 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 objectInstance Methodsconnect(mapper, dispatcher, widget)mapper -> mapping function for widget props whenever state is updated
--- Defaults toNonedispatcher -> binded with the widget's property and dispatches the action for any property
changes
--- Defaults toNoneFunction parameter -> dispatch_function, widgetFunction 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 storeadd_mapping_binding(widget, new_mapper, new_dispatcher, replace_mapping, replace_binding)widget -> Widget to insert additional mapping and binding functionnew_mapper -> new mapping function
-- Defaults toNonenew_dispatcher -> new dispatch functionSimilar to connect function-- Defaults toNonereplace_mapping -> replace the exisiting mapping function with new_mapping
--- Defaults toFalsereplace_binding -> unbind all initially binded properties and bind newly dispatched proeprties
--- Defaults toFalseadd_reducer(new_reducer)
*new_reducer -> new reducer to be added to reducer's connection
--- Must be ofReducertypeUsage#store.pyfromkivyreduximportState,Store,Action,Reducercommon_state=State(saying_hi=False,saying_bye=True)#Can also user [common_state={}]defsample_reducer(action,state=common_state):ifaction.type=='HI':previous_value=state.get('saying_hi')state.update('saying_hi',notprevious_value)elifaction.type=='BYE':previous_value=state.get('saying_bye')state.update('saying_bye',notprevious_value)else:passreturnstatehi_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.pyfromkivy.uix.labelimportLabelfromkivy.lang.builderimportBuilderfrom.storeimportcommon_store,hi_action,bye_action#class componentsBuilder.load_file('hi_bye.kv')classHi(Label):'''Class component which inherits Label widget'''__widget__='HiClass'# [IMPORTANT] to map with .kv file's namepassdefmapper(state,widget):#Maps Hiwidget.hi=state.get('saying_hi')defdispatcher(dispatch,widget):#dispatches hi actionreturn{'bind':{'hi':lambda*largs,**kwargs:dispatch(hi_action)},'init':{'hi':False}}HiClass=common_store.connect(mapper,dispatcher,Hi)#class component createddefbye_mapper(state,widget):#Maps byewidget.bye=state.get('saying_bye')defbye_dispatcher(dispatch,widget):#Dispatches bye actionreturn{'bind':{'bye':lambda*largs,**kwargs:dispatch(bye_action)},'init':{'bye':True}}defByeFunction(*largs,**kwargs):'''Functional component which returns a Label widget'''returnLabel(**kwargs)ByeFunction=cs.connect(bye_mapper,bye_dispatcher,ByeFunction)#Functional component created#main.pyfromkivy.appimportAppfromkivy.uix.boxlayoutimportBoxLayoutfrom.hiimportHiClass,ByeFunctionfromkivy.factoryimportFactoryclassHBContainer(BoxLayout):passclassHBApp(App):defbuild(self):Factory.register('ByeFunction',cls=ByeFunction)Factory.register('HiClass',cls=HiClass)returnHBContainer()#hb.kv'''<HBContainer>:HiClass:ByeFunction:text:'Bye' if self.bye else 'Hi''''LicenseMIT