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

整理python快速构建数据可视化前端的Dash库

一.Dash框架

# 导入 Dash 相关库
import dash
from dash import dcc, html  # dcc 是 Dash 核心组件库,html 是 HTML 组件库
from typing import Generic# 创建一个 Dash 应用实例
app = dash.Dash(__name__)# 定义应用的布局
app.layout = html.Div(children=[# 添加一个标题html.H1(children='你好,Dash!'),# 添加一段描述文字html.Div(children='''Dash:一个用于 Python 的 Web 应用框架。'''),# 添加一个图表dcc.Graph(id='example-graph',  # 图表的 ID,用于回调函数figure={'data': [  # 图表的数据{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': '上海'},{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': '北京'},],'layout': {  # 图表的布局'title': 'Dash 数据可视化示例'  # 图表的标题}})
])# 运行应用
if __name__ == '__main__':app.run(debug=True)  # 启动应用,debug=True 表示开启调试模式

二.输入框

# 导入 Dash 相关库
from dash import Dash, dcc, html, Input, Output# 创建 Dash 应用实例
app = Dash(__name__)# 定义应用的布局
app.layout = html.Div([# 创建一个文本输入框dcc.Input(id='input',  # 输入框的 ID,用于回调函数value='初始值',  # 输入框的默认值type='text'  # 输入框类型为文本),# 创建一个用于显示输出的 Divhtml.Div(id='output')
])# 定义回调函数
@app.callback(Output('output', 'children'),  # 输出到 id 为 'output' 的 Div 的 children 属性Input('input', 'value')  # 输入来自 id 为 'input' 的输入框的 value 属性
)
def update_output_div(input_value):# 返回格式化后的字符串,显示用户输入的内容return f'你输入了: {input_value}'# 运行应用
if __name__ == '__main__':app.run(debug=True)  # 启动应用,debug=True 表示开启调试模式

三.动态

from dash import Dash, html, dcc, Input, Output
import plotly.express as px
import pandas as pd# 创建 Dash 应用
app = Dash(__name__)# 定义示例数据集
datasets = {'数据集1': pd.DataFrame({'x': [1, 2, 3, 4, 5],'y': [10, 15, 13, 17, 21]}),'数据集2': pd.DataFrame({'x': [1, 2, 3, 4, 5],'y': [5, 10, 8, 12, 15]}),'数据集3': pd.DataFrame({'x': [1, 2, 3, 4, 5],'y': [20, 18, 22, 19, 25]})
}# 定义布局
app.layout = html.Div([html.H1("动态折线图示例"),  # 标题dcc.Dropdown(id='dataset-dropdown',  # 下拉菜单的 IDoptions=[{'label': name, 'value': name} for name in datasets.keys()],  # 下拉菜单选项value='数据集1'  # 默认选中的数据集),dcc.Graph(id='line-chart')  # 用于显示折线图的 Graph 组件
])# 定义回调函数
@app.callback(Output('line-chart', 'figure'),  # 输出到 id 为 'line-chart' 的 Graph 组件的 figure 属性Input('dataset-dropdown', 'value')  # 输入来自 id 为 'dataset-dropdown' 的下拉菜单的 value 属性
)
def update_line_chart(selected_dataset):# 获取选中的数据集df = datasets[selected_dataset]# 使用 Plotly Express 创建折线图fig = px.line(df, x='x', y='y', title=f'{selected_dataset} 折线图')return fig# 运行应用
if __name__ == '__main__':app.run(debug=True)  # 启动应用,debug=True 表示开启调试模式

四.压力表

from dash import Dash, html
import dash_daq as daq# 创建 Dash 应用
app = Dash(__name__)# 定义布局
app.layout = html.Div(style={'textAlign': 'center'},children=[daq.Thermometer(id='thermometer',value=25,min=0,max=100,label="温度计",style={'margin': '20px'}),daq.Gauge(id='gauge',value=50,min=0,max=100,label="压力表",style={'margin': '20px'})]
)# 运行应用
if __name__ == '__main__':app.run(debug=True)

