Learn how to use Dash Bio for next-gen sequencing & quality control. 🧬 Register for the Oct 27 webinar.

Returning a plotly.graph_objs figure from a class within a callback

Hi all,

I’ve written a class which processes some data and one of it’s functions returns a ‘plotly.graph_objs._figure.Figure’

If i use the output of this class in my app layout as below

html.Div(
      className="eight columns div-for-charts bg-grey",
      children=[
            dcc.Graph(id="pca-details",
                             figure=pca.plot_graphs()),
            dcc.Graph(id="pca-annual",
                              figure=pca.plot_annual())

This creates the graphs in my dash app as expected but means I can’t create the data dynamically.

However, if I use the class within a callback and return graph_objects.Figure like in the code below, the graphs don’t show any of the data but I also don’t receive any errors

html.Div(
      className="eight columns div-for-charts bg-grey",
      children=[
            dcc.Graph(id="pca-details"),
            dcc.Graph(id="pca-annual")
.....

@app.callback(
       Output('pca-details', 'figure'),
       [Input('name-dropdown', 'value')])
   def update_figure(name):
       pca = PCA_class('data.csv', 'data2.csv')
       pca.create_EWM_(name)
       pca.scale_fit_transform_()
       return pca.plot_graphs()

Any help as to why the callbacks aren’t workign properly would be much appreciated

Thanks
Grant

Hi @grant592 welcome to the forum! Did you try to print(pca.plot_graphs()) in the callback before returning this object? With such a print statement you can make sure that the callback is fired as it should, and that the figure object looks like it should. You can also take a look at possible error messages in the Javascript console of your browser (in the developers tools).

Thanks for the reply. Unwittingly I had set

app.config['suppress_callback_exceptions'] = True 

so I was not seeing the output in the console. It turns out this was due to a simple spelling error in my ‘name-dropdown’.

Thanks for taking the time to reply

1 Like