開始使用免費開始

加入商標與註記

雖然一張圖片(或圖表)勝過千言萬語,但有時儀表板仍需要更多說明文字。幸好,你已經練習如何在 Dash 應用中加入並格式化文字、圖片,以及更進一步的版面結構。

我們來建立一個同時包含公司商標、單一圖表,以及簡短說明註記的儀表板。

本練習屬於課程

使用 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)
編輯並執行程式碼