【Dash】使用 Dash Design Kit (DDK) 创建图表

一、Styling Your App

The examples in the previous section used Dash HTML Components to build a simple app layout, but you can style your app to look more professional. This section will give a brief overview of the multiple tools that you can use to enhance the layout style of a Dash app:

  • HTML and CSS
  • Dash Design kit (DDK)
  • Dash Bootstrap Components
  • Dash Mantine Components

二、Dash Design Kit (DDK)

Dash Design Kit is our high levl UI framwork that is purpose-built for Dash. With Dash Design Kit, you don't need to use HTML or CSS. Apps are mobile responsive by default and everything is themable. Dash Design Kit is licensed as part of Dash Enterprise and officially supported by Plotly.

Here's an example of what you can do with Dash Design Kit (note that you won't be able to run this example without a Dash Enterprise license).

python 复制代码
# Import packages
from dash import Dash, html, dash_table, dcc, callback, Output, Input
import pandas as pd
import plotly.express as px
import dash_design_kit as ddk

# Incorporate data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv')

# Initialize the app
app = Dash(__name__)

# App layout
app.layout = ddk.App([
    ddk.Header(ddk.Title('My First App with Data, Graph, and Controls')),
    dcc.RadioItems(options=['pop', 'lifeExp', 'gdpPercap'],
                   value='liefExp',
                   inline=True,
                   id='my-ddk-radio-items-final'),
    ddk.Row([
        ddk.Card([
            dash_table.DataTable(data=df.to_dict('records'), page_size=12, style_table={'overflowX': 'auto'})
        ], width=50),
        ddk.Card([
            ddk.Graph(figure={}, id='graph-placeholder-ddk-final')
        ], width=50),
    ]),
])

# Add controls to build the interaction
@callback(
    Output(component_id='graph-placeholder-ddk-final', component_property='figure'),
    Input(component_id='my-ddk-radio-items-final', component_property='value')
)
def update_graph(col_chosen):
    fig = px.histogram(df, x='continent', y=col_chosen, histfunc='avg')
    return fig

# Run the app
if __name__ == '__main__':
    app.run(debug=True)

三、解读

Dash Design Kit 是一个提供一组预定义组件的库,这些组件具有一致的设计风格,使得构建具有统一外观的应用程序更加容易。

python 复制代码
# Import packages
from dash import Dash, html, dash_table, dcc, callback, Output, Input
import pandas as pd
import plotly.express as px
import dash_design_kit as ddk
  • 导入所需的模块。Dash 用于构建 Web 应用,pandas 用于数据处理,plotly.express 用于数据可视化
  • dash_design_kit 作为 Dash 的一个扩展库,提供一组预定义的组件。
python 复制代码
# Incorporate data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminder2007.csv')
  • 使用 pandas 的 read_csv 函数从 URL 中加载CSV数据文件到 DataFrame df 中。
python 复制代码
# Initialize the app
app = Dash(__name__)

# App layout
app.layout = ddk.App([
    # ...
])
  • 初始化 Dash 应用。
  • 设置应用的布局,使用 ddk.App 作为根容器。
python 复制代码
# App layout
app.layout = ddk.App([
    ddk.Header(ddk.Title('My First App with Data, Graph, and Controls')),
    dcc.RadioItems(options=['pop', 'liefExp', 'gdpPercap'],
                   value='lifeExp',
                   inline=True,
                   id='my-ddk-radio-items-final'),
    ddk.Row([
        ddk.Card([
            dash_table.DataTable(data=df.to_dict('records'), page_size=12, style_table={'overflowX': 'auto'})
        ], width=50),
        ddk.Card([
            ddk.Graph(figure={}, id='graph-placeholder-ddk-final')
        ], width=50),
    ]),
])
  • ddk.Header(...) :创建一个带有标题的页眉。
  • dcc.RadioItems(...):创建一组单选按钮,允许用户选择一个选项,这将用于后续的图表更新。
  • ddk.Row([ ddk.Card([...], width=50), ddk.Card([...], width=50),]) :包含两个 ddk.Card 组件,每个组件占据 50% 的宽度。
  • dash_table.DataTable(...):在第一个 ddk.Card 组件中,创建一个 dash_table.DataTable 组件,用于显示数据表,每页显示 12 条记录,并允许水平滚动。
  • ddk.Graph(...):在第二个 ddk.Card 组件中,创建一个 ddk.Graph 组件,用于显示图表,初始图表数据为空。
python 复制代码
@callback(
    Output(component_id='graph-placeholder-ddk-final', component_property='figure'),
    Input(component_id='my-ddk-radio-items-final', component_property='value')
)
def update_graph(col_chosen):
    fig = px.histogram(df, x='continent', y=col_chosen, histfunc='avg')
    return fig
  • 定义一个回调函数 update_graph,它根据 dcc.RadioItems 组件的选中值动态更新 ddk.Graph 组件的图表。
  • 在回调函数内部,使用 plotly.express 的 px.histogram 创建一个直方图。
  • 从回调函数返回图表对象 fig,Dash 将使用这个对象更新图表组件。
相关推荐
woxihuan123456几秒前
CSS移动端实现响应式导航菜单_利用媒体查询切换显示隐藏状态
jvm·数据库·python
CCPC不拿奖不改名1 分钟前
PostgreSQL数据库部署linux服务器流程
linux·服务器·数据库·windows·python·docker·postgresql
曲幽1 分钟前
你的Agent API还在裸奔?从认证到沙箱,我用FastAPI搭了几道防线
python·fastapi·web·security·jwt·oauth2·limit·sandbox·ai agent
donecoding2 分钟前
用了多年 nvm,我终于找到 Python 的版本管理「答案」:uv
python·node.js·前端工程化
沐知全栈开发2 分钟前
AngularJS 简介
开发语言
彳亍1013 分钟前
mysql如何通过mysqldump备份视图与触发器_使用相关参数
jvm·数据库·python
深度学习lover8 分钟前
<数据集>yolo 缆绳识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·缆绳识别
骑士雄师11 分钟前
学生管理系统python版本比对
开发语言·python
William.csj12 分钟前
Linux——服务器后台运行程序指南(包含 Python 与 .sh 脚本实战)
linux·服务器·python
basketball61612 分钟前
C++ 的 const 相关知识点总结
开发语言·c++