What data type is the data of a figure in Dash for Python?

30 Views Asked by At

Hello guys I have an csv file from which I want to plot the data using dash. The csv file look like this:

141954,23.750
14200,23.750
14206,23.750
142012,23.750
142018,23.750
142023,23.750

Ive reached that I got both rows in different lists. Now I want to plot this. When I run my code everything works except that no graph is shown.

I found some informations about the data type of the data for a figure. Should it be an array or a dictionary? I am not sure yet.

Ive tried like switching to array or taking it as a list. The list both contain the correct values.

Here is my code :

 from dash import Dash, html, dcc
from os.path import isfile, join
from dash.dependencies import Input, Output
import os


mypath = '/Users/theo/Documents/Privat/Arbeiten/HiWi-Stelle/Modul-Vorbereitung/TCPServer/'

onlyfiles = [f for f in os.listdir(mypath) if isfile(join(mypath, f))]
onlyfiles.remove("TCPServer.py")
onlyfiles.remove(".DS_Store")

formatted_files = []

for file in onlyfiles:
    year = file[0:4]
    month = file[4:6]
    day = file[6:8]
    formatted_files.append(year + "." + month + "." + day)

app = Dash(__name__)

app.layout = html.Div([
    html.Div(children='Temperature Plot'),
    dcc.Dropdown(id='file-dropdown', options=formatted_files, placeholder="Select a file"),
    dcc.Graph(id='graph')
])

@app.callback(
    Output('graph', 'figure'),
    [Input('file-dropdown', 'value')]
)
def update_graph(selected_file):
    row1_list = []
    row2_list = []
    if selected_file:
        file = str(selected_file).replace(".", "") + ".csv"
        file_path = os.path.join(mypath, file)

        with open(file_path, 'r') as f:
            for line in f:
                if line != "\n":
                    split = line.split(",")
                    row1_list.append(split[0])
                    row2_list.append(split[1])

        col1_cleaned = [element.strip() for element in row1_list]
        col2_cleaned = [element.strip() for element in row2_list]

        integer_col1 = [int(x) for x in col1_cleaned]
        float_coli2 = [float(x) for x in col2_cleaned]

        fig = {
            'data': [{'x': integer_col1, 'y': float_coli2, 'type': 'bar'}],
            'layout': {'title': 'Graph of ' + selected_file}
        }

        return fig
    else:

        return {}

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

excuse my code style. I don't using python a lot.

0

There are 0 best solutions below