Skip to content

Commit 8cbbc74

Browse files
committed
Add function to run all tabs
1 parent 9b6afd3 commit 8cbbc74

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

mlip_testing/app/run_app.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""Run main application."""
2+
3+
from __future__ import annotations
4+
5+
from importlib import import_module
6+
import os
7+
from pathlib import Path
8+
9+
from dash import Dash
10+
from dash.html import Div
11+
12+
from mlip_testing import app
13+
14+
DATA_PATH = Path(__file__).parent / "data"
15+
16+
17+
def build_tabs(full_app: Dash) -> list[Div]:
18+
"""
19+
Get layout and register callbacks for all tab applications.
20+
21+
Parameters
22+
----------
23+
full_app
24+
Full application with all sub-apps.
25+
26+
Returns
27+
-------
28+
list[Div]
29+
Layouts for all tabs.
30+
"""
31+
# Find Python files e.g. app_OC157.py in mlip_tesing.app module.
32+
tabs = Path(app.__file__).parent.glob("*/app*.py")
33+
layout = []
34+
35+
# Build all layouts, and register all callbacks to main app.
36+
for tab in tabs:
37+
tab_name = (tab.parent).name
38+
tab_app = import_module(f"mlip_testing.app.{tab_name}.app_{tab_name}")
39+
layout.append(tab_app.build_layout())
40+
tab_app.register_callbacks(full_app)
41+
42+
return layout
43+
44+
45+
def build_full_app(full_app: Dash):
46+
"""
47+
Build full app layout and register callbacks.
48+
49+
Parameters
50+
----------
51+
full_app
52+
Full application with all sub-apps.
53+
"""
54+
full_app.layout = build_tabs(full_app)
55+
56+
57+
if __name__ == "__main__":
58+
port = int(os.environ.get("PORT", 8050))
59+
60+
full_app = Dash(__name__, assets_folder=DATA_PATH)
61+
build_full_app(full_app)
62+
63+
print(f"Starting Dash app on port {port}...")
64+
full_app.run(host="0.0.0.0", port=port)

0 commit comments

Comments
 (0)