Skip to content

Commit b8dfc38

Browse files
committedMay 23, 2019
Add separate init_app function for providing the flask app
1 parent f958d9e commit b8dfc38

File tree

1 file changed

+76
-59
lines changed

1 file changed

+76
-59
lines changed
 

Diff for: ‎dash/dash.py

+76-59
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ def __init__(
108108
components_cache_max_age=None,
109109
show_undo_redo=False,
110110
plugins=None,
111+
defer_app=False,
111112
**kwargs):
112113

114+
# Store some flask-related parameters for use in init_app()
115+
self.compress = compress
116+
self.name = name
117+
113118
# pylint-disable: too-many-instance-attributes
114119
if 'csrf_protect' in kwargs:
115120
warnings.warn('''
@@ -154,22 +159,6 @@ def __init__(
154159
'show_undo_redo': show_undo_redo
155160
})
156161

157-
assets_blueprint_name = '{}{}'.format(
158-
self.config.routes_pathname_prefix.replace('/', '_'),
159-
'dash_assets'
160-
)
161-
162-
self.server.register_blueprint(
163-
flask.Blueprint(
164-
assets_blueprint_name, name,
165-
static_folder=self._assets_folder,
166-
static_url_path='{}{}'.format(
167-
self.config.routes_pathname_prefix,
168-
assets_url_path.lstrip('/')
169-
)
170-
)
171-
)
172-
173162
# list of dependencies
174163
self.callback_map = {}
175164

@@ -181,15 +170,6 @@ def __init__(
181170
# default renderer string
182171
self.renderer = 'var renderer = new DashRenderer();'
183172

184-
if compress:
185-
# gzip
186-
Compress(self.server)
187-
188-
@self.server.errorhandler(exceptions.PreventUpdate)
189-
def _handle_error(_):
190-
"""Handle a halted callback and return an empty 204 response"""
191-
return '', 204
192-
193173
# static files from the packages
194174
self.css = Css()
195175
self.scripts = Scripts()
@@ -204,8 +184,79 @@ def _handle_error(_):
204184
# urls
205185
self.routes = []
206186

187+
self._layout = None
188+
self._cached_layout = None
189+
self._dev_tools = _AttributeDict({
190+
'serve_dev_bundles': False,
191+
'hot_reload': False,
192+
'hot_reload_interval': 3000,
193+
'hot_reload_watch_interval': 0.5,
194+
'hot_reload_max_retry': 8,
195+
'ui': False,
196+
'props_check': False,
197+
})
198+
199+
self._assets_files = []
200+
201+
# hot reload
202+
self._reload_hash = None
203+
self._hard_reload = False
204+
self._lock = threading.RLock()
205+
self._watch_thread = None
206+
self._changed_assets = []
207+
208+
self.logger = logging.getLogger(name)
209+
self.logger.addHandler(logging.StreamHandler(stream=sys.stdout))
210+
211+
if isinstance(plugins, _patch_collections_abc('Iterable')):
212+
for plugin in plugins:
213+
plugin.plug(self)
214+
215+
if not defer_app:
216+
self.init_app()
217+
218+
def init_app(self, app=None):
219+
"""
220+
Initialize the parts of Dash that require a flask app
221+
"""
222+
223+
if app is not None:
224+
self.server = app
225+
226+
assets_blueprint_name = '{}{}'.format(
227+
self.config.routes_pathname_prefix.replace('/', '_'),
228+
'dash_assets'
229+
)
230+
231+
self.server.register_blueprint(
232+
flask.Blueprint(
233+
assets_blueprint_name,
234+
self.name,
235+
static_folder=self._assets_folder,
236+
static_url_path='{}{}'.format(
237+
self.config.routes_pathname_prefix,
238+
self._assets_url_path.lstrip('/')
239+
)
240+
)
241+
)
242+
243+
if self.compress:
244+
# gzip
245+
Compress(self.server)
246+
247+
@self.server.errorhandler(exceptions.PreventUpdate)
248+
def _handle_error(_):
249+
"""Handle a halted callback and return an empty 204 response"""
250+
return '', 204
251+
207252
prefix = self.config['routes_pathname_prefix']
208253

254+
self.server.before_first_request(self._setup_server)
255+
256+
# add a handler for components suites errors to return 404
257+
self.server.errorhandler(exceptions.InvalidResourceError)(
258+
self._invalid_resources_handler)
259+
209260
self._add_url('{}_dash-layout'.format(prefix), self.serve_layout)
210261

211262
self._add_url('{}_dash-dependencies'.format(prefix), self.dependencies)
@@ -236,40 +287,6 @@ def _handle_error(_):
236287
'{}_favicon.ico'.format(prefix),
237288
self._serve_default_favicon)
238289

239-
self.server.before_first_request(self._setup_server)
240-
241-
self._layout = None
242-
self._cached_layout = None
243-
self._dev_tools = _AttributeDict({
244-
'serve_dev_bundles': False,
245-
'hot_reload': False,
246-
'hot_reload_interval': 3000,
247-
'hot_reload_watch_interval': 0.5,
248-
'hot_reload_max_retry': 8,
249-
'ui': False,
250-
'props_check': False,
251-
})
252-
253-
# add a handler for components suites errors to return 404
254-
self.server.errorhandler(exceptions.InvalidResourceError)(
255-
self._invalid_resources_handler)
256-
257-
self._assets_files = []
258-
259-
# hot reload
260-
self._reload_hash = None
261-
self._hard_reload = False
262-
self._lock = threading.RLock()
263-
self._watch_thread = None
264-
self._changed_assets = []
265-
266-
self.logger = logging.getLogger(name)
267-
self.logger.addHandler(logging.StreamHandler(stream=sys.stdout))
268-
269-
if isinstance(plugins, _patch_collections_abc('Iterable')):
270-
for plugin in plugins:
271-
plugin.plug(self)
272-
273290
def _add_url(self, name, view_func, methods=('GET',)):
274291
self.server.add_url_rule(
275292
name,

0 commit comments

Comments
 (0)