Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

How to access an uploaded dataset for analysis

Thank you very much for Dash! I am working on my first app, which is supposed to be a dashboard where my users can upload files, the file would be cleaned using python functions and graphs plotted. I dont know how to access the uploaded files in a dataframe. My current solution involve me hard-coding the dataframe in my script, this wouldn’t be suitable for deployment.

I have gone through the Upload component tutorial which cover returning texts and tables, I would like to access the actual dataframe and clean it with functions instead.

How can I go about this? I know its trivial, but I just cant find the solution.

import dash_core_components as dcc
import dash_html_components as html
import dash
from dash.dependencies import Input, Output
#from pandas_datareader import data as web
#from datetime import datetime as dt

from dataclean import *

def sharepoint(filename):


    import pandas as pd
    data = pd.read_excel(str(filename), sheet_name=2)
    rows_to_drop = ['Current SharePoint 2010 sites'
    ,'     Not migrating to 365 (locked)',
    '     Migration to 365 complete (locked)',
                '     Stale (may not need to be migrated)',
       'Total Active Site collections (2010 & O365 sites)',
                 'Sites Created by Group creation process',
                      'O365 sites - shell - informational']
    data.drop(labels = rows_to_drop, inplace=True)
    data.dropna(inplace = True)
    data=data.transpose()

    return data



# temporary solution cos I dont know how to access files uploaded
SP = sharepointclean('SPMigrationsandy.xlsx')
#print(SP)

app = dash.Dash()

app.layout = html.Div(children = [html.Div(children = html.H1(children = 'Office 365 Adoption Dashboard', style = {'textAlign': 'center'})) ,
                                dcc.Markdown('Please upload the files in the respective boxes'),
                                dcc.Upload(  id='upload-od4b',children=html.Div(['Drag and Drop Onedrive file or ',   html.A('Select Files')]),
                                            style={'width': '40%','height': '30px','lineHeight': '30px','borderWidth': '1px',
                                            'borderStyle': 'dashed','borderRadius': '5px','textAlign': 'center','margin': '10px'},
                                            multiple=False),
                                dcc.Upload(  id='upload-sccm',children=html.Div(['Drag and Drop SCCM file or ',   html.A('Select Files')]),
                                            style={'width': '40%','height': '30px','lineHeight': '30px','borderWidth': '1px',
                                            'borderStyle': 'dashed','borderRadius': '5px','textAlign': 'center','margin': '10px'},
                                            multiple=False),
                                dcc.Upload(  id='upload-sharepoint',children=html.Div(['Drag and Drop Sharepoint file or ',   html.A('Select Files')]),
                                            style={'width': '40%','height': '30px','lineHeight': '30px','borderWidth': '1px',
                                            'borderStyle': 'dashed','borderRadius': '5px','textAlign': 'center','margin': '10px'},
                                            multiple=False),
                                dcc.Upload(  id='upload-exchange',children=html.Div(['Drag and Drop Exchange file or ',   html.A('Select Files')]),
                                            style={'width': '40%','height': '30px','lineHeight': '30px','borderWidth': '1px',
                                            'borderStyle': 'dashed','borderRadius': '5px','textAlign': 'center','margin': '10px'},
                                            multiple=False),
                                dcc.Dropdown(id='my-dropdown',options = [{'label': 'Onedrive for Business', 'value': 'OD4B'},
                                                        {'label':'SCCM', 'value':'sccm'},
                                                        {'label':'Sharepoint', 'value':'SP'},
                                                        {'label':'Exchange', 'value':'EXC'}], value = 'OD4B'),
                                dcc.Graph(id='my-graph')


]
)

@app.callback(Output('my-graph', 'figure'), [Input('my-dropdown', 'value')])
def update_graph(selected_dropdown_value):
    if selected_dropdown_value =='SP':
        return  {'data': [{'y': SP['Active SP2010 site collections'],'x': SP.index, 'name':'Active SP2010 sites'},
                          {'y': SP['Active O365 site collections'],'x': SP.index, 'name':'Active O365 sites'}],
                         'layout': {'title': 'Sharepoint Migration'}}




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

Hi,
You can probably extend parse_contents function from upload example.
if saves uploaded file in df variable which you can clean up and process as you wish.
Or save file to DB for further processing:

elif ‘xls’ in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
df.to_sql(‘table1’, conn, if_exists=‘replace’, index=False )