Skip to content

Commit 6f54daf

Browse files
author
Constantine Karos
committed
Change get_asset_url to be more DRY
- changed assets_external_path doc string to be more descriptive - revised _add_assets_resource to join assets_external_path with assets_url_path so that it is cogruent with how get_asset_path works - added unit test `test_asset_url` - updated changelog
1 parent 74af0a3 commit 6f54daf

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,3 +694,9 @@ app = dash.Dash(...)
694694

695695
## 0.17.3 - 2017-06-22
696696
✨ This is the initial open-source release of Dash.
697+
698+
### Fixed
699+
- [#1527](https://github.com/plotly/dash/issues/1527)🐛 `get_asset_url` now pulls from an external source if `assets_external_path` is set.
700+
- updated `_add_assets_resource` to build asset urls the same way as `get_asset_url`.
701+
- updated doc string for `assets_external_path` Dash argument to be more clear that it will allways be joined with
702+
the `assets_url_path` argument when determining the url to an external asset.

dash/dash.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,12 @@ class Dash(object):
150150
:type assets_ignore: string
151151
152152
:param assets_external_path: an absolute URL from which to load assets.
153-
Use with ``serve_locally=False``. Dash can still find js and css to
154-
automatically load if you also keep local copies in your assets
155-
folder that Dash can index, but external serving can improve
156-
performance and reduce load on the Dash server.
153+
Use with ``serve_locally=False``. assets_external_path is joined
154+
with assets_url_path to determine the absolute url to the
155+
asset folder. Dash can still find js and css to automatically load
156+
if you also keep local copies in your assets folder that Dash can index,
157+
but external serving can improve performance and reduce load on
158+
the Dash server.
157159
env: ``DASH_ASSETS_EXTERNAL_PATH``
158160
:type assets_external_path: string
159161
@@ -1098,8 +1100,9 @@ def _setup_server(self):
10981100
def _add_assets_resource(self, url_path, file_path):
10991101
res = {"asset_path": url_path, "filepath": file_path}
11001102
if self.config.assets_external_path:
1101-
res["external_url"] = "{}{}".format(
1102-
self.config.assets_external_path, url_path
1103+
res["external_url"] = "/".join([
1104+
self.config.assets_external_path.rstrip('/'), self.config.assets_url_path.strip('/'), url_path.lstrip('/')
1105+
]
11031106
)
11041107
self._assets_files.append(file_path)
11051108
return res
@@ -1186,17 +1189,15 @@ def csp_hashes(self, hash_algorithm="sha256"):
11861189

11871190
def get_asset_url(self, path):
11881191
if self.config.assets_external_path:
1189-
asset = get_asset_path(
1190-
self.config.assets_external_path,
1191-
path,
1192-
self.config.assets_url_path.lstrip("/"),
1193-
)
1192+
prefix = self.config.assets_external_path
11941193
else:
1195-
asset = get_asset_path(
1196-
self.config.requests_pathname_prefix,
1197-
path,
1198-
self.config.assets_url_path.lstrip("/"),
1199-
)
1194+
prefix = self.config.requests_pathname_prefix
1195+
1196+
asset = get_asset_path(
1197+
prefix,
1198+
path,
1199+
self.config.assets_url_path.lstrip("/")
1200+
)
12001201

12011202
return asset
12021203

tests/unit/test_configs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,28 @@ def test_pathname_prefix_assets(empty_environ, req, expected):
105105
assert path == expected
106106

107107

108+
@pytest.mark.parametrize(
109+
"requests_pathname_prefix, assets_external_path, assets_url_path, expected",
110+
[
111+
(None , None , "assets" , "/assets/reset.css"),
112+
("/app/" , None , "assets" , '/app/assets/reset.css'),
113+
(None , None , "css" , "/css/reset.css"),
114+
("/app/" , None , "css" , "/app/css/reset.css"),
115+
(None , "http:\\external.com/", "assets" , "http:\\external.com/assets/reset.css"),
116+
("/app/" , "http:\\external.com/", "css" , "http:\\external.com/css/reset.css"),
117+
118+
],)
119+
def test_asset_url(empty_environ, requests_pathname_prefix, assets_external_path, assets_url_path, expected):
120+
app = Dash('Dash',
121+
requests_pathname_prefix=requests_pathname_prefix,
122+
assets_external_path=assets_external_path,
123+
assets_url_path=assets_url_path
124+
)
125+
126+
path = app.get_asset_url('reset.css')
127+
assert path == expected
128+
129+
108130
def test_get_combined_config_dev_tools_ui(empty_environ):
109131
val1 = get_combined_config("ui", None, default=False)
110132
assert (

0 commit comments

Comments
 (0)