Skip to content

Commit 2fed18b

Browse files
committed
infer app instance from type
1 parent db1dc8d commit 2fed18b

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

dash/_cli.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,30 @@ def load_app(app_path: str) -> Dash:
1313
:param app_path: The import path to the Dash app instance.
1414
:return: The loaded Dash app instance.
1515
"""
16-
if ":" not in app_path:
17-
raise ValueError(
18-
f"Invalid app path: '{app_path}'. "
19-
'The path must be in the format "module:variable".'
20-
)
16+
app_split = app_path.split(":")
17+
module_str = app_split[0]
2118

22-
module_str, app_str = app_path.split(":", 1)
23-
24-
if not module_str or not app_str:
25-
raise ValueError(
26-
f"Invalid app path: '{app_path}'. "
27-
'Both module and variable names are required in "module:variable".'
28-
)
19+
if not module_str:
20+
raise ValueError(f"Invalid app path: '{app_path}'. ")
2921

3022
try:
3123
module = importlib.import_module(module_str)
3224
except ImportError as e:
3325
raise ImportError(f"Could not import module '{module_str}'.") from e
3426

35-
try:
36-
app_instance = getattr(module, app_str)
37-
except AttributeError as e:
38-
raise AttributeError(
39-
f"Could not find variable '{app_str}' in module '{module_str}'."
40-
) from e
27+
if len(app_split) == 2:
28+
app_str = app_split[1]
29+
try:
30+
app_instance = getattr(module, app_str)
31+
except AttributeError as e:
32+
raise AttributeError(
33+
f"Could not find variable '{app_str}' in module '{module_str}'."
34+
) from e
35+
else:
36+
for module_var in vars(module).values():
37+
if isinstance(module_var, Dash):
38+
app_instance = module_var
39+
break
4140

4241
if not isinstance(app_instance, Dash):
4342
raise TypeError(f"'{app_path}' did not resolve to a Dash app instance.")
@@ -60,7 +59,9 @@ def create_parser() -> argparse.ArgumentParser:
6059
)
6160

6261
run_parser.add_argument(
63-
"app", help='The Dash app to run, in the format "module:variable".'
62+
"app",
63+
help='The Dash app to run, in the format "module:variable" '
64+
'or just "module" to find the app instance automatically. (eg: plotly run app)',
6465
)
6566

6667
# Server options

0 commit comments

Comments
 (0)