forked from pinellolab/CRISPRme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
executable file
·180 lines (157 loc) · 5.62 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
"""The script creates the index page, constituting the back bone of CRISPRme web
interface. The web interface can also be created locally, to provide an easy-to-use
GUI to submit not released or private data and perform the off-targets search.
"""
from pages import (
main_page,
navbar_creation,
results_page,
load_page,
history_page,
help_page,
contacts_page
)
from app import app, URL, current_working_directory, cache
from utils import check_directories
from dash.dependencies import Input, Output, State
from typing import Tuple
import dash_core_components as dcc
import dash_html_components as html
import sys
import os
MODEFILE = ".mode_type.txt" # running mode report file
HOST = "0.0.0.0" # server host
PORTWEB = 80 # website port
PORTLOCAL = 8080 # local server port
# initialize the webpage
server = app.server # start server
navbar = navbar_creation.navbar() # create navigation bar on top of the webpage
# display multipage website
app.layout = html.Div(
[
navbar,
dcc.Location(id='url', refresh=False),
html.Div(id='page-content'),
html.P(id='signal', style={'visibility': 'hidden'})
]
)
# switch between the website pages
@app.callback(
[Output("page-content", "children"), Output("job-link", "children")],
[Input("url", "href"), Input("url", "pathname"), Input("url", "search")],
[State("url", "hash")]
)
def change_page(href: str, path: str, search: str, hash_guide: str) -> Tuple:
"""The function switches between the selected pages.
...
Parameters
----------
href : str
URL
path : str
Current path
search : str
Current search
hash_guide : str
Guide hash
Returns
-------
Tuple
"""
if not isinstance(href, str):
raise TypeError(f"Expected {str.__name__}, got {type(href).__name__}")
if not isinstance(path, str):
raise TypeError(f"Expected {str.__name__}, got {type(path).__name__}")
if not isinstance(search, str):
raise TypeError(f"Expected {str.__name__}, got {type(search).__name__}")
if not isinstance(hash_guide, str):
raise TypeError(f"Expected {str.__name__}, got {type(hash_guide).__name__}")
if path == "/load": # show loading page
return (
load_page.load_page(),
os.path.join("".join(href.split("/")[:-1]), "/load", search)
)
if path == "/result": # display results page
job_id = search.split("=")[-1]
if not hash_guide or hash_guide is None:
return results_page.result_page(job_id), os.path.join(URL, "load", search)
elif "new" in hash_guide: # TODO: change name to guide page
return (
results_page.guidePagev3(job_id, hash_guide.split("#")[1]),
os.path.join(URL, "load", search)
)
elif "-Sample-" in hash_guide:
return (
results_page.sample_page(job_id, hash_guide.split("#")[1]),
os.path.join(URL, "load", search)
)
elif "-Pos-" in hash_guide:
return (
results_page.cluster_page(job_id, hash_guide.split("#")[1]),
os.path.join(URL, "load", search)
)
return results_page.result_page(job_id), os.path.join(URL, "load", search)
if path == "/user-guide": # display manual page
return help_page.helpPage(), os.path.join(URL, "load", search)
if path == "/contacts": # display contacts page
return contacts_page.contact_page(), os.path.join(URL, "load", search)
if path == "/history": # display results history page
return history_page.history_page(), os.path.join(URL, "load", search)
if path == "/index": # display main page
return main_page.index_page(), "/index"
return main_page.index_page(), "/index"
def index():
"""The function creates the index page, managing the whole CRISPRme web
interface. The webpage displays four main pages (accessible by the user):
- Home page
- Manual page
- Contacts page
- History page (accessible only locally)
The webpage can be created locally, using the appropriate command line
arguments.
...
Parameters
----------
None
Returns
-------
None
"""
# check CRISPRme directory tree consistency
check_directories(current_working_directory)
# check if debug mode is active
# TODO: replace using argparse in crisprme.py
debug = False
if "--debug" in sys.argv[1:]:
debug = True
# check if local server or website
website = False
if "--website" in sys.argv[1:]:
website = True
# keep track of running mode
try:
handle = open(os.path.join(current_working_directory, MODEFILE), mode="w")
if website: # 'server' mode
handle.write("server")
else: # 'local' mode
handle.write("local")
except OSError:
raise OSError(f"An error occurred while writing {MODEFILE}")
finally:
handle.close()
if website:
app.run_server(
host=HOST, port=PORTWEB, debug=debug, dev_tools_ui=debug, dev_tools_props_check=debug
)
cache.clear() # clear cache once server is closed
else: # local web-interface running
app.run_server(
host=HOST, port=PORTLOCAL, debug=debug, dev_tools_ui=debug, dev_tools_props_check=debug
)
app.run_server(
host=HOST, port=PORTWEB, debug=debug, dev_tools_ui=debug, dev_tools_props_check=debug
)
cache.clear() # clear cache once server is closed
if __name__ == "__main__":
index()