Using dash in python to upload files and plot a bar chart

I try to make an interactive dashboard using dash from plotly. I am a beginner in it therefore I used an example to do it. The code should upload different data files from a certain folder and plot a histogram based on a certain column. The name of each file looks like “30092017ARB.csv” (date + ARB.csv). The code loops over all file names in the data-folder and print the name of files in a drop-down bottom. After selecting the name of file it should be uploaded and plot a histogram. I wrote the following code:

import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
#
from os import listdir
from os.path import isfile, join
import numpy as np
#

mypath='/Users/Python/BeN_/data/'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]



app = dash.Dash()

app.layout = html.Div([
    html.H2("Ausfallsreport"),
    html.Div(
        [
            dcc.Dropdown(
                id="dataFH",
                options=[{
                    'label': i,
                    'value': i
                } for i in onlyfiles],
                value=" "),
        ],
        style={'width': '25%',
               'display': 'inline-block'}),
    dcc.Graph(id='Mygraph'),
])
@app.callback(
dash.dependencies.Output('Mygraph', 'figure'),
[dash.dependencies.Input('dataFH', 'value')])
def update_graph(dataFH):
    df = pd.read_csv(mypath+dataFH, delimiter=';',encoding='cp1252') 
    # aggregate 
    dfagg = df.groupby('Bestand', as_index=False).agg({'MW': 'sum'})
    # make strings for x-axis 
    x=["BE-"+s for s in[str(s) for s in [int(x) for x in dfagg.iloc[:,0].tolist()]]]
    y=dfagg.ix[:,1].tolist()

    trace = go.Bar(x=x, y=y, name='Declined')

    return {
        'data': [trace],
        'layout':
        go.Layout(
            title='Customer Order Status for {}'.format(dataFH),
            barmode='bar',
            yaxis=dict(tickformat=".2%"))
    }

  
if __name__ == '__main__':
    app.run_server(debug=True)

I get the following ( image)

would you please help me to figure out why I do not get bar charts?
thanks

1 Like

I am having similar issues, so if anyone has resolved this, please share.