1.building Dashboards With Dash and Plotly
1.building Dashboards With Dash and Plotly
figures
B U I L D I N G D A S H B O A R D S W I T H D A S H A N D P L O T LY
Alex Scriven
Data Scientist
What is Dash?
A Python library for creating interactive, modern, functional web applications easily.
Advantages:
Details:
Item category (Major, Minor) + description
Country
Year-Month of sale
import plotly.express as px
line_graph = px.line(
data_frame=ecom_sales,
x='Year-Month',
y='Total Sales ($)',
title='Total Sales by Month')
line_graph.show()
bar_fig = px.bar(
data_frame=ecom_sales,
x='Total Sales ($)',
y='Country',
title='Total Sales by Country',
orientation='h')
bar_fig.show()
Plotly graph properties can be updated later Notice the larger gaps between bars?
with update_layout() (important for Dash
apps!).
bar_fig.update_layout({'bargap': 0.5})
bar_fig.show()
Alex Scriven
Data Scientist
A first Dash App
import dash
from dash import dcc
app = dash.Dash()
app.layout = dcc.Graph(id='example-graph', figure=bar_fig)
if __name__ == '__main__':
app.run_server(debug=True)
import dash
from dash import dcc
dcc ('dash core components') contains the different building blocks to create the app
Two components in our app
app = dash.Dash()
Create an app object using dash.Dash()
if __name__ == '__main__':
Lastly, running the server
Script is run via the command-line ( python3 script.py ), served on a local server
While served, update and save .py file to see live updates in browser
Alex Scriven
Data Scientist
HTML and the web
Dash uses dash html components ( dash.html ) to interface between HTML and Python.
import dash
from dash import dcc, html
app = dash.Dash()
app.layout = html.Div(children=[
html.Div(style={'height':250, 'width':250, 'background-color':'red'}),
html.Div(children=[
html.H1("This box"),
html.H2("Another Title")],
style={'background-color':'lightblue'})
])
bar_fig_country = px.bar(ecom_sales,
x='Total Sales ($)', y='Country',
color='Country', color_discrete_map=
{'United Kingdom':'lightblue',
'Germany':'orange', 'France':'darkblue',
'Australia':'green', 'Hong Kong':'red'})
app = dash.Dash()
app.layout = html.Div(
children=[
html.H1("Sales Proportion by Country"),
dcc.Graph(id='bar_graph',
figure=bar_fig_country)])
app.layout = html.Div(
children=[
html.Div( style={
'width':150,'height':150,
'background-color':'lightblue'}),
html.H1("Sales Proportion by Country"),
dcc.Graph(id='bar_graph',
figure=bar_fig_country)])