整理快速构建数据可视化前端 的Dash 相关库及示例,先展示部分,后续然后点赞收藏等反馈良好,再继续更新一波进阶版;

整理不易,诚望各位看官点赞 收藏 评论 予以支持,这将成为我持续更新的动力源泉。若您在阅览时存有异议或建议,敬请留言指正批评,让我们携手共同学习,共同进取,吾辈自当相互勉励!


文章转载自:

http://VISl0W09.xbmwh.cn
http://hWq1iEEc.xbmwh.cn
http://3OZWKFFq.xbmwh.cn
http://Hl9TqzJQ.xbmwh.cn
http://oKQFAOVb.xbmwh.cn
http://63zwhg7E.xbmwh.cn
http://3TTcoRI3.xbmwh.cn
http://AyjbExQH.xbmwh.cn
http://ooKoxWRp.xbmwh.cn
http://yTUhA4FG.xbmwh.cn
http://cUCW4pJo.xbmwh.cn
http://IACnqirt.xbmwh.cn
http://ARhIOqFJ.xbmwh.cn
http://hSUPOSi3.xbmwh.cn
http://eMI8tfrb.xbmwh.cn
http://PzFxdJPr.xbmwh.cn
http://IQlCx0CB.xbmwh.cn
http://yJkOctPV.xbmwh.cn
http://x01S9bzY.xbmwh.cn
http://TeHto1KY.xbmwh.cn
http://Umi8qBBW.xbmwh.cn
http://jGweoyC5.xbmwh.cn
http://BXRMW3dR.xbmwh.cn
http://vDxdJhKg.xbmwh.cn
http://XlWJXSpp.xbmwh.cn
http://l67SoUqe.xbmwh.cn
http://ugFyZr8c.xbmwh.cn
http://GYmwVsNC.xbmwh.cn
http://61J2eqe3.xbmwh.cn
http://nmnLnwzB.xbmwh.cn
http://www.dtcms.com/a/374585.html

相关文章:

  • Redis缓存穿透、缓存击穿与雪崩防护及性能优化实战指南
  • ArcGIS学习-20 实战-地形研究
  • Ubuntu下基于Nginx+ffmpeg+video.js的HLS流媒体视频播放方案
  • Vue2 VS Vue3
  • 【ArcGIS】如何编辑图层的属性表
  • VueFlow的箭头怎么调整
  • 基于Vue3 +ElementuiPlus + Dexie.js自研的浏览器插件新建标签页tab
  • 【序列晋升】30 Spring Cloud Vault 安全配置管理的微服务守护者
  • 狂想-一种新颖的低成本内嵌标记的视触觉感知前导方案
  • 兰洋科技双展联动展示液冷创新成果,技术驱动打造绿色算力新基建
  • INDEMIND亮相2025科技创变者大会,以机器人空间智能技术解锁具身智能新边界
  • 百度SEM里什么是搜索广告、搜索词、否定关键词、上方位(竞价)广告?
  • 百度竞价推广:百度搜索竞价推广代运营
  • rabbitmq如何保证消息不丢失
  • 做百度SEM付费搜索推广时,竞价账号定向怎么设置?
  • html+css+JavaScript实现一个简单的登录
  • 【国内电子数据取证厂商龙信科技】从SQL语句开始数据库分析
  • 字节跳动Seed推出「机器人大脑」Robix:让机器人学会思考、规划与灵活互动
  • 【ComfyUI】Flux Schnell Fp8量化版图像生成
  • 【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
  • 从零开始的云计算生活——第六十天,志在千里,使用Jenkins部署K8S
  • 平板热点频繁断连?三步彻底解决
  • nand flash的擦除命令使用
  • 《Pod调度失效到Kubernetes调度器的底层逻辑重构》
  • OC-单例模式
  • C语言链表设计及应用
  • 中级统计师-统计法规-第三章 统计法的基本原则
  • 【VR音游】音符轨道系统开发实录与原理解析(OpenXR手势交互)
  • web前端安全-什么是供应链攻击?
  • Saucony索康尼推出全新 WOOOLLY 运动生活羊毛系列 生动无理由,从专业跑步延展运动生活的每一刻