Skip to main content

“帮助构建 dash kws 应用程序”

项目描述

Dash 基础架构框架

快速开始

应用程序的架构

建筑学

介绍

该包定义了用于开发 Dash 应用程序的标准框架,包括原型设计和生产应用程序。

该框架的核心思想是将核心逻辑与显示和交互元素分离。

该框架依赖于几个核心概念,它们是应用程序的构建块。

  • 回调是 Dash Apps 功能的抽象。它们执行 Dash 输入、核心功能和 Dash 输出之间的转换。因此,回调至少应该由 a_pre_function_hook和 a组成_post_function_hook,包装一个代表核心功能的函数。
  • 组件是 Dash 组件的抽象。组件是具有自包含 Dash 回调的对象。这些回调应该只对组件的功能进行操作,而没有有意义的功能。例如,StoreComponent有一个回调来将另一个回调的输出存储到它的状态中。
  • Dash 应用程序是 Dash 标准应用程序的抽象。一个 Dash 应用程序有一个或多个Components 和Callbacks。
  • 超级应用DashApp是在一个全局应用内的多个 s 的组合。超级应用为其中注册的每个核心回调公开一个 API。

除了这个框架之外,这个包还添加了一些帮助类,例如 aStoreComponent或 a FigureComponent。

核心概念

回调

例子

# callbacks.py
from dash_infra import as_callback
import numpy as np

def to_linspace(state, num_points):
    # A hook function takes the state of the callback as a first argument

    return np.linspace(0, 100, num_points)

def to_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 callback
    NumpySerializer,                    # Mixins adds capabilities to the callback
    pre_func_hooks=[to_linspace],       # Takes the input and changes it into a np.array
    post_func_hooks=[to_figure],        # Takes the output and converts it into a plotly graph
)
def square(x: "Arrayable") -> np.ndarray:
    """
    Computes the square of an array x
    """
    # the function is defined as it would normally be
    return np.array(x) ** 2

此时回调square未绑定到任何应用程序或输入。它仍然是可调用的,并square([1,2,3])返回[1, 4, 9]。

成分

例子

# components.py
from dash_core_components import Graph, Slider

from dash_infra import as_component
from dash_infra.components.groups import ComponentGroup
from dash_infra.html_helpers.divs import Col


graph_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)

标准的dash组件可以转移到我们的框架中as_component(obj, id, container)。这种转换允许在 Dash 应用程序中更轻松地注册

短跑应用

例子

# app.py

from dash_infra import KWSDashApp
from .components import group
from .callbacks import square

dash_app = KWSDashApp("A multplier component", doc=__doc__)
dash_app.register_component(group)
dash_app.register_callback(
    square, 
    outputs=("square", "figure"), 
    inputs=[("input", "value")]
)

超级应用

超级应用封装了 Dash 应用,并为 Dash 回调提供了一个 RESTful API。

专卖店

存储组件是一种抽象,用于存储回调输出以供以后使用。存储内容可供所有回调使用。

商店

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

kws_dash_infra-0.3.6.tar.gz (114.9 kB 查看哈希)

已上传 source