Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pick up app name from server instance (#771) #774

Merged
merged 4 commits into from
Jun 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dash/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Dash(object):
"""
def __init__(
self,
name='__main__',
name=None,
server=True,
assets_folder='assets',
assets_url_path='assets',
Expand Down Expand Up @@ -239,10 +239,13 @@ 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
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')

Expand Down
16 changes: 15 additions & 1 deletion tests/unit/test_configs.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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()