Get startedGet started for free

A draft sales dashboard

Given your great work so far with the e-commerce company, they have asked you to help them completely replace their existing dashboards, which require proprietary software and are costing the organization a mountain of money each month.

This won't be the final product; they want a rough draft of what is possible using Dash. They would like a line chart of their total sales each month and a bar chart of their total sales in each country. It would be good to throw in a high-level summary statistic, such as which month had the largest order volume.

Your task is to create a quick dashboard using Plotly plots stacked on each other.

This exercise is part of the course

Building Dashboards with Dash and Plotly

View Course

Exercise instructions

  • Set up a Dash layout on line 14.
  • Add the line_fig variable on line 17.
  • Add the bar_fig variable on line 19.
  • Add an .H3() title showing the max_country (the country with the highest total sales).

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

from dash import Dash, dcc, html
import pandas as pd
import plotly.express as px
ecom_sales = pd.read_csv('/usr/local/share/datasets/ecom_sales.csv')
ecom_line = ecom_sales.groupby('Year-Month')['OrderValue'].agg('sum').reset_index(name='Total Sales ($)')
line_fig = px.line(data_frame=ecom_line, x='Year-Month', y='Total Sales ($)', title='Total Sales by Month')
ecom_bar = ecom_sales.groupby('Country')['OrderValue'].agg('sum').reset_index(name='Total Sales ($)')
max_country = ecom_bar.sort_values(by='Total Sales ($)', ascending=False).loc[0]['Country']
bar_fig = px.bar(data_frame=ecom_bar, x='Total Sales ($)', y='Country', orientation='h', title='Total Sales by Country')

app = Dash()

# Set up the layout
app.____ = [
    html.H1('Sales Figures'), 
    # Add the line figure
    dcc.___(id='my-line-fig', figure=____), 
    # Add the bar figure
    dcc.___(id='my-bar-fig', figure=____), 
    # Add the H3 title
    html.____(f'The largest country by sales was {____}!')
    ]

if __name__ == '__main__':
    app.run(debug=True)
Edit and Run Code