Analyzing top customer locations
Based on your good work with the e-commerce company, you have been asked to help them expand an existing report on their sales by country.
The sales manager is interested in understanding where their top customers are located. They don't currently have a clear idea of what 'top' means. Ideally, they can easily try various cut-off values to consider someone a top customer and see how the graph changes with different cut-off values.
Your task is to help the sales manager by using a range input to facilitate a live-update app that filters sales by a minimum OrderValue
amount.
This exercise is part of the course
Building Dashboards with Dash and Plotly
Exercise instructions
- Add a range input component called
dcc.Input
with identifiermin_order_val
below line34
that lets users select a value from50
to550
. - In the callback below line
53
, check forinput_val
and filter thesales
DataFrame withOrderValue
greater than the selected value.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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)