Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Multiple Column filtering in data-frame after uploading a csv or excel files and savethe resultant data-frame

Hi all,
I need help in filtering the columns and save the resultant filtered data in data-frame. I have done the coding for displaying the data after uploading a csv or excel file. Please do needful at earlier.



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

#HTML Layout

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


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

    
# Function for reading the data

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

    decoded = base64.b64decode(content_string)
    try:
        if 'csv' in filename:
            # Assume that the user uploaded a CSV or TXT file
            df = pd.read_csv(
                io.StringIO(decoded.decode('utf-8')))
        elif 'xls' in filename:
            # Assume that the user uploaded an excel file
            df = pd.read_excel(io.BytesIO(decoded))
        elif 'txt' or 'tsv' in filename:
            # Assume that the user upl, delimiter = r'\s+'oaded an excel file
            df = pd.read_csv(
                io.StringIO(decoded.decode('utf-8')), delimiter = r'\s+')
    except Exception as e:
        print(e)
        return html.Div([
            'There was an error processing this file.'
        ])

    return df



            
#Call backs 

def generate_table(df_final):
    return dash_table.DataTable(data=df_final.to_dict('rows'),columns=[{'name': i, 'id': i} for i in df_final.columns],editable=True,
                                    virtualization=True,
                                    fixed_rows={'headers': True},
                                    page_current=0,
                                    page_size=5,
                                    style_table={ 'height':'350px','overflowY': 'auto'},
                                    style_cell_conditional=[{'if': {'column_id': c},'textAlign': 'left'} 
                                                           for c in ['Date', 'Region']],style_data_conditional=[{
                                       'if': {'row_index': 'odd'},'backgroundColor': 'rgb(248, 248, 248)'}],
                                    style_header={'backgroundColor': 'rgb(230, 230, 230)','fontFamily':'sans-serif',
                                                  'fontWeight': 'bold',"fontSize":'13px'},
                                    
                                    style_cell={'minWidth': 95, 'maxWidth': 95, 'width': 95}
                                   )

@app.callback(Output('output-data-upload','children'),
              [Input('upload-data', 'contents')],
              [State('upload-data', 'filename')]
    
)


#function for displaying the preview of the input file.

def display_table(contents,filename):
    if contents:
        contents = contents[0]
        filename= filename[0]
        df = parse_data(contents,filename)
        return html.Div([
            html.Center(html.H5('Preview')),
            html.Center(
                html.Div([
               
                    html.Div([
                        generate_table(df_final_1)
                    ])
                    ],style={'width':'85%'}
                )
        )])
    

   
    

#Calling the server    

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