Hi.
Below is my code. Currently it has 3 functions to perform:
- User selects the file type.
- User uploads the data which is displayed on the screen.
- A dropdown for selecting a particular column of the dataset.
Now I want to perform operations on that selected column but i do not know the way to extract it into a new variable.
import base64
import datetime
import io
import dash
from dash.dependencies import Input, Output, State
from dash import dcc, html, dash_table
import pandas as pd
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
dcc.Dropdown(['Excel Sheet', 'CSV File'], id='file_type', multi=False, style={'width': "40%"}),
html.Div(id='output_filetype'),
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'),
html.Div(id='column_dropdown'),
])
def parse_contents(contents, filename, date):
content_type, content_string = contents.split(',')
#global df
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Assume that the user uploaded a CSV 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))
app.layout = html.Div([
dcc.Dropdown(
[{'name': i, 'id': i} for i in df.columns], id='file_type', multi=False, style={'width': "40%"}
),
html.Div(id='output_filetype'),])
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
####### Dropdown #########
return html.Div([
html.H5(filename),
html.H6(datetime.datetime.fromtimestamp(date)),
dash_table.DataTable(
df.to_dict('records'),
[{'name': i, 'id': i} for i in df.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'
})
])
def selectData(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
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))
Dropdown = html.Div([
html.H4(('File Name: ', filename), style={'width': '100%', 'textAlign': 'center'}),
html.Br(),
dcc.Dropdown(
id='Dataset_and_Column',
options=[{'label': i, 'value': i} for i in df.columns],
style={'width': '100%', 'height': '60px', 'lineHeight': '60px', 'borderWidth': '1px',
'borderStyle': 'dashed', 'borderRadius': '5px', 'textAlign': 'center', 'display': 'inline-block'},
placeholder='Select Dataset Column'
),
html.Br(),
])
return Dropdown
except Exception as e:
print(e)
return html.Div([
'There was an error processing this file.'
])
@app.callback(Output('output_filetype', 'children'),
Output('output-data-upload', 'children'),
Output('column_dropdown', 'children'),
Input(component_id='file_type', component_property='value'),
Input('upload-data', 'contents'),
State('upload-data', 'filename'),
State('upload-data', 'last_modified'))
def update_output(value, list_of_contents, list_of_names, list_of_dates):
container = "{}".format(value)
if list_of_contents is not None and value is not None:
Dropdown = [selectData(c, n, d) for c, n, d in zip(list_of_contents, list_of_names, list_of_dates)]
children = [
parse_contents(c, n, d) for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
return container, children, Dropdown
else:
return '','',''
if __name__ == '__main__':
app.run_server(debug=True)