Skip to content

Commit 2af0e14

Browse files
committed
fix: handle IDs
1 parent ee85d11 commit 2af0e14

File tree

2 files changed

+142
-31
lines changed

2 files changed

+142
-31
lines changed

packages/myst-execute/src/execute.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,9 @@ function applyComputedOutputsToNodes(
286286
// Pull out outputs to set data
287287
const outputs = select('outputs', matchedNode) as Outputs;
288288
// Ensure that whether this fails or succeeds, we write to `children` (e.g. due to a kernel error)
289-
outputs.children = rawOutputData.map((data) => {
290-
return { type: 'output', children: [], jupyter_data: data as any };
289+
outputs.children = rawOutputData.map((data, index) => {
290+
const identifier = outputs.identifier ? `${outputs.identifier}-${index}` : undefined;
291+
return { type: 'output', children: [], jupyter_data: data as any, identifier };
291292
});
292293
} else if (isInlineExpression(matchedNode)) {
293294
const rawOutputData = thisResult as Record<string, unknown> | undefined;

packages/myst-execute/tests/execute.yml

Lines changed: 139 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,24 @@ cases:
2727
- type: block
2828
kind: notebook-code
2929
data:
30-
30+
id: nb-cell-0
31+
identifier: nb-cell-0
32+
label: nb-cell-0
33+
html_id: nb-cell-0
3134
children:
3235
- type: code
3336
lang: python
3437
executable: true
3538
value: print('abc')
3639
enumerator: 1
40+
identifier: nb-cell-0-code
41+
html_id: nb-cell-0-code
3742
- type: outputs
43+
identifier: nb-cell-0-output
44+
html_id: nb-cell-0-output
3845
children:
3946
- type: output
47+
identifier: nb-cell-0-output-0
4048
children: []
4149

4250
after:
@@ -50,22 +58,86 @@ cases:
5058
- type: block
5159
kind: notebook-code
5260
data:
61+
id: nb-cell-0
62+
identifier: nb-cell-0
63+
label: nb-cell-0
64+
html_id: nb-cell-0
65+
children:
66+
- type: code
67+
lang: python
68+
executable: true
69+
value: print('abc')
70+
enumerator: 1
71+
identifier: nb-cell-0-code
72+
html_id: nb-cell-0-code
73+
- type: outputs
74+
identifier: nb-cell-0-output
75+
html_id: nb-cell-0-output
76+
children:
77+
- type: output
78+
children: []
79+
identifier: nb-cell-0-output-0
80+
jupyter_data:
81+
output_type: stream
82+
name: stdout
83+
text: |
84+
abc
85+
- title: output without identifier is given one
86+
before:
87+
type: root
88+
children:
89+
- type: block
90+
children:
91+
- type: block
92+
kind: notebook-code
93+
identifier: nb-cell-0
94+
label: nb-cell-0
95+
html_id: nb-cell-0
96+
children:
97+
- type: code
98+
lang: python
99+
executable: true
100+
value: print('abc')
101+
enumerator: 1
102+
identifier: nb-cell-0-code
103+
html_id: nb-cell-0-code
104+
- type: outputs
105+
identifier: nb-cell-0-output
106+
html_id: nb-cell-0-output
107+
children:
108+
- type: output
109+
children: []
53110

111+
after:
112+
type: root
113+
children:
114+
- type: block
115+
children:
116+
- type: block
117+
kind: notebook-code
118+
identifier: nb-cell-0
119+
label: nb-cell-0
120+
html_id: nb-cell-0
54121
children:
55122
- type: code
56123
lang: python
57124
executable: true
58125
value: print('abc')
59126
enumerator: 1
127+
identifier: nb-cell-0-code
128+
html_id: nb-cell-0-code
60129
- type: outputs
130+
identifier: nb-cell-0-output
131+
html_id: nb-cell-0-output
61132
children:
62133
- type: output
63134
children: []
135+
identifier: nb-cell-0-output-0
64136
jupyter_data:
65-
output_type: stream
66-
name: stdout
67-
text: |
68-
abc
137+
output_type: stream
138+
name: stdout
139+
text: |
140+
abc
69141
- title: tree with inline expression is evaluated
70142
before:
71143
type: root
@@ -112,9 +184,14 @@ cases:
112184
executable: true
113185
value: print('abc')
114186
enumerator: 1
187+
identifier: nb-cell-0-code
188+
html_id: nb-cell-0-code
115189
- type: outputs
190+
identifier: nb-cell-0-output
191+
html_id: nb-cell-0-output
116192
children:
117193
- type: output
194+
identifier: nb-cell-0-output-0
118195
jupyter_data:
119196
- type: block
120197
kind: notebook-code
@@ -125,9 +202,14 @@ cases:
125202
executable: true
126203
value: raise ValueError
127204
enumerator: 1
205+
identifier: nb-cell-1-code
206+
html_id: nb-cell-1-code
128207
- type: outputs
208+
identifier: nb-cell-1-output
209+
html_id: nb-cell-1-output
129210
children:
130211
- type: output
212+
identifier: nb-cell-1-output-0
131213
jupyter_data:
132214
after:
133215
type: root
@@ -141,14 +223,19 @@ cases:
141223
executable: true
142224
value: print('abc')
143225
enumerator: 1
226+
identifier: nb-cell-0-code
227+
html_id: nb-cell-0-code
144228
- type: outputs
229+
identifier: nb-cell-0-output
230+
html_id: nb-cell-0-output
145231
children:
146232
- type: output
233+
identifier: nb-cell-0-output-0
147234
jupyter_data:
148-
output_type: stream
149-
name: stdout
150-
text: |
151-
abc
235+
output_type: stream
236+
name: stdout
237+
text: |
238+
abc
152239
- type: block
153240
kind: notebook-code
154241
data:
@@ -158,20 +245,25 @@ cases:
158245
executable: true
159246
value: raise ValueError
160247
enumerator: 1
248+
identifier: nb-cell-1-code
249+
html_id: nb-cell-1-code
161250
- type: outputs
251+
identifier: nb-cell-1-output
252+
html_id: nb-cell-1-output
162253
children:
163254
- type: output
255+
identifier: nb-cell-1-output-0
164256
jupyter_data:
165-
output_type: error
166-
# Note this traceback can be different on various machines
167-
# Not including it means we still validate an error, just don't care about the traceback
168-
# traceback:
169-
# - "\e[0;31m---------------------------------------------------------------------------\e[0m"
170-
# - "\e[0;31mValueError\e[0m Traceback (most recent call last)"
171-
# - "Cell \e[0;32mIn[2], line 1\e[0m\n\e[0;32m----> 1\e[0m \e[38;5;28;01mraise\e[39;00m \e[38;5;167;01mValueError\e[39;00m\n"
172-
# - "\e[0;31mValueError\e[0m: "
173-
ename: ValueError
174-
evalue: ''
257+
output_type: error
258+
# Note this traceback can be different on various machines
259+
# Not including it means we still validate an error, just don't care about the traceback
260+
# traceback:
261+
# - "\e[0;31m---------------------------------------------------------------------------\e[0m"
262+
# - "\e[0;31mValueError\e[0m Traceback (most recent call last)"
263+
# - "Cell \e[0;32mIn[2], line 1\e[0m\n\e[0;32m----> 1\e[0m \e[38;5;28;01mraise\e[39;00m \e[38;5;167;01mValueError\e[39;00m\n"
264+
# - "\e[0;31mValueError\e[0m: "
265+
ename: ValueError
266+
evalue: ''
175267
- title: tree with bad executable code and `raises-exception` is evaluated and passes
176268
before:
177269
type: root
@@ -186,9 +278,14 @@ cases:
186278
executable: true
187279
value: raise ValueError
188280
enumerator: 1
281+
identifier: nb-cell-0-code
282+
html_id: nb-cell-0-code
189283
- type: outputs
284+
identifier: nb-cell-0-output
285+
html_id: nb-cell-0-output
190286
children:
191287
- type: output
288+
identifier: nb-cell-0-output-0
192289
jupyter_data:
193290
after:
194291
type: root
@@ -203,20 +300,25 @@ cases:
203300
executable: true
204301
value: raise ValueError
205302
enumerator: 1
303+
identifier: nb-cell-0-code
304+
html_id: nb-cell-0-code
206305
- type: outputs
306+
identifier: nb-cell-0-output
307+
html_id: nb-cell-0-output
207308
children:
208309
- type: output
310+
identifier: nb-cell-0-output-0
209311
jupyter_data:
210-
output_type: error
211-
# Note this traceback can be different on various machines
212-
# Not including it means we still validate an error, just don't care about the traceback
213-
# traceback:
214-
# - "\e[0;31m---------------------------------------------------------------------------\e[0m"
215-
# - "\e[0;31mValueError\e[0m Traceback (most recent call last)"
216-
# - "Cell \e[0;32mIn[2], line 1\e[0m\n\e[0;32m----> 1\e[0m \e[38;5;28;01mraise\e[39;00m \e[38;5;167;01mValueError\e[39;00m\n"
217-
# - "\e[0;31mValueError\e[0m: "
218-
ename: ValueError
219-
evalue: ''
312+
output_type: error
313+
# Note this traceback can be different on various machines
314+
# Not including it means we still validate an error, just don't care about the traceback
315+
# traceback:
316+
# - "\e[0;31m---------------------------------------------------------------------------\e[0m"
317+
# - "\e[0;31mValueError\e[0m Traceback (most recent call last)"
318+
# - "Cell \e[0;32mIn[2], line 1\e[0m\n\e[0;32m----> 1\e[0m \e[38;5;28;01mraise\e[39;00m \e[38;5;167;01mValueError\e[39;00m\n"
319+
# - "\e[0;31mValueError\e[0m: "
320+
ename: ValueError
321+
evalue: ''
220322
- title: tree with bad executable code and `skip-execution` is not evaluated
221323
before:
222324
type: root
@@ -231,9 +333,13 @@ cases:
231333
executable: true
232334
value: raise ValueError
233335
enumerator: 1
336+
identifier: nb-cell-0-code
337+
html_id: nb-cell-0-code
234338
- type: outputs
339+
identifier: nb-cell-0-output
235340
children:
236341
- type: output
342+
identifier: nb-cell-0-output-0
237343
jupyter_data:
238344
after:
239345
type: root
@@ -248,7 +354,11 @@ cases:
248354
executable: true
249355
value: raise ValueError
250356
enumerator: 1
357+
identifier: nb-cell-0-code
358+
html_id: nb-cell-0-code
251359
- type: outputs
360+
identifier: nb-cell-0-output
252361
children:
253362
- type: output
363+
identifier: nb-cell-0-output-0
254364
jupyter_data:

0 commit comments

Comments
 (0)