Package: kws-dash-infra
A Dash Infrastructure FrameworkQuickstartArchitecture of an applicationIntroductionThis package defines a standard framework for the development of Dash Applications, both in the context of prototyping and production applications.The core idea of this framework is to separatecore logicfromdisplay and interaction elements.The framework relies on several core concepts, which are the building blocks of an application.Callbacksare an abstraction for Dash Apps function.
They perform the conversion between the Dash inputs, the core function and the Dash outputs.
Therefore a callback should at least be composed of a_pre_function_hookand a_post_function_hook, wrapping a function representing the core functionality.Componentsare an abstraction for Dash components.
A Component is an object which has self contained Dash callbacks.
Those callbacks should only operate on the functionality of the Component, but have no meaningful functionality.
For instance, theStoreComponenthas a callback to store the outputs from an another Callback into its state.Dash Appsare an abstraction for Dash standard Application.
A Dash Application has one or multipleComponents, andCallbacks.Super Appsare a grouping of multipleDashApps inside a global application.
Super apps expose an API for every core Callback registered inside it.In addition to this framework, this package adds a few helper classes, such as aStoreComponentor aFigureComponent.Core ConceptsCallbacksExample# callbacks.pyfromdash_infraimportas_callbackimportnumpyasnpdefto_linspace(state,num_points):# A hook function takes the state of the callback as a first argumentreturnnp.linspace(0,100,num_points)defto_figure(state,array:np.ndarray):return{"data":[dict(x=state.current_inputs[0],y=outputs,mode="markers")]}@as_callback(# Changes the function into an unbound callbackNumpySerializer,# Mixins adds capabilities to the callbackpre_func_hooks=[to_linspace],# Takes the input and changes it into a np.arraypost_func_hooks=[to_figure],# Takes the output and converts it into a plotly graph)defsquare(x:"Arrayable")->np.ndarray:"""Computes the square of an array x"""# the function is defined as it would normally bereturnnp.array(x)**2At this point the callbacksquareis not bound to any app or inputs.
It is still a callable, andsquare([1,2,3])returns[1, 4, 9].ComponentsExample# components.pyfromdash_core_componentsimportGraph,Sliderfromdash_infraimportas_componentfromdash_infra.components.groupsimportComponentGroupfromdash_infra.html_helpers.divsimportColgraph_square=as_component(Graph,"square",container=Col(s=6))slider=as_component(Slider,"input",container=Col(s=6))group=ComponentGroup("main",slider,graph_square,graph_cube)Standard dash components can be transferred into our framework withas_component(obj, id, container).
This conversion allows easier registry in the Dash applicationDash AppsExample# app.pyfromdash_infraimportKWSDashAppfrom.componentsimportgroupfrom.callbacksimportsquaredash_app=KWSDashApp("A multplier component",doc=__doc__)dash_app.register_component(group)dash_app.register_callback(square,outputs=("square","figure"),inputs=[("input","value")])Super AppsSuper apps wrap Dash apps, and provide the Dash callbacks with a restful API.StoresStore Components are an abstraction used to store callback outputs for later usage. Store contents are available for all callbacks to use.