Dears,
[edit]
I have changed my post because I found now where is the issue. So let’s go right to the problem.
I have created a class where the layout, components and the callbacks to update graph are created.
This is working well if in the MAIN, there is no dcc location and the callback associated.
But if the dcc.Location is added, then the class callbacks are not triggered. The graph is rended with default values, but can not be changed it if user clicks the dropdown component
Here is the class code and the 2 options of the MAIN program. First one is running well, second not.
Thks for your help
class code
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import random
#class graph
class clsSimpleGraph():
def __init__(self, pApp,*args, **kwargs):
self.layout_graphID = 'graph_1'
self.App = pApp
self.x = [1, 2, 3, 4]
self.y=[]
self.layout= html.Div()
#(re)build graph
def clsBuildGraph(self, pY1=None):
self.y = pY1 if pY1 else [1, 2, 3, 4]
g=[]
g.append(dcc.Graph(id='graph', figure={'data' : [{'x': self.x, 'y': self.y,'type':'bar' }],'layout':{'title' : 'test'}}))
return g
#build the layout + components and graph
def clsBuildLayout(self):
self.layout =html.Div([
html.Div(id = 'filters', children=html.Span([html.H5('click to change '),
dcc.Dropdown(id='filter',options=[{'label': 'VALUE', 'value':random.sample(range(30), 4)}],
multi=False)])) ,
html.Div(id = 'chart', children = self.clsBuildGraph(self.y))
])
return self.layout
#create callbacks dynamically
def clsBuildCallbacks(self):
@self.App.callback(
Output(component_id='chart', component_property='children'),
[ Input(component_id= 'filter', component_property='value')])
def updatelayout(val1=None):
val1 = random.sample(range(30), 4)
return self.clsBuildGraph(val1)
#build the final application
#build the layout and build the callbacks
def clsBuildApp(self):
lay = self.clsBuildLayout()
callb = self.clsBuildCallbacks()
return lay
main option 1 : all is running well
if __name__=="__main__":
appServer=dash.Dash(__name__)
appServer.config['suppress_callback_exceptions']=True
graph = clsSimpleGraph(appServer)
appServer.layout = graph.clsBuildApp()
appServer.run_server(debug=True)
main option 2 : class callbacks don’t refresh the graph
if __name__=="__main__":
appServer=dash.Dash(__name__)
appServer.config['suppress_callback_exceptions']=True
appServer.layout = html.Div([
dcc.Location(id='url', refresh=False),
html.Div(id='page-content')
])
@appServer.callback(Output('page-content', 'children'),[Input('url', 'pathname')])
def display_page(pathname):
if pathname:
graph = clsSimpleGraph(appServer)
lay = graph.clsBuildApp()
appServer.layout = lay
return appServer.layout
appServer.run_server(debug=True)