Hello, I’m still new to dash. I am trying to upload multiple images to my server but this code only registers one image and also, uploading(2 images) take a very long time. not sure what I am doing wrong in the code.
Any help would be much appreciated. Thanks
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
#import dash_table_experiments as dt
import datetime
import base64
import json
import pandas as pd
import plotly
import io
import numpy as np
app = dash.Dash()
app.scripts.config.serve_locally = True
app.layout = html.Div([
dcc.Upload(
id='upload-image',
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-image-upload'),
])
def parse_contents(contents):
return html.Div([
# HTML images accept base64 encoded strings in the same format
# that is supplied by the upload
html.Img(src=contents),
html.Hr(),
html.Div('Raw Content'),
html.Pre(contents[0:2] + '...', style={
'whiteSpace': 'pre-wrap',
'wordBreak': 'break-all'
})
])
@app.callback(Output('output-image-upload', 'children'),
[Input('upload-image', 'contents')])
def update_output(list_of_contents):
print(np.array(list_of_contents.split(',')).shape)
if list_of_contents is not None:
import base64
from base64 import decodestring
cont=np.array(list_of_contents.split(','))
num=cont.shape
for i in range(0,num[0]):
with open("./foo_(%d).jpg"% i,"wb") as f:
f.write(decodestring(list_of_contents[list_of_contents.find(",")+i:].encode('ascii')))
children = [parse_contents(c) for c in list_of_contents]
return children
app.css.append_css({
'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'
})
if __name__ == '__main__':
app.run_server(debug=True)