Skip to content

Commit b4a4ce9

Browse files
committed
Fix assets path take request_pathname_prefix into consideration.
1 parent e5f7d20 commit b4a4ce9

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

dash/_utils.py

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ def format_tag(tag_name, attributes, inner='', closed=False, opened=False):
2020
'{}="{}"'.format(k, v) for k, v in attributes.items()]))
2121

2222

23+
def get_asset_path(requests_pathname, routes_pathname, asset_path):
24+
i = requests_pathname.rfind(routes_pathname)
25+
req = requests_pathname[:i]
26+
27+
return '/'.join([
28+
# Only take the first part of the pathname
29+
req,
30+
'assets',
31+
asset_path
32+
])
33+
34+
2335
class AttributeDict(dict):
2436
"""
2537
Dictionary subclass enabling attribute lookup/assignment of keys/values.

dash/dash.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from ._utils import AttributeDict as _AttributeDict
2525
from ._utils import interpolate_str as _interpolate
2626
from ._utils import format_tag as _format_tag
27+
from ._utils import get_asset_path as _get_asset_path
2728
from . import _configs
2829

2930

@@ -327,9 +328,9 @@ def _relative_url_path(relative_package_path='', namespace=''):
327328
'Serving files from absolute_path isn\'t supported yet'
328329
)
329330
elif 'asset_path' in resource:
330-
static_url = flask.url_for('assets.static',
331-
filename=resource['asset_path'],
332-
mod=resource['ts'])
331+
static_url = self.get_asset_url(resource['asset_path'])
332+
# Add a bust query param
333+
static_url += '?m={}'.format(resource['ts'])
333334
srcs.append(static_url)
334335
return srcs
335336

@@ -938,6 +939,12 @@ def add_resource(p, filepath):
938939
elif f == 'favicon.ico':
939940
self._favicon = path
940941

942+
def get_asset_url(self, path):
943+
return _get_asset_path(
944+
self.config.requests_pathname_prefix,
945+
self.config.routes_pathname_prefix,
946+
path)
947+
941948
def run_server(self,
942949
port=8050,
943950
debug=False,

tests/test_configs.py

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# noinspection PyProtectedMember
33
from dash import _configs
44
from dash import exceptions as _exc
5+
from dash._utils import get_asset_path
56
import os
67

78

@@ -88,6 +89,21 @@ def test_pathname_prefix_environ_requests(self):
8889
_, routes, req = _configs.pathname_configs()
8990
self.assertEqual('/requests/', req)
9091

92+
def test_pathname_prefix_assets(self):
93+
req = '/'
94+
routes = '/'
95+
path = get_asset_path(req, routes, 'reset.css')
96+
self.assertEqual('/assets/reset.css', path)
97+
98+
req = '/requests/'
99+
path = get_asset_path(req, routes, 'reset.css')
100+
self.assertEqual('/requests/assets/reset.css', path)
101+
102+
req = '/requests/routes/'
103+
routes = '/routes/'
104+
path = get_asset_path(req, routes, 'reset.css')
105+
self.assertEqual('/requests/assets/reset.css', path)
106+
91107

92108
if __name__ == '__main__':
93109
unittest.main()

0 commit comments

Comments
 (0)