When integrating a Dash app that uses Dash pages into a Flask app, both methods outlined here result in (differently) incorrect behaviour of the page meta tags.
To reproduce, create a file assets/app.svg and then run the following minimal apps.
Go to http://localhost:8050/dash/page. The page renders ok but if you check the source then the meta tags point to the wrong place - note the doubled up dash:
For Method 1, the code in _path_to_page cannot find the right page and so returns an empty dictionary. The reason for this is that flask.request.path.strip("/") returns dash/page rather than just page, which is the value to match against in the page registry page["path"]:
https://github.com/plotly/dash/blob/78ebf0db5f297989b4b07ec004a3c7f9e11ee412/dash/_pages.py#L386
Suggested fix: use flask.request.view_args["path"] instead, which gives just page.
These suggested fixes come about just from inspecting flask.request and looking for a suitable string. They work on the above two examples but I haven't carefully tested them, so there might be other cases that they don't solve (or worse, cases that currently work but the suggested solutions break). Hopefully someone who knows more about flask routing than me can comment on whether these are the right fixes.
This is a very small bug so I guess will not be high priority to fix, although the fix should also be quick and easy. I'd be happy to raise a PR if the above suggestions seem correct or if someone makes a better suggestion.
Describe your context
Please provide us your environment, so we can easily reproduce the issue.
Describe the bug
When integrating a Dash app that uses Dash pages into a Flask app, both methods outlined here result in (differently) incorrect behaviour of the page meta tags.
To reproduce, create a file
assets/app.svg
and then run the following minimal apps.Method 1
Go to http://localhost:8050/dash/page. The page renders ok but if you check the source then the meta tags have no content:
Method 2
Go to http://localhost:8050/dash/page. The page renders ok but if you check the source then the meta tags point to the wrong place - note the doubled up
dash
:Expected behaviour
In both the above cases, the meta tag should be as follows:
Source of problem
For Method 1, the code in
_path_to_page
cannot find the right page and so returns an empty dictionary. The reason for this is thatflask.request.path.strip("/")
returnsdash/page
rather than justpage
, which is the value to match against in the page registrypage["path"]
: https://github.com/plotly/dash/blob/78ebf0db5f297989b4b07ec004a3c7f9e11ee412/dash/_pages.py#L386 Suggested fix: useflask.request.view_args["path"]
instead, which gives justpage
.For Method 2, the call to
app.get_asset_url
already includes therequests_pathname_prefix
, as doesflask.request.url_root
, and so you end up with this being repeated: https://github.com/plotly/dash/blob/78ebf0db5f297989b4b07ec004a3c7f9e11ee412/dash/_pages.py#L393 Suggested fix: useflask.request.host_url
instead, which gives justhttp://localhost:8050
.These suggested fixes come about just from inspecting
flask.request
and looking for a suitable string. They work on the above two examples but I haven't carefully tested them, so there might be other cases that they don't solve (or worse, cases that currently work but the suggested solutions break). Hopefully someone who knows more about flask routing than me can comment on whether these are the right fixes.This is a very small bug so I guess will not be high priority to fix, although the fix should also be quick and easy. I'd be happy to raise a PR if the above suggestions seem correct or if someone makes a better suggestion.