Skip to content

Commit

Permalink
Merge pull request #547 from plotly/fix-asset-paths
Browse files Browse the repository at this point in the history
Fix assets blueprint path management
  • Loading branch information
T4rk1n authored Jan 23, 2019
2 parents e72f483 + 5abb1d4 commit 534d285
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## [UNRELEASED] - 2019-01-23
## Fixed
- Asset blueprint takes routes prefix into it's static path. [#547](https://github.com/plotly/dash/pull/547)
- Asset url path no longer strip routes from requests. [#547](https://github.com/plotly/dash/pull/547)

## Changed
- `assets_folder` argument now default to 'assets' [#547](https://github.com/plotly/dash/pull/547)
- The assets folder is now always relative to the given root path of `name` argument, the default of `__main__` will get the `cwd`. [#547](https://github.com/plotly/dash/pull/547)
- No longer coerce the name argument from the server if the server argument is provided. [#547](https://github.com/plotly/dash/pull/547)

## [0.35.2] - 2019-01-11
## Fixed
- Fix typo in some exception names [#522](https://github.com/plotly/dash/pull/522)
Expand Down
5 changes: 1 addition & 4 deletions dash/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ def generate_hash():

def get_asset_path(
requests_pathname,
routes_pathname,
asset_path,
asset_url_path):
i = requests_pathname.rfind(routes_pathname)
req = requests_pathname[:i]

return '/'.join([
# Only take the first part of the pathname
req,
requests_pathname.rstrip('/'),
asset_url_path,
asset_path
])
Expand Down
31 changes: 20 additions & 11 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(
name='__main__',
server=None,
static_folder='static',
assets_folder=None,
assets_folder='assets',
assets_url_path='/assets',
assets_ignore='',
include_assets_files=True,
Expand All @@ -102,21 +102,15 @@ def __init__(
See https://github.com/plotly/dash/issues/141 for details.
''', DeprecationWarning)

name = name if server is None else server.name
self._assets_folder = assets_folder or os.path.join(
flask.helpers.get_root_path(name), 'assets'
self._assets_folder = os.path.join(
flask.helpers.get_root_path(name),
assets_folder,
)
self._assets_url_path = assets_url_path

# allow users to supply their own flask server
self.server = server or Flask(name, static_folder=static_folder)

if 'assets' not in self.server.blueprints:
self.server.register_blueprint(
flask.Blueprint('assets', 'assets',
static_folder=self._assets_folder,
static_url_path=assets_url_path))

env_configs = _configs.env_configs()

url_base_pathname, routes_pathname_prefix, requests_pathname_prefix = \
Expand Down Expand Up @@ -146,6 +140,22 @@ def __init__(
env_configs, 2678400))
})

assets_blueprint_name = '{}{}'.format(
self.config.routes_pathname_prefix.replace('/', '_'),
'dash_assets'
)

self.server.register_blueprint(
flask.Blueprint(
assets_blueprint_name, name,
static_folder=self._assets_folder,
static_url_path='{}{}'.format(
self.config.routes_pathname_prefix,
assets_url_path.lstrip('/')
)
)
)

# list of dependencies
self.callback_map = {}

Expand Down Expand Up @@ -1056,7 +1066,6 @@ def _serve_default_favicon(self):
def get_asset_url(self, path):
asset = _get_asset_path(
self.config.requests_pathname_prefix,
self.config.routes_pathname_prefix,
path,
self._assets_url_path.lstrip('/')
)
Expand Down
10 changes: 4 additions & 6 deletions tests/test_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,16 @@ def test_pathname_prefix_environ_requests(self):

def test_pathname_prefix_assets(self):
req = '/'
routes = '/'
path = get_asset_path(req, routes, 'reset.css', 'assets')
path = get_asset_path(req, 'reset.css', 'assets')
self.assertEqual('/assets/reset.css', path)

req = '/requests/'
path = get_asset_path(req, routes, 'reset.css', 'assets')
path = get_asset_path(req, 'reset.css', 'assets')
self.assertEqual('/requests/assets/reset.css', path)

req = '/requests/routes/'
routes = '/routes/'
path = get_asset_path(req, routes, 'reset.css', 'assets')
self.assertEqual('/requests/assets/reset.css', path)
path = get_asset_path(req, 'reset.css', 'assets')
self.assertEqual('/requests/routes/assets/reset.css', path)


if __name__ == '__main__':
Expand Down
1 change: 0 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ def test_index_customization(self):

def test_assets(self):
app = dash.Dash(__name__,
assets_folder='tests/assets',
assets_ignore='.*ignored.*')
app.index_string = '''
<!DOCTYPE html>
Expand Down

0 comments on commit 534d285

Please sign in to comment.