title: Register plotly dash callbacks after app.run_server() without having to reload webpage?
body: <p>I have built a dash app where new visualizations can be loaded on demand from python classes.
  For static content without callbacks, this is working fine.</p>
  <p>When trying to load callbacks, these are not active when first calling the page. Only after having reloaded the webpage once, the on-demand content works as expected.</p>
  <p><a href="https://i.stack.imgur.com/MJFAE.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MJFAE.gif" alt="enter image description here" /></a></p>
  <p>My best guess is that this is caused by registering the callback <em>after</em> having executed <code>app.run_server()</code>. Preloading however is not an option, as the classes in my application require additional input arguments, which are only known during runtime of the server.</p>
  <p>So my question is: Does anyone know a way how to successfully register callbacks after the server is already running? Or another way to achieve the same behaviour?</p>
  <p>Here is my approach so far, comparing preloaded behaviour to on-demand behaviour:</p>
  <pre class="lang-py prettyprint-override"><code>from dash import Dash, html, dcc, Input, Output
  
  
  class OnDemandContent:
  
      def __init__(self):
          # prefix to distinguish on-demand from preloaded components
          self.prefix = self.__class__.__name__
          # registering a class method as a callback,
          # from https://community.plotly.com/t/putting-a-dash-instance-inside-a-class/6097/4
          app.callback(Output(f'{self.prefix}counter', 'children'),
                       Input(f'{self.prefix}interval', 'n_intervals'))(self.update)
  
      def get_layout(self):
          return html.Div([
              html.Div(['Counter has not started yet'], id=f'{self.prefix}counter'),
              dcc.Interval(id=f'{self.prefix}interval', interval=100),
          ])
  
      def update(self, n_clicks):
          return f'Counting: {n_clicks}'
  
  
  class PreloadedContent(OnDemandContent):
      pass
  
  # callback exceptions are suppressed, because some elements are loaded on demand
  app = Dash(__name__, suppress_callback_exceptions=True)
  
  app.layout = html.Div([
      html.Div('Preloaded Content is not shown yet', id='content-preloaded'),
      html.Button('Show PreloadedContent', id='show-preloaded', n_clicks=0),
      html.Hr(),
      html.Div('OnDemandContent is not shown yet', id='content-on-demand'),
      html.Button('Show OnDemandContent', id='show-on-demand', n_clicks=0),
  ])
  
  # initialize preload class and hence activate its callbacks before running the server.
  # in my app, this is not an option, because the class would require further input
  # which is given by the user during runtime
  preloaded_instance = PreloadedContent()
  
  # callbacks for &quot;show x&quot; buttons
  @app.callback(
      Output('content-on-demand', 'children'),
      Input('show-on-demand', 'n_clicks'),
      prevent_initial_call=True,
  )
  def update_output(_):
      return OnDemandContent().get_layout()
  
  @app.callback(
      Output('content-preloaded', 'children'),
      Input('show-preloaded', 'n_clicks'),
      prevent_initial_call=True,
  )
  def update_output(_):
      return preloaded_instance.get_layout()
  
  
  app.run_server(port=80, debug=True)
  </code></pre>