-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathindex.tsx
247 lines (214 loc) · 6.75 KB
/
index.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { render as prender } from "preact";
import {
ReTable,
SpaceChart,
Histogram,
CreateSingleEstimateResult,
EstimatesOverview,
EstimatesPanel,
ReData,
Circuit,
setRenderer,
} from "qsharp-lang/ux";
import markdownIt from "markdown-it";
import "./widgets.css";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - there are no types for this
import mk from "@vscode/markdown-it-katex";
const md = markdownIt();
md.use(mk);
setRenderer((input: string) => md.render(input));
export function mdRenderer(input: string) {
// Note: Need to ensure this 'fix' is still needed with the latest data JSON.
// In early testing backslashes were being double-escaped in the results.
return md.render(input.replace(/\\\\/g, "\\"));
}
// Param types for AnyWidget render functions
import type { AnyModel } from "@anywidget/types";
type RenderArgs = {
model: AnyModel;
el: HTMLElement;
};
export function render({ model, el }: RenderArgs) {
const componentType = model.get("comp");
// There is an existing issue where in VS Code it always shows the widget background as white.
// (See https://github.com/microsoft/vscode-jupyter/issues/7161)
// We tried to fix this in CSS by overridding the style, but there is a race condition whereby
// depending on which style gets injected first (ours or ipywidgets), it may or may not work.
// The solution here is to force our own override to be last in the style list if not already.
// It's a bit of a hack, but it works, and I couldn't find something better that wouldn't be fragile.
if (
!el.ownerDocument.head.lastChild?.textContent?.includes("widget-css-fix")
) {
const forceStyle = el.ownerDocument.createElement("style");
forceStyle.textContent = `/* widget-css-fix */ .cell-output-ipywidget-background {background-color: transparent !important;}`;
el.ownerDocument.head.appendChild(forceStyle);
}
switch (componentType) {
case "SpaceChart":
renderChart({ model, el });
break;
case "EstimatesOverview":
renderEstimatesOverview({ model, el });
break;
case "EstimateDetails":
renderTable({ model, el });
break;
case "Histogram":
renderHistogram({ model, el });
break;
case "EstimatesPanel":
renderEstimatesPanel({ model, el });
break;
case "Circuit":
renderCircuit({ model, el });
break;
default:
throw new Error(`Unknown component type ${componentType}`);
}
}
function renderTable({ model, el }: RenderArgs) {
const onChange = () => {
const estimates = model.get("estimates");
const index = model.get("index");
const singleEstimateResult = CreateSingleEstimateResult(estimates, index);
prender(
<ReTable
estimatesData={singleEstimateResult}
mdRenderer={mdRenderer}
></ReTable>,
el,
);
};
onChange();
model.on("change:estimates", onChange);
model.on("change:index", onChange);
}
function renderChart({ model, el }: RenderArgs) {
const onChange = () => {
const estimates = model.get("estimates");
const index = model.get("index");
const singleEstimateResult = CreateSingleEstimateResult(estimates, index);
prender(<SpaceChart estimatesData={singleEstimateResult}></SpaceChart>, el);
};
onChange();
model.on("change:estimates", onChange);
model.on("change:index", onChange);
}
function renderEstimatesOverview({ model, el }: RenderArgs) {
const onChange = () => {
const results = model.get("estimates");
const colors = model.get("colors");
const runNames = model.get("runNames");
let estimates = [];
if (results[0] == null) {
estimates.push(results);
} else {
for (const estimate of Object.values(results)) {
estimates.push(estimate);
}
}
const onRowDeleted = createOnRowDeleted(estimates, (newEstimates) => {
estimates = newEstimates;
model.set("estimates", estimates);
});
prender(
<EstimatesOverview
estimatesData={estimates}
runNames={runNames}
colors={colors}
isSimplifiedView={true}
onRowDeleted={onRowDeleted}
setEstimate={() => undefined}
allowSaveImage={true}
></EstimatesOverview>,
el,
);
};
onChange();
model.on("change:estimates", onChange);
model.on("change:colors", onChange);
model.on("change:runNames", onChange);
}
function renderEstimatesPanel({ model, el }: RenderArgs) {
const onChange = () => {
const results = model.get("estimates");
const colors = model.get("colors");
const runNames = model.get("runNames");
let estimates: ReData[] = [];
if (results[0] == null) {
estimates.push(results);
} else {
for (const estimate of Object.values(results)) {
estimates.push(estimate as ReData);
}
}
const onRowDeleted = createOnRowDeleted(estimates, (newEstimates) => {
estimates = newEstimates;
model.set("estimates", estimates);
});
prender(
<EstimatesPanel
estimatesData={estimates}
runNames={runNames}
colors={colors}
renderer={mdRenderer}
calculating={false}
onRowDeleted={onRowDeleted}
allowSaveImage={true}
></EstimatesPanel>,
el,
);
};
onChange();
model.on("change:estimates", onChange);
model.on("change:colors", onChange);
model.on("change:runNames", onChange);
}
function createOnRowDeleted(
estimates: ReData[],
setEstimates: (estimates: ReData[]) => void,
) {
return (rowId: string) => {
// Clone estimates into a new object
const newEstimates = JSON.parse(JSON.stringify(estimates)) as ReData[];
// Splice out the estimate that was deleted
const index = newEstimates.findIndex(
(estimate) => estimate.jobParams.runName === rowId,
);
if (index >= 0) {
newEstimates.splice(index, 1);
}
setEstimates(newEstimates);
};
}
function renderHistogram({ model, el }: RenderArgs) {
const onChange = () => {
const buckets = model.get("buckets") as { [key: string]: number };
const bucketMap = new Map(Object.entries(buckets));
const shot_count = model.get("shot_count") as number;
prender(
<Histogram
data={bucketMap}
shotCount={shot_count}
filter={""}
onFilter={() => undefined}
shotsHeader={true}
></Histogram>,
el,
);
};
onChange();
model.on("change:buckets", onChange);
model.on("change:shot_count", onChange);
}
function renderCircuit({ model, el }: RenderArgs) {
const onChange = () => {
const circuitJson = model.get("circuit_json") as string;
prender(<Circuit circuit={JSON.parse(circuitJson)}></Circuit>, el);
};
onChange();
model.on("change:circuit_json", onChange);
}