网站首页 > 技术文章 正文
Dash 是一款构建 Web 应用的 Python 低代码框架,建立在 Plotly.js、React 和 Flask 之上,将现代 UI 元素如下拉框、滑块和图形直接与 Python 代码绑定,使得熟悉python的朋友可以快速打造出演示程序。
- 安装
pip install dash pip install pandas |
- 初试
import pandas as pd
import plotly.express as px
from dash import Dash, html, dcc
app = Dash()
df = pd.DataFrame({'x': [1, 2, 3], 'SF': [4, 1, 2], 'Montreal': [2, 4, 5]}) # x是坐标轴刻度;
fig = px.bar(df, x='x', y=['SF', 'Montreal'], barmode='group') # 柱状图
app.layout = html.Div(children=[
html.H1(children='This is Dash Demo'),
html.Div(children='Dash: 一款Python web应用框架'),
dcc.Graph(id='dash-example-graph', figure=fig)
])
if __name__ == '__main__':
app.run_server(debug=True)
- 结果
程序运行后,会有如下输出结果:
Dash is running on http://127.0.0.1:8050/ * Serving Flask app 'test' * Debug mode: on |
按照提示访问 : http://127.0.0.1:8050/
可以得到如下结果:

- 说明
layout(布局) 就像 HTML 一样,由一棵组件树构成。如代码中的 html.H1、html.Div、dcc.Graph。
dash.html 包含所有 HTML 标签,如 html.H1(children='Hello Dash') 实际上生成的 HTML 代码为 <h1>Hello Dash</h1>。
不是所有组件都是纯 HTML,模块 dash.dcc 包含交互的高级组件,底层通过 React.js、JavaScript、HTML、CSS实现。
每个组件通过关键字参数进行描述,但属性 children 比较特殊,一般总是作为第一个属性,因此不用关键字参数来描述。
app.run_server(debug=True) 可实现热加载,即修改代码后会自动刷新浏览器。
猜你喜欢
- 2024-12-23 如何在你的项目中混合 Rust 和 Python
- 2024-12-23 一秒开挂!纯 Python 开发 Web 应用
- 2024-12-23 用 Python 与 Windows 交互 - Pywin32库
- 2024-12-23 交互式环境(Python Shell)编写Python代码
- 2024-12-23 这个用Python编写的大数据测试工具,我给100分
- 2024-12-23 Python与Mysql交互库(持续更新)
- 2024-12-23 Python解释器和交互模式
- 2024-12-23 有用的 Python 提示和技巧 — #5
- 2024-12-23 Python 语言如何和 C/C++ 语言交互使用
- 2024-12-23 简单学Python——做一个可交互的图(结合ipywidgets库)
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)