始める無料で始める

ロゴと注記を追加する

百聞は一見にしかず(グラフも同じ!)ですが、ダッシュボードには説明文が必要なこともあります。幸い、皆さんはテキストの追加と整形、画像の挿入、さらにDashアプリの構造化のスキルを磨いてきました。

ここでは、会社のロゴ、1つのグラフ、そして簡単な説明メモを含むダッシュボードを作成します。

この演習はコースの一部です

Dash と Plotly で作るダッシュボード

コースを見る

演習の手順

  • 13の下に、logo_link のURLを使って会社のロゴを追加します。
  • 20の既存のテキストの後に、変数 top_country太字で追加します。
  • 24の下に、会社の著作権表記 ' Copyright E-Com INC'斜体で追加します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

from dash import Dash, dcc, html
import plotly.express as px
import pandas as pd
ecom_sales = pd.read_csv('/usr/local/share/datasets/ecom_sales.csv')
logo_link = 'https://assets.datacamp.com/production/repositories/5893/datasets/2bac9433b0e904735feefa26ca913fba187c0d55/e_com_logo.png'
ecom_bar = ecom_sales.groupby('Country')['OrderValue'].agg('sum').reset_index(name='Total Sales ($)').sort_values(by='Total Sales ($)', ascending=False)
top_country = ecom_bar.loc[0]['Country']            
bar_fig_country = px.bar(ecom_bar, x='Total Sales ($)', y='Country', color='Country', color_discrete_map={'United Kingdom':'lightblue', 'Germany':'orange', 'France':'darkblue', 'Australia':'green', 'Hong Kong':'red'})         
    
app = Dash()

app.layout = [
  # Add the company logo
  html.____(src=____),
  html.H1('Sales by Country'),
  dcc.Graph(
    figure=bar_fig_country,
    style={'width':'750px', 'margin':'auto'}),
  html.Span(children=[
    # Add the top country text
    'This year, the most sales came from: ', 
    html.____(____),
    html.Br(),
    # Italicize copyright notice
    html.____(' Copyright E-Com INC')])
    ]

if __name__ == '__main__':
    app.run(debug=True)
コードを編集して実行