它也用作业余爱好者的备忘录。请原谅。
上次https://qiita.com/Naoya_Study/items/507cf063a9300e9e41f6
今天填写的表格在这里↓↓
1.安装Dash Bootstrap组件
有一个库可让您在Python框架Dash中使用Bootstrap。
这次,我将使用它来制作非常简单的2x2布局。
使用的库是Dash Bootstrap组件。
https://dash-bootstrap-components.opensource.faculty.ai/
安装它。
1 | pip install dash-bootstrap-components |
基本模板如下所示。
通过向dbc.Container中添加元素来创建布局。
1 2 3 4 5 6 7 8 9 | import dash import dash_bootstrap_components as dbc app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP]) app.layout = dbc.Container("""ここに色々な要素を足していく""") if __name__ == "__main__": app.run_server(debug=True) |
2.做一个简单的布局
现在让我们实际编写一个2x2布局。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import dash import dash_bootstrap_components as dbc import dash_html_components as html app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP]) app.layout = dbc.Container( [ dbc.Row( [ dbc.Col( html.H1("ここにタイトル!"), style={"background-color": "pink"} ) ], className="h-30" ), dbc.Row( [ dbc.Col( html.P("世界地図!"), width=7, style={"height": "100%", "background-color": "red"}, ), dbc.Col( html.P("国別感染者数のリスト!"), width=5, style={"height": "100%", "background-color": "green"}, ), ], className="h-50" ), dbc.Row( [ dbc.Col( html.P("日本地図!"), width=7, style={"height": "100%", "background-color": "blue"}, ), dbc.Col( html.P("国内感染者数推移の棒グラフ!"), width=5, style={"height": "100%", "background-color": "cyan"}, ), ], className="h-50", ), ], style={"height": "90vh"}, ) if __name__ == "__main__": app.run_server(debug=True) |
1 | className="h-50" |
此50表示高度的50%。
1 | style={"height": "90vh"} |
vh代表"视口高度",表示屏幕高度的90%(可能)
3.插入图形
根据此布局插入先前创建的图形。
基本上只需将html.P()替换为创建的dcc.Graph()。
1 2 3 4 | #書き換える html.P() ↓ dcc.Graph() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | import dash import dash_bootstrap_components as dbc import dash_html_components as html import requests, io, re import pandas as pd import numpy as np import plotly.express as px import plotly.graph_objects as go from datetime import datetime as dt import dash_table import dash_core_components as dcc from corona_def import Get_Df, Df_Merge, Df_Count_by_Date, Df_Globe_Merge app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP]) app.layout = dbc.Container( [ dbc.Row( [ dbc.Col( html.H1("Novel Coronavirus (COVID-19) Situation"), style = { "size": "30px", "background-color": "#1b1935", "color": "white", "textAlign": "center", } ) ], className = "h-30" ), dbc.Row( [ dbc.Col( html.H4("世界の感染者数"), width = 7, style = { "height": "100%", "background-color": "white", "textAlign": "left", "padding": "10px" }, ), dbc.Col( html.H4(""), width = 5, style = { "height": "100%", "background-color": "white", "textAlign": "center" }, ), ], className = "h-8" ), dbc.Row( [ dbc.Col( dcc.Graph( id = 'WorldMap', figure = { 'data' : [ go.Choropleth( locations = df_globe_merge['code'], z = df_globe_merge['Confirmed'], text = df_globe_merge['COUNTRY'], colorscale = 'Plasma', marker_line_color ='darkgray', marker_line_width = 0.5, colorbar_title = '感染者数', ) ], 'layout' : go.Layout( template = "ggplot2", width = 600, height = 270, title = { 'font': {'size': 25}, 'y': 0.9, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top' }, margin={'b': 5, 'l': 5, 'r': 5, 't': 5}, geo={"projection_type": 'equirectangular'} ) } ), width = 7, style = { "height": "100%", "background-color": "white", "border-color": "white", "border-width":"10px" }, ), dbc.Col( dash_table.DataTable( columns = [{"name": i, "id": i} for i in df_globe.columns], data = df_globe.to_dict('records'), style_header = {'backgroundColor': '#eae6e8'}, style_cell = { 'backgroundColor': '#fbf9f7', 'color': 'black', 'font-size': '15px', 'textAlign': 'center' }, style_table = { 'maxHeight': '300px', 'overflowY': 'scroll', 'overflowX': 'hidden', }, ), width = 5, style = {"height": "100%"} ), ], className = "h-50" ), dbc.Row( [ dbc.Col( html.H4("国内感染者分布"), width = 7, style = { "height": "100%", "background-color": "white", "textAlign": "left", "padding":"10px" }, ), dbc.Col( html.H4("国内累計感染者数"), width = 5, style = { "height": "100%", "background-color": "white", "textAlign": "left", "padding":"13px" }, ), ], className = "h-30" ), dbc.Row( [ dbc.Col( dcc.Graph( id = 'JapMap', figure = { 'data' : [ go.Scattergeo( lat = df_merge["緯度"], lon = df_merge["経度"], mode = 'markers', marker = { "color": 'red', "size": df_merge['China']/5+6, "opacity": 0.8, "reversescale": True, "autocolorscale": False }, hovertext = df_merge['text'], hoverinfo = "text" ) ], 'layout' : go.Layout( width = 600, height = 270, template = "ggplot2", title = { 'font': {'size':25}, 'y': 0.9, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'}, margin = {'b': 1, 'l': 1, 'r': 1, 't': 1}, geo = dict( resolution = 50, landcolor = 'rgb(204, 204, 204)', coastlinewidth = 1, lataxis = dict(range = [28, 47]), lonaxis = dict(range = [125, 150]), ) ) } ), width = 7, style = {"height": "100%",}, ), dbc.Col( dcc.Graph( id = '都道府県別感染者数', figure = { 'data' : [ go.Bar( name='前日までの累積数', x=df_count_by_date.index, y=df_count_by_date['gap'], ), go.Bar( name='新規数', x=df_count_by_date.index, y=df_count_by_date['China'] ) ], 'layout' : go.Layout( width = 450, height = 270, legend = { 'x': 0.05, 'y': 0.9, 'bordercolor': 'black', 'borderwidth': 1 }, barmode = 'stack', template = "ggplot2", margin = {'b': 5, 'l': 5, 'r': 5, 't': 5}, xaxis_title = None, yaxis_title = "感染者数" ) } ), width = 5, style = {"height": "100%"}, ), ], className = "h-50", ), ], style = {"height": "90vh"}, ) if __name__ == "__main__": app.run_server(debug=True) |
目前,我能够按绘制的方式插入图形。
该图还可以交互移动,非常有趣。
但是,外观仍未完成,因此我们将从下一次开始改善视觉效果。