Upload
Files can be text files, images, zip files, excel spreadsheets, etc.
Does not support hdf5 files
Each file is converted to a base64 encoded string
Size limits
- 249M, OK
- 624M, No Response
Upload a Single File
import base64
import dash
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
from dash.exceptions import PreventUpdate
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Upload(id = 'upload-data',
children=html.Div([
'Drag and Drop or Select a File',
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
multiple=False),
html.Div(id = 'content')
])
def parse(content):
content_type, content_string = content.split(',') # split the received content
decoded = base64.b64decode(content_string) # convert printable ASCII characters to bytes
decoded_str = decoded.decode('utf-8') # convert binary data to string
return decoded_str
@app.callback(Output('content', 'children'),
Input('upload-data', 'contents'),
)
def get_update_content(content):
if not content:
raise PreventUpdate
decoded_str = parse(content)
return decoded_str
if __name__ == "__main__":
app.run_server(debug=True)
Upload Multiple Files
import base64
import dash
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
from dash.exceptions import PreventUpdate
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Upload(id = 'upload-data',
children=html.Div([
'Drag and Drop or Select a File',
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
multiple=True),
html.Div(id = 'content')
])
def parse(content):
content_type, content_string = content.split(',') # split the received content
decoded = base64.b64decode(content_string) # convert printable ASCII characters to bytes
decoded_str = decoded.decode('utf-8') # convert binary data to string
return decoded_str
@app.callback(Output('content', 'children'),
Input('upload-data', 'contents'),
)
def get_update_content(contents):
if not contents:
raise PreventUpdate
temp = ''
for content in contents:
temp += parse(content)
return temp
if __name__ == "__main__":
app.run_server(debug=True)
Upload Data in CSV
import io
import base64
import dash
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
from dash.exceptions import PreventUpdate
import pandas as pd
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Upload(id = 'upload-data',
children=html.Div([
'Drag and Drop or Select a File',
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
multiple=False),
html.Div(id = 'output-content')
])
def parse(content):
content_type, content_string = content.split(',') # split the received content
decoded = base64.b64decode(content_string) # convert printable ASCII characters to bytes
decoded_str = decoded.decode('utf-8') # convert binary data to string
df = pd.read_csv(io.StringIO(decoded_str))
return str(df.shape)
@app.callback(Output('output-content', 'children'),
Input('upload-data', 'contents'),
State('upload-data', 'filename'),
)
def get_update_content(content, filename):
if not content:
raise PreventUpdate
data_string = parse(content)
return data_string
if __name__ == "__main__":
app.run_server(debug=True)
Upload an Image
import io
import base64
import dash
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
from dash.exceptions import PreventUpdate
import pandas as pd
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Upload(id = 'upload-data',
children=html.Div([
'Drag and Drop or Select a File',
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
multiple=False),
html.Div(id = 'output-content')
])
@app.callback(Output('output-content', 'children'),
Input('upload-data', 'contents'),
State('upload-data', 'filename'),
)
def get_update_content(content, filename):
if not content:
raise PreventUpdate
return html.Img(src=content)
if __name__ == "__main__":
app.run_server(debug=True)
dash-uploader
from pathlib import Path
import uuid
import dash_uploader as du
import dash
import dash_html_components as html
from dash.dependencies import Input, Output, State
app = dash.Dash(__name__)
UPLOAD_FOLDER_ROOT = r"Uploads"
du.configure_upload(app, UPLOAD_FOLDER_ROOT)
def get_upload_component(id):
return du.Upload(
id=id,
max_file_size=1800, # 1800 Mb
filetypes=['csv', 'zip'],
upload_id=uuid.uuid1(), # Unique session id
)
def get_app_layout():
return html.Div(
[
html.H1('Demo'),
html.Div(
[
get_upload_component(id='dash-uploader'),
html.Div(id='callback-output'),
],
style={ # wrapper div style
'textAlign': 'center',
'width': '600px',
'padding': '10px',
'display': 'inline-block'
}),
],
style={
'textAlign': 'center',
},
)
# get_app_layout is a function
# This way we can use unique session id's as upload_id's
app.layout = get_app_layout()
@du.callback(
output=Output('callback-output', 'children'),
id='dash-uploader',
)
def get_a_list(filenames):
return html.Ul([html.Li(filenames)])
if __name__ == '__main__':
app.run_server(debug=True)
Reference