I write the code below to let users upload photos, then I use a callback to update datatable according to number of photos uploaded.
To let users preview their uploaded photos, I want to display them wth tooltip_data in datatable accordingly.
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.Hr(),
dash_table.DataTable(id = "site_photo_datatable",
columns = [{"id":col, "name":col} for col in ["Photo", "Photo Date", "Location", "Remarks"]],
data=[{"Photo":"default.jpg", "Photo Date":today, "Location":"Location", "Remarks":"Remarks"}],
tooltip_data = [],
editable = True,
column_selectable = "multi",
row_deletable = True,
style_cell = {"textAlign":"left"},
style_header={"backgroundColor":"Aquamarine", "font-weight":"bold"}),
])
@app.callback(Output('site_photo_datatable', 'data'),
Output('site_photo_datatable', 'tooltip_data'),
Input('upload-image', 'contents'),
State('upload-image', 'filename'),
State("site_photo_datatable", "data"),
prevent_initial_call = True
)
def update_output(contents, filename, data):
if contents is not None:
photo_no = 1
data = []
tooltip_for_table = []
for i in range(len(contents)):
data.append({"Photo":f"Photo {photo_no}", "Photo Date":today, "Location":"", "Remarks":""})
tooltip_for_table.append({"Photo": {"value": f"![]({html.Img(src=contents[photo_no-1])})",
"type":"markdown"}})
photo_no += 1
return data, tooltip_for_table
However, after I run the code, I only get a blank tooltip pop-up cell, without photo displayed. Any advice?
app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
*I have used bootstrap in my code, in case it matters