Flask `current_app` context

Hey everyone,

I’ve wanted to decouple some parts of suboptimal code and read about the flask.current_app feature, which exposes the flask app context more easily. Is the use in dash the same? Do I just use the flask option? Or is there something like from dash import current_app?

also see here:


That’s a neat option! I suppose flask.current_app might be available in Dash, but it would give you the Flask app, not the Dash app. Sounds like flask.current_app is primarily for use inside request handlers, is your use case inside callback functions? We could certainly add the app to dash.callback_context and you could access it from there.

While that would be nice as well, I’m not entirely sure if it’s exactly my context. The context would be when I declare a callback, e.g. I define my UI and callbacks in a class

.... other imports
from flask import current_app as app

class UI:
   def __init__(self):
      # some stuff

   def layout(self):
      return html.Div(children=[...more content....], id etc...)
   # some example callback
   def set_callbacks(self):
      ramp_servo_target = 'rampTarget_{}'.format(self._deviceNumber)
      dynamically_generated_function = self.__createRampCallback()
         Output('rampInfo_{}'.format(self._deviceNumber), 'data'),
         [Input(ramp_servo_target, 'value')]

specifically I need something to reference when I use app.callback(...). I hoped this would work through flask in some sort of way where the server always provides this instance of the app it’s running, but that seems wrong to me now.

What I currently used as a solution is I define the dash.Dash() instance in some dependencies module and import that in both the UI module and the run.py or app.py file as in from dependencies import app with:

import dash
from nqontrol import settings
from nqontrol.servoDevice import ServoDevice
from nqontrol.errors import ConfigurationError

if settings.DEVICES_LIST:
    devices = [ServoDevice(deviceNumber=i, readFromFile=settings.SETTINGS_FILE)
               for i in settings.DEVICES_LIST]
    raise ConfigurationError('No device specified and no save file provided.')

app = dash.Dash(__name__)

this being the dependencies file

Ah I see. Yes, that’s the currently accepted pattern to use. I’m not sure it’s a good idea to rely on the app existing when each of these UI files is executed. We’ve been discussing another way to solve this problem in https://github.com/plotly/dash/issues/637 - in that case each module, which I was provisionally calling a Section, wouldn’t need to know anything about the app it’s going to be added to - the app and the Section class would take care of that when adding it to the layout.

That and the added persistence features make for great additions to the system, just read up on that. Since you authored that and I just got you on the line, does the persistence change make the old set the layout as a function thingy redundant? I mean the part on how Dash either caches the inital layout or retriggers components on browser refresh or when opening in a second tab (accidentally).

Also jsut read through the section part and geeez would that be great. Thanks a lot :smiley: