-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.py
61 lines (49 loc) · 1.71 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import importlib
import os
import pathlib
from dash import dcc, html
from dash.dependencies import Input, Output
from app import app
from apps.contents import contents
# expose the Flask instance for the HTTP server (gunicorn)
server = app.server
# create a pathlist for the various dash apps
file_path = pathlib.Path(__file__).parent / 'apps'
# Scan through the apps directory and find all the dash apps
projlist = []
modlist = []
for dir_, _, files in os.walk(file_path):
for file_name in files:
if file_name.endswith(".py"):
if file_name.endswith("__init__.py"):
pass
elif file_name.endswith("Ocean_Data_Loader.py"):
pass
# add them to the lists
elif file_name.endswith(".py"):
rel_dir = os.path.relpath(dir_, file_path)
projlist.append(rel_dir)
name = file_name[:-3]
modlist.append(name)
# Import app modules and map them to URL paths
path_module_mapping = {}
for i in range(len(projlist)):
path_module_mapping['/apps/' + projlist[i] + '/' + modlist[i]] = importlib.import_module('apps.' + projlist[i] + '.' + modlist[i])
# Build the layout of the index page
app.layout = html.Div([
dcc.Location(id='url', refresh=False),
html.Div(id='page-content')
])
@app.callback(Output('page-content', 'children'),
Input('url', 'pathname'))
def display_page(pathname):
if pathname == '/':
return contents.layout
try:
module = path_module_mapping[pathname]
return module.layout
except KeyError:
return '404'
if __name__ == '__main__':
# Start the Flask server for local development
app.run_server(debug=True)