Skip to content

Commit 5ba8575

Browse files
committed
Adding files for dash demo
1 parent 84427a1 commit 5ba8575

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

code/stacked_bar_app.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
""" Example Dash application accompanying the post
2+
at http://pbpython.com/plotly-dash-intro.html
3+
"""
4+
5+
import dash
6+
import dash_core_components as dcc
7+
import dash_html_components as html
8+
import plotly.graph_objs as go
9+
import pandas as pd
10+
11+
# Read in the Excel file
12+
df = pd.read_excel(
13+
"https://github.com/chris1610/pbpython/blob/master/data/salesfunnel.xlsx?raw=True"
14+
)
15+
16+
# Pivot the data to get it a summary format
17+
pv = pd.pivot_table(
18+
df,
19+
index=['Name'],
20+
columns=["Status"],
21+
values=['Quantity'],
22+
aggfunc=sum,
23+
fill_value=0)
24+
25+
# Build a trace for each status that will eventual make the stacked bar
26+
trace1 = go.Bar(x=pv.index, y=pv[('Quantity', 'declined')], name='Declined')
27+
trace2 = go.Bar(x=pv.index, y=pv[('Quantity', 'pending')], name='Pending')
28+
trace3 = go.Bar(x=pv.index, y=pv[('Quantity', 'presented')], name='Presented')
29+
trace4 = go.Bar(x=pv.index, y=pv[('Quantity', 'won')], name='Won')
30+
31+
# Create the basic app
32+
app = dash.Dash()
33+
34+
# Populate the HTML structure of the app with the graph element
35+
app.layout = html.Div(children=[
36+
html.H1(children='Sales Funnel Report'),
37+
html.Div(children='''National Sales Funnel Report.'''),
38+
dcc.Graph(
39+
id='example-graph',
40+
figure={
41+
'data': [trace1, trace2, trace3, trace4],
42+
'layout':
43+
go.Layout(title='Order Status by Customer', barmode='stack')
44+
})
45+
])
46+
47+
# Allow the app to serve from the command line
48+
if __name__ == '__main__':
49+
app.run_server(debug=True)

code/stacked_bar_interactive.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
""" Example Dash application accompanying the post
2+
at http://pbpython.com/plotly-dash-intro.html
3+
"""
4+
5+
import dash
6+
import dash_core_components as dcc
7+
import dash_html_components as html
8+
import plotly.graph_objs as go
9+
import pandas as pd
10+
11+
# Read in the data from Excel
12+
df = pd.read_excel(
13+
"https://github.com/chris1610/pbpython/blob/master/data/salesfunnel.xlsx?raw=True"
14+
)
15+
16+
# Get a list of all the avilable managers
17+
mgr_options = df["Manager"].unique()
18+
19+
# Create the app
20+
app = dash.Dash()
21+
22+
# Populate the layout with HTML and graph components
23+
app.layout = html.Div([
24+
html.H2("Sales Funnel Report"),
25+
html.Div(
26+
[
27+
dcc.Dropdown(
28+
id="Manager",
29+
options=[{
30+
'label': i,
31+
'value': i
32+
} for i in mgr_options],
33+
value='All Managers'),
34+
],
35+
style={'width': '25%',
36+
'display': 'inline-block'}),
37+
dcc.Graph(id='funnel-graph'),
38+
])
39+
40+
41+
# Add the callbacks to support the interactive componets
42+
@app.callback(
43+
dash.dependencies.Output('funnel-graph', 'figure'),
44+
[dash.dependencies.Input('Manager', 'value')])
45+
def update_graph(Manager):
46+
if Manager == "All Managers":
47+
df_plot = df.copy()
48+
else:
49+
df_plot = df[df['Manager'] == Manager]
50+
51+
pv = pd.pivot_table(
52+
df_plot,
53+
index=['Name'],
54+
columns=["Status"],
55+
values=['Quantity'],
56+
aggfunc=sum,
57+
fill_value=0)
58+
59+
trace1 = go.Bar(x=pv.index, y=pv[('Quantity', 'declined')], name='Declined')
60+
trace2 = go.Bar(x=pv.index, y=pv[('Quantity', 'pending')], name='Pending')
61+
trace3 = go.Bar(x=pv.index, y=pv[('Quantity', 'presented')], name='Presented')
62+
trace4 = go.Bar(x=pv.index, y=pv[('Quantity', 'won')], name='Won')
63+
64+
return {
65+
'data': [trace1, trace2, trace3, trace4],
66+
'layout':
67+
go.Layout(
68+
title='Customer Order Status for {}'.format(Manager),
69+
barmode='stack')
70+
}
71+
72+
73+
if __name__ == '__main__':
74+
app.run_server(debug=True)

0 commit comments

Comments
 (0)