Here is my code:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import mysql.connector
import pandas as pd
import plotly.graph_objs as px
import matplotlib.pyplot as plt
#import plotly .express as px
Step 1. Launch the application
app = dash.Dash()
Step 2. Import the dataset
df = pd.read_excel("./billingdata.xlsx")
Step 3. Create a plotly figure
trace_1 = px.Scatter(x = df.OVERAGE_ACCOUNTS_PER_CYCLE, y = df[‘CORP_ID’],
name = ‘CORP_ID’,#marker=dict(color=’#ffcdd2’))
line = dict(width = 1,
color = ‘rgb(229, 151, 50)’))
layout = px.Layout(title = ‘Billing cycle’,
hovermode = ‘closest’)
fig = px.Figure(data = [trace_1], layout = layout)
Step 4. Create a Dash layout
df[‘BILLING_CYCLE_START_DATE’] = pd.to_datetime(df.BILLING_CYCLE_START_DATE)
features = df.BILLING_CYCLE_START_DATE
opts = [{‘label’ : i, ‘value’ : i} for i in features]
app.layout = html.Div([
# adding a plot
dcc.Graph(id = ‘plot’, figure = fig),
# dropdown
html.P([
html.Label("BILLING_CYCLE_START_DATE"),
dcc.Dropdown(
id = 'opt',
options = opts,
value = features[0],
multi = False)
], style = {'width': '400px',
'fontSize' : '20px',
'padding-left' : '100px',
'display': 'inline-block'})
])
Step 5. Add callback functions
@app.callback(dash.dependencies.Output(‘plot’, ‘figure’),
[dash.dependencies.Input(‘opt’, ‘value’)]
)
def update_figure(X):
#dff = df[df.BILLING_CYCLE_START_DATE==X]
trace_1 = px.Scatter(x = df.OVERAGE_ACCOUNTS_PER_CYCLE, y = df['CORP_ID'],
name = 'CORP_ID',#marker=dict(color='#ffcdd2'))
line = dict(width = 1,
color = 'rgb(229, 151, 50)'))
trace_2 = px.Scatter(x = df.OVERAGE_ACCOUNTS_PER_CYCLE, y = df[X],
name = str(X),
line = dict(width = 2,
color = 'rgb(106, 181, 135)'))
fig = px.Figure(data =([trace_1, trace_2]), layout = layout)
return fig.update_traces(mode='markers+lines')
Step 6. Add the server clause
if name == ‘main’:
app.run_server(debug = True)