分析頂尖顧客的所在地
基於你先前在這家電商公司的優異表現,他們請你協助擴充一份現有的各國銷售報表。
銷售經理想知道頂尖顧客主要分布在哪裡。不過,他們目前對「頂尖」的定義還不確定。理想情況下,他們能輕鬆嘗試不同的門檻值來判定誰是頂尖顧客,並觀察圖表如何隨不同門檻而改變。
你的任務是使用範圍輸入,建立一個即時更新的應用,依據最低 OrderValue 金額來篩選銷售資料,協助銷售經理進行分析。
本練習屬於課程
使用 Dash 與 Plotly 建立儀表板
練習說明
- 在第 34 行下方加入一個名為
dcc.Input、識別碼為min_order_val的範圍輸入元件,讓使用者可選擇介於50到550的值。 - 在第 53 行下方的回呼函式中,檢查
input_val,並以選定的值過濾salesDataFrame,僅保留OrderValue大於該值的紀錄。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
from dash import Dash, dcc, html, Input, Output, callback
import plotly.express as px
import pandas as pd
from datetime import datetime, date
ecom_sales = pd.read_csv('/usr/local/share/datasets/ecom_sales.csv')
logo_link = 'https://assets.datacamp.com/production/repositories/5893/datasets/fdbe0accd2581a0c505dab4b29ebb66cf72a1803/e-comlogo.png'
def make_break(num_breaks):
br_list = [html.Br()] * num_breaks
return br_list
def add_logo():
corp_logo = html.Img(
src=logo_link,
style={'width':'75px','margin':'20px 20px 5px 5px','border':'1px dashed lightblue','display':'inline-block'})
return corp_logo
def style_c():
layout_style={'display':'inline-block','margin':'0 auto','padding':'20px'}
return layout_style
app = Dash()
app.layout = [
add_logo(),
*make_break(2),
html.H1('Sales Dashboard'),
*make_break(3),
html.Div([
html.H2('Controls', style=style_c()),
html.H3('Set minimum OrderValue'),
*make_break(2),
dcc.Input(
# Add a range input
id='min_order_val', type='____',
____=____, ____=____, value=50,
debounce=False,
style={'width':'300px', 'height':'30px'})],
style={'width':'350px', 'height':'350px', 'vertical-align':'top', 'border':'1px solid black',
'display':'inline-block', 'margin':'0px 80px'}),
html.Div([
dcc.Graph(id='sales_country'),
html.H2('Sales Quantity by Country', style={ 'border':'2px solid black', 'width':'400px', 'margin':'0 auto'})],
style={'width':'500px','display':'inline-block'})
]
@callback(
Output(component_id='sales_country', component_property='figure'),
Input(component_id='min_order_val', component_property='value'))
def update_plot(input_val):
sales = ecom_sales.copy(deep=True)
# Check for input and filter sales
if ____:
input_val = round(float(input_val), 2)
sales = sales[sales['OrderValue'] > ____]
fig = px.scatter(data_frame=sales, y='OrderValue', height=400,
x='Quantity', color='Country',
title=f'Orders of Min Value ${input_val}')
return fig
if __name__ == '__main__':
app.run(debug=True)