I need help with the last function below(‘connect_dropdown(contents, filename, value)’) where ‘value’ is value from dropdown
import base64
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas as pd
from io import StringIO
import io
import plotly.graph_objs as go
from plotly.offline import iplot, init_notebook_mode
def parse_contents(contents, filename):
content_type, content_string = contents.split(’,’)
decoded = base64.b64decode(content_string)
try:
if ‘csv’ in filename:
df = pd.read_csv(io.StringIO(decoded.decode(‘utf-8’)))
elif ‘xlsx’ in filename:
df = pd.read_excel(io.BytesIO(decoded))
except Exception as e:
print(e)
return None
return df
def create_dropdown_list(df):
metric_name_list=[col for col in df.columns if ‘_name’ in col]
metric_level_list=[w.replace(’_name’, ‘’) for w in metric_name_list]
dropdown_list=[{‘label’: key, ‘value’: value} for (key,value) in zip(metric_level_list,metric_name_list)]
return dropdown_list
app = dash.Dash()
app.layout = html.Div([
html.H1([‘DASHBOARD’]),
html.Div([
dcc.Upload(id='upload_data',
multiple=False,
style={'width': 105, 'display': 'inline-block', 'margin-left': 7},
children=html.Button('Select File'),
),
],style={'width': '20%', 'display': 'inline-block'}),
html.Div([
dcc.Dropdown(id='dropdown')
]),
html.Div([
dcc.Graph(id='number-out')
]),
html.Div(id='dimension-dropdown')
])
#app.config.supress_callback_exceptions = True
@app.callback(Output(‘dropdown’, ‘options’),
[Input(‘upload_data’, ‘contents’)],
[State(‘upload_data’, ‘filename’)])
def dynamic_control_maker(contents, filename):
df = parse_contents(contents, filename)
options =[{‘label’: j.capitalize(), ‘value’: j} for j in df.columns]
return options
@app.callback(Output(‘number-out’, ‘figure’),
[Input(‘upload_data’, ‘contents’)],
[State(‘dropdown’, ‘value’),
State(‘upload_data’, ‘filename’)])
def connect_dropdown(contents, filename, value):
#if not contents or not filename:
#return
df = parse_contents(contents, filename)
print(dynamic_control_maker(contents, filename))
wags = pd.DataFrame(df[‘Item’].value_counts()).reset_index()
wag1 = wags[:4]
return {‘data’:[go.Bar(
x=wag1.iloc[:,0],
y=wag1.iloc[:,1]
)]}
if name == ‘main’:
app.run_server()