Ok so here is a working example
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash()
app.config['suppress_callback_exceptions']=True
image = "tmpimg.png"
app.layout = html.Div([
html.Div([
html.Div([
html.Button('Load image', id='load-button'),
dcc.Upload(
id='upload-data',
children=html.Button('Upload image', id='upload-button')
)
]),
html.Div([
html.Div(id='images-div'),
html.Div(id='classification-div'),
html.Div(id='classification-div2')
])
])
])
@app.callback(
Output(component_id='images-div', component_property='children'),
[Input('load-button','n_clicks')]
)
def update_output_div_img(n_clicks):
return html.Div([html.Img(
src=app.get_asset_url(image),
style={
'width' : '10%',
'cursor': 'pointer'
}
)], id='img'
)
@app.callback(
Output(component_id='classification-div', component_property='children'),
[Input('img','n_clicks')]
)
def update_output_div1(n_clicks):
return html.Div([html.H2('Div1')])
@app.callback(Output('classification-div2', 'children'),
[Input('upload-data', 'contents')])
def update_output_div2(content):
return html.Div([html.H2('Div2')])
@app.callback(
Output(component_id='classification-div', component_property='style'),
[Input('upload-data', 'contents')]
)
def update_style(content):
if content:
return {'display':'none'}
else: return {'display':'inline'}
if __name__ == '__main__':
app.run_server()
This is not giving an error. So the update_output_div_img will load an image with a callback when you load the page or when you push the button “Load image”. Now after the image is loaded then you can click it and a text Div1 will appear. When you push the upload button to load an image the “Div1” should disappear and only the Div 2 remain. So far so good.
Now when I click on the image again the “Div1” text doesn’t appear because the display was changed to “none”. I want that when I click on the image again the “Div1” text should show up again so I modified the callback for the style of the first div above like this so that it gets triggered when you click on the image and since there is no content I guess it should change the display to inline.
@app.callback(
Output(component_id='classification-div', component_property='style'),
[Input('upload-data', 'contents'),
Input('img','n_clicks')]
)
def update_style(content,n_clicks):
if content:
return {'display':'none'}
else: return {'display':'inline'}
But this triggers the “Error Loading Dependencies” message I think because the image that is clicked is generated by another callback while the upload component was loaded from the start.
Any ideas how to work around this?
By the way, is there a way to select the code and format it to look like code in this forum without having to add 4 spaces manually before each line? I only see the “Blockquote” option but not for code formatting. Thanks