Why is the canvas not getting updated when I supply another file as input?
import base64
import datetime
import io
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import dash_canvas
import dash_table
import pandas as pd
from dash_canvas.utils import parse_jsonstring_rectangle
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
list_columns = ['width', 'height', 'left', 'top', 'label']
columns = [{'name': i, "id": i} for i in list_columns]
columns[-1]['presentation'] = 'dropdown'
list_preferred = ['Company Name','Company Address','Invoice Number','Tax','Total']
shortlists = [{'label': i, 'value': i} for i in list_preferred]
app.layout = html.Div([
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
),
dash_canvas.DashCanvas(
id='canvas',
width=500,
tool='rectangle',
lineWidth=2,
lineColor='rgba(255,0, 0, 0.5)',
hide_buttons=['pencil', 'line'],
goButtonTitle='Label'
),
html.Div([
html.Div([
html.H3('Label images with bounding boxes'),
]),
html.Div([
dash_table.DataTable(
id='table',
columns=columns,
editable=True,
dropdown = {'label': {'options': shortlists}},
export_format='xlsx',
export_headers='display'),
])
]),
html.Div(id='output-data-upload'),
])
@app.callback(Output('canvas', 'filename'),[Input('upload-data', 'filename')],prevent_initial_call=True)
def update_output(list_of_names):
print("list_of_names\n",list_of_names)
filename = list_of_names
if filename is not None and filename[0] !="":
return app.get_asset_url(filename[0])
@app.callback(Output('table', 'data'), [Input('canvas', 'json_data')], [State('table','data')],prevent_initial_call=True)
def show_string(json_data,table_data):
if json_data!=None:
box_coordinates = parse_jsonstring_rectangle(json_data)
df = pd.DataFrame(box_coordinates, columns=list_columns[:-1])
stdt = df.to_dict('records')
if table_data!=None:
for i in range(len(table_data)):
stdt[i]['label'] = table_data[i]['label']
return stdt