Skip to content

Commit c11062e

Browse files
committed
Fix use_pages in Jupyter where __main__ has no __file__
Fixes #3132
1 parent d97a508 commit c11062e

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
55
## [UNRELEASED]
66

77
### Fixed
8+
- [#4196](https://github.com/plotly/dash/pull/4196) Fix `use_pages=True` crashing with `AttributeError` in Jupyter, where the `__main__` module has no `__file__`. Fixes [#3132](https://github.com/plotly/dash/issues/3132).
89
- [#4196](https://github.com/plotly/dash/pull/4196) Fix pages `redirect_from` returning 404 behind a proxy that strips `requests_pathname_prefix` (Dash Enterprise workspaces), and redirecting to a doubled prefix with `url_base_pathname`. Fixes [#3549](https://github.com/plotly/dash/issues/3549).
910
- [#4167](https://github.com/plotly/dash/pull/4167) `dash-generate-components` no longer crashes when generating an R package from a `package.json` whose `author` is an object or includes a URL, and a missing author email now gets the intended error message. Fixes [#1577](https://github.com/plotly/dash/issues/1577).
1011
- [#4169](https://github.com/plotly/dash/pull/4169) Fix the inferred path of a page whose module name contains the pages folder name: `pages/landing_pages.py` no longer gets `/` and `pages/subpages/report.py` no longer gets `/report`. Fixes [#4168](https://github.com/plotly/dash/issues/4168).

‎dash/_pages.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def _infer_path(module_name, template):
8888
def _module_name_is_package(module_name):
8989
if module_name not in sys.modules:
9090
return False
91-
file = sys.modules[module_name].__file__
92-
return file and file.endswith("__init__.py")
91+
file = getattr(sys.modules[module_name], "__file__", None)
92+
return bool(file) and file.endswith("__init__.py")
9393

9494

9595
def _path_to_module_name(path):

‎tests/integration/multi_page/test_pages_layout.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import sys
2+
import types
3+
14
import pytest
25
import dash
36
from dash import Dash, Input, State, dcc, html, Output
@@ -295,3 +298,23 @@ def test_pala007_app_title_discription(dash_duo, clear_pages_state):
295298
assert dash.page_registry["page1"]["title"] == "Page 1 Title"
296299
assert dash.page_registry["home"]["description"] == "App Description"
297300
assert dash.page_registry["page1"]["description"] == "Page 1 Description"
301+
302+
303+
def test_pala008_main_module_without_file(
304+
dash_duo, clear_pages_state, tmp_path, monkeypatch
305+
):
306+
# Jupyter runs the app in a __main__ module that has no __file__.
307+
monkeypatch.setitem(sys.modules, "__main__", types.ModuleType("__main__"))
308+
pages_folder = tmp_path / "pages"
309+
pages_folder.mkdir()
310+
(pages_folder / "home.py").write_text(
311+
"import dash\n"
312+
"from dash import html\n"
313+
"dash.register_page(__name__, path='/')\n"
314+
"layout = html.Div('home', id='home')\n"
315+
)
316+
317+
app = Dash("__main__", use_pages=True, pages_folder=str(pages_folder))
318+
app.layout = dash.page_container
319+
dash_duo.start_server(app)
320+
dash_duo.wait_for_text_to_equal("#home", "home")

0 commit comments

Comments
 (0)