Black Lives Matter. Please consider donating to Black Girls Code today.

How To Use Python / Pandas codes through Dash HTML?

Hi Guys,

is possible to execute pandas and python code trhgout the HTML in Dash?

I have the application below:

import base64
import datetime
import io

import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table

import pandas as pd

import dash_table_experiments
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objects as go
import numpy as np
import dash_table_experiments as dash_table
import dash
from dash.dependencies import Input, Output, State
import dash_table
import pandas as pd
import plotly_express as px
import plotly.offline as pyo

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '100%',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        },
        # Allow multiple files to be uploaded
        multiple=True
    ),
    html.Div(id='output-data-upload'),
])


def parse_contents(contents, filename, date):
    content_type, content_string = contents.split(',')

    decoded = base64.b64decode(content_string)
    try:
        if 'csv' in filename:
            # Assume that the user uploaded a CSV file
            df3 = pd.read_csv(
                io.StringIO(decoded.decode('utf-8')))
        elif 'xls' in filename:
            # Assume that the user uploaded an excel file
            df3 = pd.read_excel(io.BytesIO(decoded))
    except Exception as e:
        print(e)
        return html.Div([
            'There was an error processing this file.'
        ])

    return html.Div([
        html.H5(filename),
        html.H6(datetime.datetime.fromtimestamp(date)),

        dash_table.DataTable(
            data=df3.to_dict('records'),
            columns=[{'name': i, 'id': i} for i in df3.columns]
        ),

        html.Hr(),  # horizontal line

        # For debugging, display the raw contents provided by the web browser
        html.Div('Raw Content'),
        html.Pre(contents[0:200] + '...', style={
            'whiteSpace': 'pre-wrap',
            'wordBreak': 'break-all'
        }),
    
html.Div([ 


dcc.Graph(
        id = 'GrapGo',
        figure = fig)
    
    
])    
    
    ])




@app.callback(Output('output-data-upload', 'children'),
              [Input('upload-data', 'contents')],
              [State('upload-data', 'filename'),
               State('upload-data', 'last_modified')])
def update_output(list_of_contents, list_of_names, list_of_dates):
    if list_of_contents is not None:
        children = [
            parse_contents(c, n, d) for c, n, d in
            zip(list_of_contents, list_of_names, list_of_dates)]
        return children



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

However after Upload Components receive the CSV file in df 3, i need to make the manipulations below to build my figure.

fdr = df3.groupby(['Time', 'Machine']).sum()

fdrf = fdr.drop(['Units Pruduced'], axis=1)
fdrf = fdrf.drop_duplicates()
fdrf

fdr4= df3.groupby(['Machine', 'Time']).sum()
fdr4.reset_index(inplace=True)

fig = px.scatter(fdr4, x="Time", y="Units Pruduced", color="Machine", template="plotly_white")
fig.update_traces(mode='lines+markers')

How can i proceed to make these manipulations after Upload Components get my csv?