From 6295e22f77fdc85e7980f03834706cbfdd94189e Mon Sep 17 00:00:00 2001 From: Philippe Duval Date: Fri, 24 Aug 2018 14:48:19 -0400 Subject: [PATCH 1/2] Fix assets path take request_pathname_prefix into consideration. --- dash/_utils.py | 12 ++++++++++++ dash/dash.py | 13 ++++++++++--- tests/test_configs.py | 16 ++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/dash/_utils.py b/dash/_utils.py index bc71fde955..9b1a48faae 100644 --- a/dash/_utils.py +++ b/dash/_utils.py @@ -20,6 +20,18 @@ def format_tag(tag_name, attributes, inner='', closed=False, opened=False): '{}="{}"'.format(k, v) for k, v in attributes.items()])) +def get_asset_path(requests_pathname, routes_pathname, asset_path): + i = requests_pathname.rfind(routes_pathname) + req = requests_pathname[:i] + + return '/'.join([ + # Only take the first part of the pathname + req, + 'assets', + asset_path + ]) + + class AttributeDict(dict): """ Dictionary subclass enabling attribute lookup/assignment of keys/values. diff --git a/dash/dash.py b/dash/dash.py index 4cfd0d4684..8177761082 100644 --- a/dash/dash.py +++ b/dash/dash.py @@ -24,6 +24,7 @@ from ._utils import AttributeDict as _AttributeDict from ._utils import interpolate_str as _interpolate from ._utils import format_tag as _format_tag +from ._utils import get_asset_path as _get_asset_path from . import _configs @@ -329,9 +330,9 @@ def _relative_url_path(relative_package_path='', namespace=''): 'Serving files from absolute_path isn\'t supported yet' ) elif 'asset_path' in resource: - static_url = flask.url_for('assets.static', - filename=resource['asset_path'], - mod=resource['ts']) + static_url = self.get_asset_url(resource['asset_path']) + # Add a bust query param + static_url += '?m={}'.format(resource['ts']) srcs.append(static_url) return srcs @@ -942,6 +943,12 @@ def add_resource(p, filepath): elif f == 'favicon.ico': self._favicon = path + def get_asset_url(self, path): + return _get_asset_path( + self.config.requests_pathname_prefix, + self.config.routes_pathname_prefix, + path) + def run_server(self, port=8050, debug=False, diff --git a/tests/test_configs.py b/tests/test_configs.py index ee29673309..6b65613a90 100644 --- a/tests/test_configs.py +++ b/tests/test_configs.py @@ -2,6 +2,7 @@ # noinspection PyProtectedMember from dash import _configs from dash import exceptions as _exc +from dash._utils import get_asset_path import os @@ -88,6 +89,21 @@ def test_pathname_prefix_environ_requests(self): _, routes, req = _configs.pathname_configs() self.assertEqual('/requests/', req) + def test_pathname_prefix_assets(self): + req = '/' + routes = '/' + path = get_asset_path(req, routes, 'reset.css') + self.assertEqual('/assets/reset.css', path) + + req = '/requests/' + path = get_asset_path(req, routes, 'reset.css') + self.assertEqual('/requests/assets/reset.css', path) + + req = '/requests/routes/' + routes = '/routes/' + path = get_asset_path(req, routes, 'reset.css') + self.assertEqual('/requests/assets/reset.css', path) + if __name__ == '__main__': unittest.main() From b815c392a4184acc158b5cab3e4b3a7e549022d1 Mon Sep 17 00:00:00 2001 From: Philippe Duval Date: Mon, 27 Aug 2018 16:43:16 -0400 Subject: [PATCH 2/2] Version bump. --- CHANGELOG.md | 7 +++++++ dash/version.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e96b0267e7..5e8d53aab6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.26.3 - 2018-08-27 +## Fixed +- Prefix assets files with `requests_pathname_prefix`. [#351](https://github.com/plotly/dash/pull/351) + +## Added +- `Dash.get_asset_url` will give the prefixed url for the asset file. + ## 0.26.2 - 2018-08-26 ## Fixed - Only create the assets blueprint once for app that provide the same flask instance to multiple dash instance. [#343](https://github.com/plotly/dash/pull/343) diff --git a/dash/version.py b/dash/version.py index c42375ce97..be363825ce 100644 --- a/dash/version.py +++ b/dash/version.py @@ -1 +1 @@ -__version__ = '0.26.2' +__version__ = '0.26.3'