{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(dash:layout)=\n",
"# Dash 布局\n",
"\n",
"```{topic} 导航\n",
"本节将通过一些应用程序带领你了解 Dash 应用程序的基础方面:应用程序布局。\n",
"```\n",
"\n",
"Dash 为应用程序的所有可视组件提供了 Python 类。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import plotly.express as px\n",
"from dash import dcc, html\n",
"from dash import Dash"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"app = Dash(__name__)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dash 小例子"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"创建 plotly 图:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# 假设您有一个 \"long-form\" 数据帧\n",
"# 更多选项请参见 https://plotly.com/python/px-arguments/\n",
"df = pd.DataFrame({\n",
" \"Fruit\": [\"Apples\", \"Oranges\", \"Bananas\", \"Apples\", \"Oranges\", \"Bananas\"],\n",
" \"Amount\": [4, 1, 2, 2, 4, 5],\n",
" \"City\": [\"SF\", \"SF\", \"SF\", \"Montreal\", \"Montreal\", \"Montreal\"]\n",
"})\n",
"\n",
"fig = px.bar(df, x=\"Fruit\", y=\"Amount\", color=\"City\", barmode=\"group\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"设置 Dash 布局:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"app.layout = dcc.Graph(\n",
" id='example-graph',\n",
" figure=fig\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"if __name__ == '__main__':\n",
" app.run_server(debug=True)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [
{
"data": {
"application/vnd.plotly.v1+json": {
"config": {
"plotlyServerURL": "https://plot.ly"
},
"data": [
{
"alignmentgroup": "True",
"hovertemplate": "City=SF
Fruit=%{x}
Amount=%{y}
Fruit=%{x}
Amount=%{y}