-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo_interface.py
287 lines (266 loc) · 10.9 KB
/
demo_interface.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright 2024 D-Wave
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This file stores the Dash HTML layout for the app."""
from __future__ import annotations
from dash import dcc, html
from demo_configs import (
DESCRIPTION,
HOSTS,
MAIN_HEADER,
SOLVER_TIME,
THEME_COLOR_SECONDARY,
THUMBNAIL,
VMS,
)
from src.demo_enums import PriorityType
def slider(label: str, id: str, config: dict) -> html.Div:
"""Slider element for value selection.
Args:
label: The title that goes above the slider.
id: A unique selector for this element.
config: A dictionary of slider configerations, see dcc.Slider Dash docs.
"""
return html.Div(
className="slider-wrapper",
children=[
html.Label(label),
dcc.Slider(
id=id,
className="slider",
**config,
marks={
config["min"]: str(config["min"]),
config["max"]: str(config["max"]),
},
tooltip={
"placement": "bottom",
"always_visible": True,
},
),
],
)
def radio(label: str, id: str, options: list, value: int, inline: bool = True) -> html.Div:
"""Radio element for option selection.
Args:
label: The title that goes above the radio.
id: A unique selector for this element.
options: A list of dictionaries of labels and values.
value: The value of the radio that should be preselected.
inline: Whether the options are displayed beside or below each other.
"""
return html.Div(
className="radio-wrapper",
children=[
html.Label(label),
dcc.RadioItems(
id=id,
className=f"radio{' radio--inline' if inline else ''}",
inline=inline,
options=options,
value=value,
),
],
)
def generate_settings_form() -> html.Div:
"""This function generates settings for selecting the scenario.
Returns:
html.Div: A Div containing the settings for selecting the scenario.
"""
priority_options = [
{"label": priority.label, "value": priority.value} for priority in PriorityType
]
return html.Div(
className="settings",
children=[
slider(
"Number of Virtual Machines",
"vms",
VMS,
),
slider(
"Number of Hosts",
"hosts",
HOSTS,
),
radio(
"Balance Priority",
"priority",
sorted(priority_options, key=lambda op: op["value"]),
0,
),
html.Label("Solver Time Limit (Seconds)"),
dcc.Input(
id="solver-time-limit",
type="number",
**SOLVER_TIME,
),
],
)
def generate_run_buttons() -> html.Div:
"""Run and cancel buttons to run the optimization."""
return html.Div(
id="button-group",
children=[
html.Button(id="run-button", children="Run Optimization", n_clicks=0, disabled=False),
html.Button(
id="cancel-button",
children="Cancel Optimization",
n_clicks=0,
className="display-none",
),
],
)
def generate_graph(index: int) -> html.Div:
"""Generates a graph with a zoom button.
Args:
index: A unit integer to identify the graph by.
Returns:
html.Div: A div containing a graph and magnifying button.
"""
return html.Div(
children=[
html.Div(
[
html.Div([html.Span("+"), html.Span("-")], className="magnifying-lens"),
html.Div(className="magnifying-handle"),
],
className="magnifying",
id={"type": "magnifying", "index": index},
),
dcc.Graph(
id={"type": "graph", "index": index},
responsive=True,
config={"displayModeBar": False},
className="graph-element",
),
],
)
def create_interface():
"""Set the application HTML."""
return html.Div(
id="app-container",
children=[
# Below are any temporary storage items, e.g., for sharing data between callbacks.
dcc.Store(id="vms-store"),
dcc.Store(id="hosts-store"),
# Header brand banner
html.Div(className="banner", children=[html.Img(src=THUMBNAIL)]),
# Settings and results columns
html.Div(
className="columns-main",
children=[
# Left column
html.Div(
id={"type": "to-collapse-class", "index": 0},
className="left-column",
children=[
html.Div(
className="left-column-layer-1", # Fixed width Div to collapse
children=[
html.Div(
className="left-column-layer-2", # Padding and content wrapper
children=[
html.H1(MAIN_HEADER),
html.P(DESCRIPTION),
generate_settings_form(),
generate_run_buttons(),
],
)
],
),
# Left column collapse button
html.Div(
html.Button(
id={"type": "collapse-trigger", "index": 0},
className="left-column-collapse",
children=[html.Div(className="collapse-arrow")],
),
),
],
),
# Right column
html.Div(
className="right-column",
children=[
dcc.Tabs(
id="tabs",
value="input-tab",
mobile_breakpoint=0,
children=[
dcc.Tab(
label="Current State",
id="input-tab",
value="input-tab", # used for switching tabs programatically
className="tab",
children=[
dcc.Loading(
parent_className="input",
type="circle",
color=THEME_COLOR_SECONDARY,
children=[
html.Div(
[
generate_graph(0),
generate_graph(1),
],
className="graph-wrapper",
),
html.Div(
[
generate_graph(2),
generate_graph(3),
],
className="graph-wrapper",
),
],
),
],
),
dcc.Tab(
label="Updated State",
id="results-tab",
className="tab",
disabled=True,
children=[
dcc.Loading(
parent_className="results",
type="circle",
color=THEME_COLOR_SECONDARY,
children=[
html.Div(
[
generate_graph(4),
generate_graph(5),
],
className="graph-wrapper",
),
html.Div(
[
generate_graph(6),
generate_graph(7),
],
className="graph-wrapper",
),
],
),
],
),
],
)
],
),
],
),
],
)