Here is the link to the stack overflow question and in brief I am trying to pull some data from an endpoint and update the a chart from that end point.
app.layout = html.Div(
[
html.H1("File Browser"),
html.H3('Select Model Type & Upload File'),
dcc.RadioItems(
id='modelType',
options=[
{'label': 'ALBERT', 'value': '1'},
{'label': 'BERT', 'value': '2'},
],
labelStyle={'display': 'inline-block'}),
dcc.Upload(
id="upload-data",
children=html.Div(
["Drag and drop or click to select a file to upload."]
),
style={
"width": "100%",
"height": "60px",
"lineHeight": "60px",
"borderWidth": "1px",
"borderStyle": "dashed",
"borderRadius": "5px",
"textAlign": "center",
"margin": "10px",
},
multiple=True,
),
dcc.Graph(id='Mygraph'),
html.Div(id='output-data-upload'),
html.H2("File List"),
html.Ul(id="file-list"),
html.Div(id='intermediate-value')
],
)
def save_file(name, content):
"""Decode and store a file uploaded with Plotly Dash."""
data = content.encode("utf8").split(b";base64,")[1]
with open(os.path.join(UPLOAD_DIRECTORY, name), "wb") as fp:
fp.write(base64.decodebytes(data))
def uploaded_files():
"""List the files in the upload directory."""
files = []
for filename in os.listdir(UPLOAD_DIRECTORY):
path = os.path.join(UPLOAD_DIRECTORY, filename)
if os.path.isfile(path):
files.append(filename)
return files
def file_download_link(filename):
"""Create a Plotly Dash 'A' element that downloads a file from the app."""
location = "/download/{}".format(urlquote(filename))
return html.A(filename, href=location)
@app.callback(Output("intermediate-value", "children"),
[Input("upload-data", "filename"), Input("upload-data", "contents"),
Input('modelType', 'value')])
def post_to_database(uploaded_filenames, uploaded_file_contents,value):
metadata = db.MetaData()
conn = engine.connect()
table = db.Table('jobs', metadata, autoload=True, autoload_with=engine)
if uploaded_filenames is not None and uploaded_file_contents is not None:
for name, data in zip(uploaded_filenames, uploaded_file_contents):
save_file(name, data)
qry = db.insert(table).values(
type = value,
csv_path = 'var/html/' + name
)
ins_sql = conn.execute(qry)
job_id = ins_sql.inserted_primary_key
print('job id ', job_id)
result = requests.post('some_endpoint', json={'job_id' : 8})
data = result.text
return data
@app.callback(Output('Mygraph','figure'),
[Input('intermediate-value', 'children')])
def update_graph(data_response):
if data_response is None:
raise dash.exceptions.PreventUpdate
data = {"negative":1,"neutral":12,"positive":6}
df =pd.DataFrame.from_dict(data,orient='index',columns=['count'])
figure = px.pie(df, values='count', names=df.index, title='Predicted Labels')
return figure
The error is in the stackoverflow question https://stackoverflow.com/questions/63721481/i-cant-update-a-chart-after-requesting-some-data-form-a-call-back-in-dash