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

[BUG] Meta tags don't work for Dash pages when app is integrated into Flask app #2790

Open
antonymilne opened this issue Mar 12, 2024 · 1 comment

Comments

@antonymilne
Copy link

antonymilne commented Mar 12, 2024

Describe your context

Please provide us your environment, so we can easily reproduce the issue.

dash                      2.16.1
dash_ag_grid              31.0.1
dash-bootstrap-components 1.5.0
dash-core-components      2.0.0
dash-html-components      2.0.0
dash-mantine-components   0.12.1
dash-table                5.0.0

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

import dash
import flask

server = flask.Flask(__name__)

app = dash.Dash(server=server, routes_pathname_prefix="/dash/", use_pages=True, pages_folder=""")
dash.register_page("page", layout=dash.html.P("Hello, Dash!"))
app.layout = dash.page_container
app.run()

Go to http://localhost:8050/dash/page. The page renders ok but if you check the source then the meta tags have no content:

<meta property="twitter:image" content="">

Method 2

import dash
import flask
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple

server = flask.Flask(__name__)

app = dash.Dash(requests_pathname_prefix="/dash/", use_pages=True, pages_folder="")
dash.register_page("page", layout=html.P("Hello, Dash!"))
app.layout = dash.page_container
application = DispatcherMiddleware(server, {"/dash": app.server})
run_simple("localhost", 8050, application)

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:

<meta property="twitter:image" content="http://127.0.0.1:8050/dash/dash/assets/app.svg">

Expected behaviour

In both the above cases, the meta tag should be as follows:

<meta property="twitter:image" content="http://127.0.0.1:8050/dash/assets/app.svg">

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 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"]:

start_page, path_variables = _path_to_page(flask.request.path.strip("/"))

Suggested fix: use flask.request.view_args["path"] instead, which gives just page.

For Method 2, the call to app.get_asset_url already includes the requests_pathname_prefix, as does flask.request.url_root, and so you end up with this being repeated:

"".join([flask.request.url_root, image.lstrip("/")]) if image else None

Suggested fix: use flask.request.host_url instead, which gives just http://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.

@Coding-with-Adam
Copy link

Coding-with-Adam commented Mar 15, 2024

hi @antonymilne ,
yes, we agree that it's a small bug. If you'd create the PR, we would be happy to review it. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants