From 7c1aa058382edcab979359628082bd4baa9ca5ff Mon Sep 17 00:00:00 2001 From: Alexandre Batisse Date: Sat, 15 Jun 2019 08:39:20 +0200 Subject: [PATCH 1/4] use flask name if not set --- dash/dash.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/dash/dash.py b/dash/dash.py index db544be0b1..f464013a6f 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -205,7 +205,7 @@ class Dash(object): """ def __init__( self, - name='__main__', + name=None, server=True, assets_folder='assets', assets_url_path='assets', @@ -239,10 +239,14 @@ def __init__( # We have 3 cases: server is either True (we create the server), False # (defer server creation) or a Flask app instance (we use their server) - if isinstance(server, bool): - self.server = Flask(name) if server else None - elif isinstance(server, Flask): + if isinstance(server, Flask): self.server = server + # GH 771 + if name is None: + name = getattr(server, 'name', '__main__') + elif isinstance(server, bool): + name = name if name else '__main__' + self.server = Flask(name) if server else None else: raise ValueError('server must be a Flask app or a boolean') From e947ec814c185a898bc6481f94146132cd37b5fe Mon Sep 17 00:00:00 2001 From: Alexandre Batisse Date: Sat, 15 Jun 2019 08:54:04 +0200 Subject: [PATCH 2/4] add changelog entry --- dash/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dash/CHANGELOG.md b/dash/CHANGELOG.md index f10f09459c..d9cfb06fc2 100644 --- a/dash/CHANGELOG.md +++ b/dash/CHANGELOG.md @@ -15,6 +15,8 @@ - [#739](https://github.com/plotly/dash/pull/739) Allow the Flask app to be provided to Dash after object initialization. This allows users to define Dash layouts etc when using the app factory pattern, or any other pattern that inhibits access to the app object. This broadly complies with the flask extension API, allowing Dash to be considered as a Flask extension where it needs to be. +- [#774](https://github.com/plotly/dash/pull/774) Allow the Flask app to set the Dash app name if the name is not provided by users. + - [#722](https://github.com/plotly/dash/pull/722) Assets are served locally by default. Both JS scripts and CSS files are affected. This improves robustness and flexibility in numerous situations, but in certain cases initial loading could be slowed. To restore the previous CDN serving, set `app.scripts.config.serve_locally = False` (and similarly with `app.css`, but this is generally less important). - Undo/redo toolbar is removed by default, you can enable it with `app=Dash(show_undo_redo=true)`. The CSS hack `._dash-undo-redo:{display:none;}` is no longer needed [#724](https://github.com/plotly/dash/pull/724) From 6c698860da6410fb85aaca8fd4318a972bbb08d6 Mon Sep 17 00:00:00 2001 From: Alexandre Batisse Date: Sun, 16 Jun 2019 09:08:11 +0200 Subject: [PATCH 3/4] remove unneeded comment --- dash/dash.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dash/dash.py b/dash/dash.py index f464013a6f..deb28d76fc 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -241,7 +241,6 @@ def __init__( # (defer server creation) or a Flask app instance (we use their server) if isinstance(server, Flask): self.server = server - # GH 771 if name is None: name = getattr(server, 'name', '__main__') elif isinstance(server, bool): From f2ef20714310456f4838c1d9adae096c6c18fcaa Mon Sep 17 00:00:00 2001 From: Alexandre Batisse Date: Sun, 16 Jun 2019 09:08:35 +0200 Subject: [PATCH 4/4] add missing test for app name setting --- tests/unit/test_configs.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_configs.py b/tests/unit/test_configs.py index 28417f09f0..341be7ad94 100644 --- a/tests/unit/test_configs.py +++ b/tests/unit/test_configs.py @@ -1,9 +1,11 @@ import os import unittest +import pytest +from flask import Flask # noinspection PyProtectedMember from dash._configs import ( pathname_configs, DASH_ENV_VARS, get_combined_config, load_dash_env_vars) -from dash import exceptions as _exc +from dash import Dash, exceptions as _exc from dash._utils import get_asset_path @@ -138,5 +140,17 @@ def test_load_dash_env_vars_refects_to_os_environ(self): self.assertEqual(vars[var], 'false') +@pytest.mark.parametrize('name, server, expected', [ + (None, True, '__main__'), + ('test', True, 'test'), + ('test', False, 'test'), + (None, Flask('test'), 'test'), + ('test', Flask('other'), 'test'), +]) +def test_app_name_server(name, server, expected): + app = Dash(name=name, server=server) + assert app.config.name == expected + + if __name__ == '__main__': unittest.main()