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

update get_asset_url function #1528

Merged
merged 6 commits into from
Jan 17, 2021
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,9 @@ app = dash.Dash(...)

## 0.17.3 - 2017-06-22
✨ This is the initial open-source release of Dash.

### Fixed
- [#1527](https://github.com/plotly/dash/issues/1527)🐛 `get_asset_url` now pulls from an external source if `assets_external_path` is set.
- updated `_add_assets_resource` to build asset urls the same way as `get_asset_url`.
- updated doc string for `assets_external_path` Dash argument to be more clear that it will allways be joined with
the `assets_url_path` argument when determining the url to an external asset.
25 changes: 13 additions & 12 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,12 @@ class Dash(object):
:type assets_ignore: string

:param assets_external_path: an absolute URL from which to load assets.
Use with ``serve_locally=False``. Dash can still find js and css to
automatically load if you also keep local copies in your assets
folder that Dash can index, but external serving can improve
performance and reduce load on the Dash server.
Use with ``serve_locally=False``. assets_external_path is joined
with assets_url_path to determine the absolute url to the
asset folder. Dash can still find js and css to automatically load
if you also keep local copies in your assets folder that Dash can index,
but external serving can improve performance and reduce load on
the Dash server.
env: ``DASH_ASSETS_EXTERNAL_PATH``
:type assets_external_path: string

Expand Down Expand Up @@ -1098,9 +1100,7 @@ def _setup_server(self):
def _add_assets_resource(self, url_path, file_path):
res = {"asset_path": url_path, "filepath": file_path}
if self.config.assets_external_path:
res["external_url"] = "{}{}".format(
self.config.assets_external_path, url_path
)
res["external_url"] = self.get_asset_url(url_path.lstrip("/"))
self._assets_files.append(file_path)
return res

Expand Down Expand Up @@ -1185,11 +1185,12 @@ def csp_hashes(self, hash_algorithm="sha256"):
]

def get_asset_url(self, path):
asset = get_asset_path(
self.config.requests_pathname_prefix,
path,
self.config.assets_url_path.lstrip("/"),
)
if self.config.assets_external_path:
prefix = self.config.assets_external_path
else:
prefix = self.config.requests_pathname_prefix

asset = get_asset_path(prefix, path, self.config.assets_url_path.lstrip("/"))

return asset

Expand Down
34 changes: 34 additions & 0 deletions tests/unit/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,40 @@ def test_pathname_prefix_assets(empty_environ, req, expected):
assert path == expected


@pytest.mark.parametrize(
"requests_pathname_prefix, assets_external_path, assets_url_path, expected",
[
(None, None, "assets", "/assets/reset.css"),
("/app/", None, "assets", "/app/assets/reset.css"),
(None, None, "css", "/css/reset.css"),
("/app/", None, "css", "/app/css/reset.css"),
(
None,
"http://external.com/",
"assets",
"http://external.com/assets/reset.css",
),
("/app/", "http://external.com/", "css", "http://external.com/css/reset.css"),
],
)
def test_asset_url(
empty_environ,
requests_pathname_prefix,
assets_external_path,
assets_url_path,
expected,
):
app = Dash(
"Dash",
requests_pathname_prefix=requests_pathname_prefix,
assets_external_path=assets_external_path,
assets_url_path=assets_url_path,
)

path = app.get_asset_url("reset.css")
assert path == expected


def test_get_combined_config_dev_tools_ui(empty_environ):
val1 = get_combined_config("ui", None, default=False)
assert (
Expand Down