Skip to content

Commit 4ce87ee

Browse files
authored
Merge pull request #1528 from karosc/dev
update get_asset_url function
2 parents c5ba38f + 1bda5fe commit 4ce87ee

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,9 @@ app = dash.Dash(...)
695695

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

dash/dash.py

+13-12
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,9 +1100,7 @@ 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-
)
1103+
res["external_url"] = self.get_asset_url(url_path.lstrip("/"))
11041104
self._assets_files.append(file_path)
11051105
return res
11061106

@@ -1185,11 +1185,12 @@ def csp_hashes(self, hash_algorithm="sha256"):
11851185
]
11861186

11871187
def get_asset_url(self, path):
1188-
asset = get_asset_path(
1189-
self.config.requests_pathname_prefix,
1190-
path,
1191-
self.config.assets_url_path.lstrip("/"),
1192-
)
1188+
if self.config.assets_external_path:
1189+
prefix = self.config.assets_external_path
1190+
else:
1191+
prefix = self.config.requests_pathname_prefix
1192+
1193+
asset = get_asset_path(prefix, path, self.config.assets_url_path.lstrip("/"))
11931194

11941195
return asset
11951196

tests/unit/test_configs.py

+34
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,40 @@ 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+
(
116+
None,
117+
"http://external.com/",
118+
"assets",
119+
"http://external.com/assets/reset.css",
120+
),
121+
("/app/", "http://external.com/", "css", "http://external.com/css/reset.css"),
122+
],
123+
)
124+
def test_asset_url(
125+
empty_environ,
126+
requests_pathname_prefix,
127+
assets_external_path,
128+
assets_url_path,
129+
expected,
130+
):
131+
app = Dash(
132+
"Dash",
133+
requests_pathname_prefix=requests_pathname_prefix,
134+
assets_external_path=assets_external_path,
135+
assets_url_path=assets_url_path,
136+
)
137+
138+
path = app.get_asset_url("reset.css")
139+
assert path == expected
140+
141+
108142
def test_get_combined_config_dev_tools_ui(empty_environ):
109143
val1 = get_combined_config("ui", None, default=False)
110144
assert (

0 commit comments

Comments
 (0)