开始使用免费开始使用

添加徽标和说明

一图(或一张图表)胜千言,但有时仪表板仍需要更多说明性文字。幸运的是,您已经练习了在 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)
编辑并运行代码