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

removed deprecated before_first_request #2265

Merged
merged 5 commits into from
Oct 17, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed

- [#2265](https://github.com/plotly/dash/pull/2265) Removed deprecated `before_first_request` as reported in [#2177](https://github.com/plotly/dash/issues/2177).
- [#2257](https://github.com/plotly/dash/pull/2257) Fix tuple types in the TypeScript component generator.

## [2.6.2] - 2022-09-23
Expand Down
23 changes: 22 additions & 1 deletion dash/_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
from urllib.parse import parse_qs
from fnmatch import fnmatch
import re
import flask

from . import _validate
from ._utils import AttributeDict
from ._get_paths import get_relative_path
from ._callback_context import context_value
from ._get_app import get_app


CONFIG = AttributeDict()
Expand Down Expand Up @@ -118,6 +120,25 @@ def _parse_path_variables(pathname, path_template):
return dict(zip(var_names, variables))


def _create_redirect_function(redirect_to):
def redirect():
return flask.redirect(redirect_to, code=301)

return redirect


def _set_redirect(redirect_from, path):
app = get_app()
if redirect_from and len(redirect_from):
for redirect in redirect_from:
fullname = app.get_relative_path(redirect)
app.server.add_url_rule(
fullname,
fullname,
_create_redirect_function(app.get_relative_path(path)),
)


def register_page(
module,
path=None,
Expand Down Expand Up @@ -280,7 +301,7 @@ def register_page(
image=(image if image is not None else _infer_image(module)),
image_url=image_url,
)
page.update(redirect_from=redirect_from)
page.update(redirect_from=_set_redirect(redirect_from, page["path"]))

PAGE_REGISTRY[module] = page

Expand Down
33 changes: 13 additions & 20 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ def __init__( # pylint: disable=too-many-statements
for plugin in plugins:
plugin.plug(self)

# tracks internally if a function already handled at least one request.
self._got_first_request = {"pages": False, "setup_server": False}

if self.server is not None:
self.init_app()

Expand Down Expand Up @@ -542,7 +545,7 @@ def _handle_error(_):
"""Handle a halted callback and return an empty 204 response."""
return "", 204

self.server.before_first_request(self._setup_server)
self.server.before_request(self._setup_server)

# add a handler for components suites errors to return 404
self.server.errorhandler(InvalidResourceError)(self._invalid_resources_handler)
Expand Down Expand Up @@ -1271,6 +1274,10 @@ def dispatch(self):
return response

def _setup_server(self):
if self._got_first_request["setup_server"]:
return
self._got_first_request["setup_server"] = True

# Apply _force_eager_loading overrides from modules
eager_loading = self.config.eager_loading
for module_name in ComponentRegistry.registry:
Expand Down Expand Up @@ -2027,8 +2034,12 @@ def enable_pages(self):
if self.pages_folder:
self._import_layouts_from_pages()

@self.server.before_first_request
@self.server.before_request
def router():
if self._got_first_request["pages"]:
return
self._got_first_request["pages"] = True

@self.callback(
Output(_ID_CONTENT, "children"),
Output(_ID_STORE, "data"),
Expand Down Expand Up @@ -2107,24 +2118,6 @@ def update(pathname, search):
Input(_ID_STORE, "data"),
)

def create_redirect_function(redirect_to):
def redirect():
return flask.redirect(redirect_to, code=301)

return redirect

# Set redirects
for module in _pages.PAGE_REGISTRY:
page = _pages.PAGE_REGISTRY[module]
if page["redirect_from"] and len(page["redirect_from"]):
for redirect in page["redirect_from"]:
fullname = self.get_relative_path(redirect)
self.server.add_url_rule(
fullname,
fullname,
create_redirect_function(page["relative_path"]),
)

def run_server(self, *args, **kwargs):
"""`run_server` is a deprecated alias of `run` and may be removed in a
future version. We recommend using `app.run` instead.
Expand Down