|
| 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