Set default configuration for Dash components

Hey there,

is there a way to set defaults for configuration options of Dash components. In particular I want to change the buttons in the Modebar across my complete Project. I know I can set the config parameter on every single component but this seems very messy and not the right way to do this.

Thanks for your help

I haven’t tried this, but maybe you could subclass dcc.Graph and set some default options there?

class MyGraph(dcc.Graph):
    def __init__(self, *args, **kwargs):
        config = {"modeBarButtonsToRemove": ["pan2d"]}
        if "config" in kwargs:
            config.update(kwargs["config"])
            kwargs.pop("config")

        super().__init__(*args, config=config, **kwargs)

Then just use MyGraph everywhere instead of dcc.Graph.

Another option would be to create utility function(s) for setting up the component(s), i.e. something like

import dash_core_components as dcc

def my_graph(*args, **kwargs):
    default_args = {"modeBarButtonsToRemove": ["pan2d"]}
    kwargs = {**kwargs, **default_args}
    return dcc.Graph(*args, **kwargs)
1 Like

This seems like a very nice solution to do it for a new app. But replacing it in the complete application requires some effort. I hoped for a more simple solution.