売上ダッシュボードの下書き
これまでのあなたの活躍を受けて、eコマース企業は既存のダッシュボードを完全に置き換える手助けを依頼してきました。現状は専用ソフトが必要で、毎月かなりのコストがかかっているためです。
今回は最終版ではなく、Dash でどこまでできるかのラフな下書きが欲しいとのことです。毎月の総売上の折れ線グラフと、国別の総売上の棒グラフを用意してください。加えて、どの月の注文数が最大だったかといった、高レベルのサマリー統計も含められると良いでしょう。
あなたのタスクは、Plotly のプロットを上下に積み重ねた簡易ダッシュボードを作成することです。
この演習はコースの一部です
Dash と Plotly で作るダッシュボード
演習の手順
- 14 行目で Dash のレイアウトを設定します。
- 17 行目に変数
line_figを追加します。 - 19 行目に変数
bar_figを追加します。 - 合計売上が最大の国
max_countryを表示する.H3()タイトルを追加します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
from dash import Dash, dcc, html
import pandas as pd
import plotly.express as px
ecom_sales = pd.read_csv('/usr/local/share/datasets/ecom_sales.csv')
ecom_line = ecom_sales.groupby('Year-Month')['OrderValue'].agg('sum').reset_index(name='Total Sales ($)')
line_fig = px.line(data_frame=ecom_line, x='Year-Month', y='Total Sales ($)', title='Total Sales by Month')
ecom_bar = ecom_sales.groupby('Country')['OrderValue'].agg('sum').reset_index(name='Total Sales ($)')
max_country = ecom_bar.sort_values(by='Total Sales ($)', ascending=False).loc[0]['Country']
bar_fig = px.bar(data_frame=ecom_bar, x='Total Sales ($)', y='Country', orientation='h', title='Total Sales by Country')
app = Dash()
# Set up the layout
app.____ = [
html.H1('Sales Figures'),
# Add the line figure
dcc.___(id='my-line-fig', figure=____),
# Add the bar figure
dcc.___(id='my-bar-fig', figure=____),
# Add the H3 title
html.____(f'The largest country by sales was {____}!')
]
if __name__ == '__main__':
app.run(debug=True)