Appreciate some help with the
“multiple files to be uploaded
multiple=True” property in dash
For this, am I able to have it “True” for 1 output/callback and then turn it to “False” for the next output/callback within the same dashboard script?
Because my first callback has commands that require ‘multiple’ since I parse the file and display the image, filename and date.
My next callback takes the image filename and preprocesses the image and runs it through a model and displays an output
It runs into issues because of that multiple property
My code:
model = keras.models.load_model(filename)
no_of_pixels = 255
app = dash.Dash(__name__)
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'
},
multiple=True
),
html.Div(id='output-image-upload'),
html.Div(id='output-prediction'),
])
#For the image display
def parse_contents(contents, filename, date):
return html.Div([
html.H5(filename),
html.H6(datetime.datetime.fromtimestamp(date)),
html.Img(src=contents),
html.Hr(),
html.Div('Raw Content'),
html.Pre(contents[0:200] + '...', style={
'whiteSpace': 'pre-wrap',
'wordBreak': 'break-all'
})
])
#for the model output
def load_and_preprocess(image):
image1 = Image.open(image)
rgb = Image.new('RGB', image1.size)
rgb.paste(image1)
image = rgb
test_image = image.resize((256,256))
return test_image
#for the model output
def np_array_normalise(test_image):
np_image = np.array(test_image)
np_image = np_image / no_of_pixels
final_image = np.expand_dims(np_image, 0)
return final_image
#For the image display
@app.callback(Output('output-image-upload', 'children'),
Input('upload-image', 'contents'),
State('upload-image', 'filename'),
State('upload-image', 'last_modified'),
#For the image display
def update_output(list_of_contents, list_of_names, list_of_dates):
if list_of_contents is not None:
children = [
parse_contents(c, n, d) for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
return children
#for the model output
@app.callback(Output('output-prediction', 'children'),
Input('upload-image', 'filename'))
#for the model output
def prediction(image):
final_img = load_and_preprocess(image)
final_img = np_array_normalise(final_img)
Y = model.predict(final_img)
return Y
if __name__ == '__main__':
app.run_server(debug=True)