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

Adds ability to redirect "/" when using pages #2126

Merged
merged 4 commits into from
Jul 12, 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 @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- [#2109](https://github.com/plotly/dash/pull/2109) Add `maxHeight` to Dropdown options menu.

### Fixed
- [#2126](https://github.com/plotly/dash/pull/2126) Fix bug where it was not possible to redirect from root when using pages.
- [#2114](https://github.com/plotly/dash/pull/2114) Fix bug [#1978](https://github.com/plotly/dash/issues/1978) where text could not be copied from cells in tables with `cell_selectable=False`.
- [#2102](https://github.com/plotly/dash/pull/2102) Fix bug as reported in [dash-labs #113](https://github.com/plotly/dash-labs/issues/113) where files starting with `.` were not excluded when building `dash.page_registry`.
- [#2100](https://github.com/plotly/dash/pull/2100) Fixes bug where module name in for a custom `not_found_404` page is incorrect in the `dash.page_registry` when not using the `pages` folder.
Expand Down
8 changes: 7 additions & 1 deletion dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ def _handle_error(_):
self._add_url("_dash-update-component", self.dispatch, ["POST"])
self._add_url("_reload-hash", self.serve_reload_hash)
self._add_url("_favicon.ico", self._serve_default_favicon)
self._add_url("", self.index)
if not self.use_pages:
self._add_url("", self.index)

# catch-all for front-end routes, used by dcc.Location
self._add_url("<path:path>", self.index)
Expand Down Expand Up @@ -2118,6 +2119,11 @@ def redirect():
fullname,
create_redirect_function(page["relative_path"]),
)
# set "/" if not redirected
try:
self._add_url("", self.index)
except AssertionError:
pass

def run_server(self, *args, **kwargs):
"""`run_server` is a deprecated alias of `run` and may be removed in a
Expand Down
30 changes: 30 additions & 0 deletions tests/integration/test_pages_redirect_home.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import dash


def test_pare001_redirect_home(dash_duo):

app = dash.Dash(__name__, use_pages=True, pages_folder="")

dash.register_page(
"multi_layout1",
layout=dash.html.Div("text for multi_layout1", id="text_multi_layout1"),
path="/",
)

dash.register_page(
"redirect_home",
redirect_from=["/"],
layout=dash.html.Div("Redirect", id="redirect"),
)

app.layout = dash.page_container

dash_duo.start_server(app)

dash_duo.wait_for_page(url=f"http://localhost:{dash_duo.server.port}/redirect-home")
dash_duo.wait_for_text_to_equal("#redirect", "Redirect")

assert dash_duo.get_logs() == [], "browser console should contain no error"

# clean up after this test, so this redirect does not affect other pages tests
del dash.page_registry["redirect_home"]