This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
forked from chkunkel/dash-bio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
174 lines (146 loc) · 5.28 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
import os
from config import DASH_APP_NAME
import base64
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import logging
from tests.dashbio_demos.utils.app_wrapper import app_page_layout
logging.getLogger("werkzeug").setLevel(logging.ERROR)
app = dash.Dash(__name__)
app.config["suppress_callback_exceptions"] = True
server = app.server
# sort apps alphabetically
appList = os.listdir(os.path.join("tests", "dashbio_demos"))
appList.sort()
apps = {
filename.replace(".py", "").replace("app_", ""): getattr(
getattr(
__import__(".".join(
["tests", "dashbio_demos", filename.replace(".py", "")])), "dashbio_demos"
),
filename.replace(".py", ""),
)
for filename in appList
if filename.startswith("app_") and filename.endswith(".py")
}
for key in apps:
apps[key].callbacks(app)
app.layout = html.Div(
id="index-waitfor",
children=[
dcc.Location(id="location"),
html.Div(id='gallery-header', children=[
html.Div(id='gallery-title', children=[
'Dash Bio'
]),
html.Div(id='gallery-subtitle', children=[
'A suite of bioinformatics components \
compatible with Plotly\'s Dash.'
]),
]),
html.Div(id="container")
]
)
def get_demo_app_img(name):
"""Get path to image corresponding to given app."""
pic_fname = './tests/dashbio_demos/images/pic_{}.png'.format(
name.replace('app_', '')
)
return pic_fname
def demo_app_name(name):
""" Returns a capitalized title for the app """
return name.replace('app_', '').replace('_', ' ').title()
def demo_app_desc(name):
""" Returns the content of the description specified in the app. """
desc = ''
try:
desc = apps[name].description()
except AttributeError:
pass
return desc
def demo_app_header_colors(name):
""" Returns the colors of the header specified in the app, if any. """
try:
return apps[name].header_colors()
except AttributeError:
return {}
def demo_app_link_id(name):
"""Returns the value of the id of the dcc.Link related to the demo app. """
return 'app-link-id-{}'.format(name.replace("_", "-"))
@app.callback(Output("container", "children"), [Input("location", "pathname")])
def display_app(pathname):
if pathname == '/{}'.format(DASH_APP_NAME) \
or pathname == '/{}/'.format(DASH_APP_NAME) \
or pathname == '/' or pathname is None:
return html.Div(
id='gallery-apps',
children=[
html.Div(className='gallery-app', children=[
dcc.Link(
children=[
html.Img(className='gallery-app-img',
src='data:image/png;base64,{}'.format(
base64.b64encode(open(get_demo_app_img(name),
'rb').read()).decode()
)), # base-64 encoded image
html.Div(className='gallery-app-info', children=[
html.Div(className='gallery-app-name', children=[
demo_app_name(name)
]),
html.Div(className='gallery-app-desc', children=[
demo_app_desc(name)
])
])
],
id=demo_app_link_id(name),
href="/{}/{}".format(
DASH_APP_NAME,
name.replace("app_", "").replace("_", "-")
)
)
]) for name in apps
])
app_name = \
pathname.replace(
'/{}/'.format(DASH_APP_NAME), '/').replace(
"/", "").replace("-", "_")
if app_name in apps:
return html.Div(id="waitfor",
children=app_page_layout(
apps[app_name].layout(),
app_title=demo_app_name(app_name),
app_name=app_name,
**demo_app_header_colors(app_name)
))
else:
return """
App not found.
You supplied "{}" and these are the apps that exist:
{}
""".format(
app_name, list(apps.keys())
)
@app.callback(
Output('gallery-header', 'children'),
[Input('location', 'pathname')]
)
def hide_header(pathname):
if pathname != '/{}'.format(DASH_APP_NAME) \
and pathname != '/{}/'.format(DASH_APP_NAME) \
and pathname != '/' and pathname is not None:
return []
return [
html.Div(id='gallery-title', children=[
'Dash Bio'
]),
html.Div(id='gallery-subtitle', children=[
'A suite of bioinformatics components \
compatible with Plotly\'s Dash.'
]),
]
app.css.config.serve_locally = True
app.scripts.config.serve_locally = True
if __name__ == "__main__":
app.run_server(debug=False)