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

Prefix assets with requests_pathname_prefix. #351

Merged
merged 2 commits into from
Aug 27, 2018
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
12 changes: 12 additions & 0 deletions dash/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 10 additions & 3 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


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

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion dash/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.26.2'
__version__ = '0.26.3'
16 changes: 16 additions & 0 deletions tests/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


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