使用Dash开发交互式数据可视化网页--页面布局

2024-06-23 21:08

本文主要是介绍使用Dash开发交互式数据可视化网页--页面布局,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Dash应用布局

后续的操作前,需要安装如下Python包

pip install dash==0.20.0  # The core dash backend
pip install dash-renderer==0.11.2  # The dash front-end
pip install dash-html-components==0.8.0  # HTML components
pip install dash-core-components==0.18.1  # Supercharged components
pip install plotly --upgrade  # Plotly graphing library used in examples

使用Dash生成HTML

Dash应用包括两个部分,应用布局(layout)和数据交互(interactivity)。其中布局部分用来展示数据以及引导使用者使用。Dash提供了dash_core_componentsdash_html_components, 以类的方式对HTML和JS进行封装,便于调用。下面先构建一个最简单的布局

import dash
import dash_core_components as dcc
import dash_html_components as htmlapp = dash.Dash()app.layout = html.Div(children=[html.H1(children = 'Hello Dash'),html.Div(children = '''Dash: A web application frameworkd for Python.'''),dcc.Graph(id = 'example-graph',figure = {'dash':[{'x': [1,2,3], 'y':[4,1,2], 'type':'bar', 'name':'SF'},{'x': [1,2,3], 'y':[2,4,5], 'type':'bar', 'name':'Montrel'},],'layout':{'title':'Dash data Visualization'}})
])if __name__ == '__main__':app.run_server(debug=True, host='0.0.0.0')

首先用app=dash.Dash()创建了Dash应用的实例,这个实例可以通过app.run_server()运行。

其次这个应用的布局(layout)由html组件(html.Div等)和图形组件(dcc.Graph等)构成。其中基础的html标签来自于dash_html_components,而更加React.js库里的高级组件则是由dash_core_components提供。

最后的展示形式需要后期慢慢的调整, 比如说调整一下字体对齐, 字体颜色和背景颜色等

import dash_core_components as dcc
import dash_html_components as htmlapp = dash.Dash()colors = {'background':'#111111','text':'#7FDBFF'
}app.layout = html.Div(style={'backgroundColor':colors['background']},children=[html.H1(children = 'Hello Dash',style = {'textAlign':'center','color': colors['text']}),html.Div(children = '''Dash: A web application frameworkd for Python.''', style = {'textAlign':'center','color': colors['text']}),dcc.Graph(id = 'example-graph',figure = {'data':[{'x': [1,2,3], 'y':[4,1,2], 'type':'bar', 'name':'SF'},{'x': [1,2,3], 'y':[2,4,5], 'type':'bar', 'name':'Montreal'},],'layout':{'plot_bgcolor': colors['background'],'paper_bgcolor': colors['background'],'font':{'color': colors['text']},'title':'Dash data Visualization'}})
])if __name__ == '__main__':app.run_server(debug=True, host='0.0.0.0')

这里的html组件都设置了style,用来调整样式,

可视化

dash_core_components库中有一个Graph组件,它利用开源的JavaScript图形库--plotly.js进行交互式数据渲染。Graph里的figure参数等价于plotly.py里的figure参数,即任何plotly.js支持的图形都可以用dash_core_components调用。查看https://plot.ly/python/了解更多plotly.py的图形。

比如说这里可以基于Pandas的数据库创建散点图

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pdapp = dash.Dash()df = pd.read_csv('https://gist.githubusercontent.com/chriddyp/' +'5d1ea79569ed194d432e56108a04d188/raw/' +'a9f9e8076b837d541398e999dcbac2b2826a81f8/'+'gdp-life-exp-2007.csv')plot = [dcc.Graph(id = 'life-exp-vs-GDP',figure = {'data':[go.Scatter(x=df[df['continent'] == i]['gdp per capita'],y=df[df['continent'] == i]['life expectancy'],text=df[df['continent'] == i]['country'],mode='markers',opacity=0.7,marker={'size':15,'line':{'width':0.5, 'color':'white'}},name = i) for i in df.continent.unique()],'layout': go.Layout(xaxis={'type':'log','title':'GDP per Capita'},yaxis={'title':'Life Expectancy'},margin={'l':40,'b':40,'t':10,'r':10},legend={'x':0, 'y':1},hovermode='closest')})]app.layout = html.Div(html.Div(children=[html.Div(className='col-md-4'),html.Div(plot,className='col-md-4')],className='row')
)# Append an externally hosted CSS stylesheet
my_css_url = "https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
app.css.append_css({"external_url": my_css_url
})# Append an externally hosted JS bundle
my_js_url = 'https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js'
app.scripts.append_script({"external_url": my_js_url
})if __name__ == '__main__':app.run_server(debug=True)

这部分代码将图形部分的代码从html组件中抽离出来,写完之后,再添加到html总体组件中。此外还增加了bootstrap的css样式。

Markdown语法

Dash的dash_html_components支持原生的HTML语句,而dash_core_componentsMarkdown提供了Markdown得渲染。

import dash
import dash_core_components as dcc
import dash_html_components as htmlapp = dash.Dash()markdown_text = '''
### Dash and MarkdownDash apps can be written in Markdown.
Dash uses the [CommonMark](http://commonmark.org/)
specification of Markdown.
Check out their [60 Second Markdown Tutorial](http://commonmark.org/help/)
if this is your first introduction to Markdown!
'''app.layout = html.Div([dcc.Markdown(children=markdown_text)
])if __name__ == '__main__':app.run_server(debug=True)

dash_core_components里不仅仅提供了Markdown, graphs这些图形组件,还支持下拉栏等其他使用工具,可在https://plot.ly/dash/dash-core-components进一步了解

小节

这部分主要是学习了Dash应用得layout. layout是不同组件的层级关系树,最后结果是html页面。html页面的HTML基本语法由dash_html_components提供,而高级的绘图和下拉栏等则是由dash_core_components提供.

参考资料:

  • https://plot.ly/dash/getting-started
  • https://plot.ly/dash/dash-core-components
  • https://plot.ly/dash/dash-html-components

这篇关于使用Dash开发交互式数据可视化网页--页面布局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1088277

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java