Sales in a Dash app
Your Plotly graphs are making a great impact at the company. However, it is difficult to share your work with other colleagues. You are constantly having to export your graphs as images or HTML files, and when there's a new version, you need to send many emails.
It would be better if you could serve up the graphs you create as a web application. You decide to try this out on your line graph of monthly sales by country.
The ecom_sales
dataset is available, and line_fig
has been recreated for you.
As a reminder, in this course, you must include __name__
inside dash.Dash()
for the app to run, but in your own projects, you can leave this out.
This exercise is part of the course
Building Dashboards with Dash and Plotly
Exercise instructions
- Create a blank Dash app object.
- Set up the app layout to be a single graph component.
- Insert the line graph created for you into the Dash app.
- Set the app to run the server in development mode when the script is run from the console.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
import dash
from dash import dcc
import pandas as pd
import plotly.express as px
ecom_sales = pd.read_csv('/usr/local/share/datasets/ecom_sales.csv')
ecom_sales = ecom_sales.groupby(['Year-Month','Country'])['OrderValue'].agg('sum').reset_index(name='Total Sales ($)')
line_fig = px.line(data_frame=ecom_sales, x='Year-Month', y='Total Sales ($)', title='Total Sales by Month', color='Country')
# Create the Dash app
app = dash.____(__name__)
# Set up the layout with a single graph
app.____ = dcc.____(
id='my-line-graph',
# Insert the line graph
figure=____)
# Set the app to run in development mode
if __name__ == '__main__':
app.____(debug=____)