-
Notifications
You must be signed in to change notification settings - Fork 5
/
Outputs.tsx
336 lines (315 loc) · 10.3 KB
/
Outputs.tsx
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import * as React from "react";
import ReactLoading from "react-loading";
import {
Card,
Row,
Col,
OverlayTrigger,
Tooltip,
Modal,
Button,
ProgressBar,
} from "react-bootstrap";
import * as moment from "moment";
import { RemoteOutputs, Outputs, Simulation, Output, TableOutput, BokehOutput } from "../types";
import { imgDims } from "../utils";
import API from "./API";
import { CheckboxWidget } from "./notify";
import { RolePerms } from "../roles";
import { lt as semverlt } from "semver";
interface OutputsProps {
api: API;
remoteSim?: Simulation<RemoteOutputs>;
sim?: Simulation<Outputs>;
setNotifyOnCompletion?: (notify: boolean) => void;
}
type OutputsState = Readonly<{}>;
const TableComponent: React.FC<{ output: TableOutput }> = ({ output }) => (
<div
dangerouslySetInnerHTML={{ __html: output.data }} // needs to be sanitized somehow.
className="card publish markdown"
/>
);
const BokehComponent: React.FC<{ output: BokehOutput }> = ({ output }) => {
// @ts-ignore
let bokeh = window.Bokeh200;
// @ts-ignore
if (semverlt(output.data.doc.version, "2.0.0")) {
// @ts-ignore
bokeh = window.Bokeh140;
}
console.log("bokeh", bokeh);
// @ts-ignore
bokeh.embed.embed_item(output.data, output.id);
return <div id={output.id} data-root-id={output.id} className="bk-root"></div>;
};
const OutputModal: React.FC<{
output: Output | BokehOutput | TableOutput;
children: JSX.Element;
}> = ({ output, children }) => {
const [show, setShow] = React.useState(false);
React.useEffect(() => {
// @ts-ignore
if (show) const res = window.MathJax.typeset();
}, [show]);
let el;
switch (output.media_type) {
case "table":
el = <TableComponent output={output as TableOutput} />;
break;
case "bokeh":
console.log("bokeh", output);
el = <BokehComponent output={output as BokehOutput} />;
break;
case "PNG":
el = <img src={`data:image/png;base64, ${output.data}`} />;
break;
case "JPEG":
el = <img src={`data:image/jpeg;base64, ${output.data}`} />;
break;
default:
el = <div dangerouslySetInnerHTML={{ __html: output.data }} />;
}
return (
<>
<Button variant="outline-light" style={{ border: 0 }} onClick={() => setShow(true)}>
{children}
</Button>
<Modal show={show} onHide={() => setShow(false)} size="xl" className="output-modal">
<Modal.Header closeButton>
<Modal.Title className={"w-100"} style={{ textAlign: "center" }}>
{output.title}
</Modal.Title>
</Modal.Header>
<Modal.Body>
<Card style={{ backgroundColor: "white" }}>
<Card.Body
className={`d-flex ${
window.innerWidth < 992 ? "justify-content-left" : "justify-content-center"
}`}
style={{ overflow: "auto" }}
>
{el}
</Card.Body>
</Card>
</Modal.Body>
<Modal.Footer>
<Button variant="outline-primary" onClick={() => setShow(false)}>
Close
</Button>
</Modal.Footer>
</Modal>
</>
);
};
const Pending: React.FC<{
eta?: number;
originalEta?: number;
notify?: boolean;
setNotify?: (notify: boolean) => void;
showNotify?: boolean;
}> = ({ eta, originalEta, notify, setNotify, showNotify }) => {
let el;
if (eta !== null && originalEta !== null) {
let percent = 100 * (1 - eta / originalEta);
el = (
<div>
<Card.Title>
<h3 className="text-center">
Estimated time remaining: {moment.duration(eta, "seconds").humanize()}
</h3>
{showNotify ? (
<div className="text-center">
<CheckboxWidget value={notify} setValue={setNotify} message="Email me when ready." />
</div>
) : null}
</Card.Title>
<ProgressBar
className="mt-5 mb-5"
now={percent}
style={{ height: "1.8rem" }}
label={`${percent}%`}
srOnly
animated
/>
</div>
);
} else {
el = (
<div className="d-flex justify-content-center">
<ReactLoading type="spokes" color="#2b2c2d" />
</div>
);
}
return (
<Card className="card-outer">
<Card className="card-inner">
<Card.Body>{el}</Card.Body>
</Card>
</Card>
);
};
const Traceback: React.FC<{ remoteSim: Simulation<RemoteOutputs> }> = ({ remoteSim }) => (
<Card className="card-outer">
<Card className="card-inner">
<Card.Body>
<Card.Title>
<h2>Your calculation failed. You may re-enter your parameters and try again.</h2>
</Card.Title>
<p className="lead">
Compute Studio developers have already been notified about this failure. You are welcome
to email me at <a href="mailto:hank@compute.studio">hank@compute.studio</a> if you would
like to get in touch about this error.
</p>
<h4>Traceback:</h4>
<pre>
<code>{remoteSim.traceback}</code>
</pre>
</Card.Body>
</Card>
</Card>
);
const NewSimulation: React.FC<{}> = () => (
<Card className="card-outer">
<Card className="card-inner">
<Card.Body>
<Card.Title>
<h2>You have not run your simulation yet.</h2>
</Card.Title>
</Card.Body>
</Card>
</Card>
);
const V0Simulation: React.FC<{ remoteSim: Simulation<RemoteOutputs> }> = ({ remoteSim }) => {
let project = remoteSim.project;
let { model_version, gui_url } = remoteSim;
let creation_date = moment(remoteSim.creation_date).format("MMMM Do YYYY, h:mm:ss a");
return (
<Card>
<Card.Body>
<p>
These results were generated by {project.title} on {creation_date} using {model_version}.
Since the outputs data format has been updated since this simulation was created, they
must be viewed on the outputs page hosted at this link: <a href={gui_url}>{gui_url}</a>.
</p>
<div className="text-center">
<Button variant="success" href={gui_url}>
{" "}
Click to View Results
</Button>
</div>
</Card.Body>
</Card>
);
};
export default class OutputsComponent extends React.Component<OutputsProps, OutputsState> {
constructor(props) {
super(props);
}
render() {
let { api, remoteSim, sim } = this.props;
if (api.modelpk !== remoteSim?.model_pk.toString()) {
return <Pending />;
} else if (!api.modelpk || remoteSim?.status === "STARTED") {
return <NewSimulation />;
} else if (!remoteSim) {
return <Pending />;
} else if (remoteSim?.status === "PENDING") {
return (
<Pending
eta={remoteSim.eta}
originalEta={remoteSim.original_eta}
showNotify={RolePerms.hasWriteAccess(remoteSim)}
notify={remoteSim.notify_on_completion}
setNotify={this.props.setNotifyOnCompletion}
/>
);
} else if (remoteSim.traceback || sim?.traceback) {
return <Traceback remoteSim={remoteSim} />;
} else if (remoteSim?.outputs_version === "v0") {
return <V0Simulation remoteSim={remoteSim} />;
}
let creation_date = moment(remoteSim.creation_date).format("MMMM Do YYYY, h:mm:ss a");
let model_version = remoteSim.model_version;
let project = remoteSim.project;
let remoteOutputs = remoteSim.outputs.outputs;
let outputs: Outputs = null;
if (sim) {
outputs = sim.outputs;
}
return (
<Card className="card-outer" style={{ overflow: "auto" }}>
<Card className="card-inner">
<Card.Body>
<p className="lead">
{`These results were generated by ${project.title} on ${creation_date} using ${model_version}.`}
</p>
<Row className="align-items-center">
{remoteOutputs.renderable.outputs.map((remoteOutput, ix) => {
let media_type = remoteOutput.media_type;
let output: TableOutput | BokehOutput | any;
if (outputs) {
switch (media_type) {
case "table":
output = outputs.renderable[ix];
break;
case "bokeh":
output = outputs.renderable[ix];
break;
default:
output = outputs.renderable[ix];
}
}
let [width, height] = imgDims(remoteOutput.screenshot);
width = width ? width : 500;
height = height ? height : 500;
return (
<Col
className="align-self-center"
style={{ margin: "1rem", maxWidth: width }}
key={`output-${ix}`}
>
<OverlayTrigger
trigger={["hover", "click"]}
overlay={<Tooltip id={`${ix}-tooltip`}>{remoteOutput.title}</Tooltip>}
>
{outputs !== null ? (
<OutputModal output={output}>
<img
style={{ objectFit: "contain" }}
src={remoteOutput.screenshot}
alt={remoteOutput.title}
height={height}
width={width}
/>
</OutputModal>
) : (
<img
style={{ objectFit: "contain" }}
src={remoteOutput.screenshot}
alt={remoteOutput.title}
height={height}
width={width}
/>
)}
</OverlayTrigger>
</Col>
);
})}
</Row>
<Row className="text-center">
<Col>
<a
href={`/${project.owner}/${project.title}/${remoteSim.model_pk}/download/`}
className="btn btn-lg btn-match-nav"
>
Download Results
</a>
</Col>
</Row>
</Card.Body>
</Card>
</Card>
);
}
}