当前位置: 首页 > news >正文

【Dash】使用 dash_mantine_components 创建图表

一、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 Mantine Components

Dash Mantine is as community-maintained library built off of the Mantine component system. Although it is not officially maintained or supported by the Plotly team, Dash Mantine is another powerful way of customizing app layouts. The Dash Mantine Components uses the Grid module to structure the layout. Instead of defining a row, we define a dmc.Grid, within which we insert dmc.Col s and define their width by assigning a number to the span property.

For the app below to run successfully, make sure to install the Dash Mantine Components library: pip install dash-mantine-components==0.12.1

from dash import Dash, dash_table, dcc, callback, Output, Input
import pandas as pd
import plotly.express as px
import dash_mantine_components as dmcdf = pd.read_csv('https://raw.githubusercontent.com/GarciaShanCW/DATA/main/DataAnalysis01.csv')app = Dash(__name__)app.layout = dmc.MantineProvider(theme={'colorScheme': 'light'},children=dmc.Container([dmc.Title('My First App with Data, Graph, and Control', size='h3'),dmc.RadioGroup([dmc.Radio(i, value=i) for i in ['pop', 'lifeExp', 'gdpPercap']],id='my-dmc-radio-item',value='lifeExp',size='sm'),dmc.Grid([dmc.Col([dash_table.DataTable(data=df.to_dict('records'),page_size=12, style_table={'overflowX': 'auto'})], span=6),dmc.Col([dcc.Graph(figure={}, id='graph-placeholder')], span=6),]),], fluid=True)
)@callback(Output(component_id='graph-placeholder', component_property='figure'),Input(component_id='my-dmc-radio-item', component_property='value')
)
def update_graph(col_chosen):fig = px.histogram(df, x='continent', y=col_chosen, histfunc='avg')return figif __name__ == '__main__':app.run(debug=True)

三、解读

dash_mantine_components 是一个基于 Mantine Design System 的 Dash 组件库。它允许开发者在 Dash 应用程序中使用 Mantine 的组件来构建用户界面。Mantine 是一个现代化的、功能丰富的 React UI 组件库。

from dash import Dash, dash_table, dcc, callback, Output, Input
import pandas as pd
import plotly.expree as px
imoprt dash_mantine_components as dmc
  • 导入 Dash 库中的主要组件,包括Dash 类、dash_table、dcc(Dash 核心组件库)以及回调函数所需的 Output 和 Input
  • 导入 pandas 库,并简称 pd,用于数据处理
  • 导入 plotly.express 库,并简称为 px,用于创建交互式图表
  • 导入 dash_mantine_components 库,并简称为 dmc ,这是一个 Dash UI 组件库
app = Dash(__name__)app.layout = dmc.MantineProvider(theme={'colorScheme': 'light'},children=dmc.Container([dmc.Title('My First App with Data, Graph, and Control', size='h3'),dmc.RadioGroup([dmc.Radio(i, value=i) for i in ['pop', 'lifeExp', 'gdpPercap']],id='my-dmc-radio-item',value='lifeExp',size='sm'),dmc.Grid([dmc.Col([dash_table.DataTable(data=df.to_dict('records'),page_size=12, style_table={'overflowX': 'auto'})], span=6),dmc.Col([dcc.Graph(figure={}, id='graph-placeholder')], span=6),]),], fluid=True)
)
  • app.layout = dmc.MantineProvider(....) 设置 Dash 应用的布局,使用 MantineProvider 组件来提供主题样式
  • theme={'colorScheme': 'light'}, 设置应用的主题色为浅色
  • children=dmc.Container([...]) 在 MantineProvider 中添加一个容器组件,包含应用的主要内容
  • dmc.Title(...) 添加一个标题组件,显示应用的标题
  • dmc.RadioGroup([...]) 创建一个单选按钮组,允许用户选择不同的数据列进行图表展示
  • dmc.Grid([...]) 创建一个网格布局,用于在页面上排列不同的组件
  • dmc.Col([...]) 在网格布局中添加列组件,用户放置 DataTable 或 Graph 等组件
  • dash_table.DataTable(...) 添加一个 DataTable 组件,用于显示数据表
  • dcc.Graph(...) 添加 Graph 组件,用于显示图表
@callback(Output(component_id='graph-placeholder', component_property='figure'),Input(component_id='my-dmc-radio-item', component_property='value')
)
def update_graph(col_chosen):fig = px.histogram(df, x='continent', y=col_chosen, histfunc='avg')return fig
  • 定义一个回调函数,用于更新图表
  • Output 指定回调函数的输出,即更新 graph-placeholder 组件的 figure 属性
  • Input 指定回调函数的输入,即监听 my-dmc-radio-item 组件的 value 属性变化
  • 定义回调函数 update_garaph,根据用户选择的列更新图表
  • px.histogram(...) 使用 plotly.express 创建一个直方图,返回更新后的图表对象

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 动态规划求解最小斯坦纳树(证了一天两夜)
  • 1998年考研真题英语二(204)阅读理解翻译
  • C语言宠物系统
  • CnosDB 元数据集群 – 分布式时序数据库的大脑
  • C++(week15): C++提高:(五)Redis数据库
  • 将PPT中的元素保存为高清图片
  • 看图学sql之sql的执行顺序
  • 分布式接口文档聚合,Solon 是怎么做的?
  • Qt编程技巧小知识点(2)GPIB缓存区数据读取
  • 快递批量查询有什么软件安利一下
  • 关于手机中的红外遥控
  • leetcode hot 100(1)
  • 多尺度病理图像纹理特征作为肺腺癌预后预测的新指标|文献精读·24-08-09
  • 【vue2】回车发送,Ctrl+回车换行,shift+回车换行禁用
  • 【动态规划】1、不同路径II+2、三角形最小路径和
  • 【347天】每日项目总结系列085(2018.01.18)
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • Android优雅地处理按钮重复点击
  • CAP理论的例子讲解
  • Hibernate最全面试题
  • JSONP原理
  • React-flux杂记
  • Spring Boot MyBatis配置多种数据库
  • TypeScript迭代器
  • 面试总结JavaScript篇
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 《天龙八部3D》Unity技术方案揭秘
  • ​​​​​​​​​​​​​​Γ函数
  • ​LeetCode解法汇总518. 零钱兑换 II
  • # Java NIO(一)FileChannel
  • # 达梦数据库知识点
  • #stm32驱动外设模块总结w5500模块
  • #微信小程序:微信小程序常见的配置传旨
  • $GOPATH/go.mod exists but should not goland
  • (~_~)
  • (03)光刻——半导体电路的绘制
  • (1)常见O(n^2)排序算法解析
  • (11)MATLAB PCA+SVM 人脸识别
  • (C++17) optional的使用
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (LeetCode 49)Anagrams
  • (LeetCode C++)盛最多水的容器
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (SERIES12)DM性能优化
  • (二)丶RabbitMQ的六大核心
  • (附源码)springboot车辆管理系统 毕业设计 031034
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (附源码)计算机毕业设计SSM智能化管理的仓库管理
  • (四)linux文件内容查看
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • (转)拼包函数及网络封包的异常处理(含代码)
  • (转载)Google Chrome调试JS
  • (最优化理论与方法)第二章最优化所需基础知识-第三节:重要凸集举例
  • .equal()和==的区别 怎样判断字符串为空问题: Illegal invoke-super to void nio.file.AccessDeniedException
  • .NET Core IdentityServer4实战-开篇介绍与规划