Skip to content

Commit

Permalink
fix: align closer to origin/main
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Nov 20, 2024
1 parent 9b92238 commit d17c4a4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 48 deletions.
5 changes: 3 additions & 2 deletions packages/myst-cli/src/process/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,11 @@ export async function processNotebookFull(

const outputsChildren = (cell.outputs as IOutput[]).map((output) => {
// Embed outputs in an output block
const result: { type: 'output'; id: string; data: IOutput } = {
// TODO: output-refactoring -- drop to a single output in future
const result: { type: 'output'; id: string; data: IOutput[] } = {
type: 'output',
id: nanoid(),
data: output,
data: [output],
};
return result;
});
Expand Down
12 changes: 11 additions & 1 deletion packages/myst-cli/src/transforms/crossReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { CrossReference, Dependency, Link, SourceFileKind } from 'myst-spec
import type { ISession } from '../session/types.js';
import { loadFromCache, writeToCache } from '../session/cache.js';
import type { SiteAction, SiteExport } from 'myst-config';
import type { IOutput } from '@jupyterlab/nbformat';

export const XREF_MAX_AGE = 1; // in days

Expand Down Expand Up @@ -40,9 +41,18 @@ function upgradeMystData(data: MystData): MystData {
'output',
(node: GenericNode, index: number | null, parent: GenericParent | null) => {
if (parent && parent.type !== 'outputs') {
// assert node.children.length === 1
const outputsChildren = node.data.map((outputData: IOutput) => {
return {
type: 'output',
// TODO: output-refactoring -- drop to single output in future
data: [outputData],
children: [], // FIXME: ignoring children
};
});
const outputs = {
type: 'outputs',
children: [node],
children: outputsChildren,
};
parent.children[index!] = outputs;
}
Expand Down
19 changes: 13 additions & 6 deletions packages/myst-cli/src/transforms/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ export async function transformMarkdownOutputs(
switch (rawOutput.output_type) {
case 'display_data':
case 'execute_result': {
// TODO: output-refactoring -- drop to single output in future
const mimeBundle = (rawOutput.data as any)[0] as Record<string, unknown>;
// Find the most MyST-like Markdown (if any)
const [bestEntry] = Object.entries(rawOutput.data as object)
const [bestEntry] = Object.entries(mimeBundle)
.filter(([mimeType]) => mimeType.startsWith(MARKDOWN_MIME_TYPE))
.map(([mimeType, data]) => [parseVariant(mimeType), data])
.filter(([variant]) => variant === undefined || variant === 'myst')
Expand Down Expand Up @@ -80,7 +82,8 @@ export async function transformOutputsToCache(
outputs
.filter((output) => output.visibility !== 'remove')
.map(async (output) => {
[output.data] = await minifyCellOutput([output.data] as IOutput[], cache.$outputs, {
// TODO: output-refactoring -- drop to single output in future
output.data = await minifyCellOutput(output.data as IOutput[], cache.$outputs, {
computeHash,
maxCharacters: opts?.minifyMaxCharacters,
});
Expand Down Expand Up @@ -116,7 +119,8 @@ export function transformFilterOutputStreams(
const outputs = selectAll('output', block) as GenericNode[];
// There should be only one output in the block
outputs.forEach((output) => {
[output.data] = [output.data].filter((data: IStream | MinifiedMimeOutput) => {
// TODO: output-refactoring -- drop to single output in future
output.data = output.data.filter((data: IStream | MinifiedMimeOutput) => {
if (
(stderr !== 'show' || blockRemoveStderr) &&
data.output_type === 'stream' &&
Expand Down Expand Up @@ -232,7 +236,8 @@ export function transformOutputsToFile(
const cache = castSession(session);

outputs.forEach((node) => {
walkOutputs([node.data], (obj) => {
// TODO: output-refactoring -- drop to single output in future
walkOutputs(node.data, (obj) => {
const { hash } = obj;
if (!hash || !cache.$outputs[hash]) return undefined;
obj.path = writeCachedOutputToFile(session, hash, cache.$outputs[hash], writeFolder, {
Expand Down Expand Up @@ -275,14 +280,16 @@ export function reduceOutputs(
const outputs = selectAll('output', mdast) as GenericNode[];
const cache = castSession(session);
outputs.forEach((node) => {
if (!node.data && !node.children?.length) {
// TODO: output-refactoring -- drop to single output in future
if (!node.data?.length && !node.children?.length) {
node.type = '__delete__';
return;
}
node.type = '__lift__';
if (node.children?.length) return;
const selectedOutputs: { content_type: string; hash: string }[] = [];
[node.data].forEach((output: MinifiedOutput) => {
// TODO: output-refactoring -- drop to single output in future
node.data.forEach((output: MinifiedOutput) => {
let selectedOutput: { content_type: string; hash: string } | undefined;
walkOutputs([output], (obj: any) => {
const { output_type, content_type, hash } = obj;
Expand Down
5 changes: 3 additions & 2 deletions packages/myst-execute/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function buildCacheKey(kernelSpec: KernelSpec, nodes: (CodeBlock | InlineExpress
* TODO: lift this to the myst-spec definition
*/
type CodeBlockOutput = Output & {
data: IOutput;
data: IOutput[];
};

/**
Expand Down Expand Up @@ -285,8 +285,9 @@ function applyComputedOutputsToNodes(
// Pull out outputs to set data
const outputs = select('outputs', matchedNode) as Outputs;
// Ensure that whether this fails or succeeds, we write to `children` (e.g. due to a kernel error)
// TODO: output-refactoring -- drop to single output in future
outputs.children = ((thisResult as IOutput[]) ?? []).map((data) => {
return { type: 'output', children: [], data: data as any };
return { type: 'output', children: [], data: [data] as any };
});
} else if (isInlineExpression(matchedNode)) {
// Set data of expression to the result, or empty if we don't have one
Expand Down
85 changes: 48 additions & 37 deletions packages/myst-execute/tests/execute.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ cases:
value: this will break, but shouldn't be executed
- type: block
kind: notebook-code
data:

children:
- type: code
lang: python
executable: true
value: print('abc')
enumerator: 1
- type: outputs
children:
- type: output
children: []

after:
type: root
children:
Expand All @@ -45,22 +49,23 @@ cases:
value: this will break, but shouldn't be executed
- type: block
kind: notebook-code
data:

children:
- type: code
lang: python
executable: true
value: print('abc')
enumerator: 1
- type: outputs
children:
- type: output
# We lose the ID information, as it's not generated
data:
output_type: stream
name: stdout
text: |
abc
children: []

data:
- output_type: stream
name: stdout
text: |
abc
- title: tree with inline expression is evaluated
before:
type: root
Expand Down Expand Up @@ -100,68 +105,73 @@ cases:
children:
- type: block
kind: notebook-code
data:
children:
- type: code
lang: python
executable: true
value: print('abc')
enumerator: 1
- type: outputs
children:
- type: output
data:
children: []
- type: block
kind: notebook-code
html_id: nb-cell-0
data:
children:
- type: code
lang: python
executable: true
value: raise ValueError
enumerator: 1
- type: outputs
children:
- type: output
data:
children: []
after:
type: root
children:
- type: block
kind: notebook-code
data:
children:
- type: code
lang: python
executable: true
value: print('abc')
enumerator: 1
- type: outputs
children:
- type: output
data:
output_type: stream
name: stdout
text: |
abc
- output_type: stream
name: stdout
text: |
abc
- type: block
kind: notebook-code
data:
children:
- type: code
lang: python
executable: true
value: raise ValueError
enumerator: 1
- type: outputs
children:
- type: output
data:
output_type: error
# Note this traceback can be different on various machines
# Not including it means we still validate an error, just don't care about the traceback
# traceback:
# - "\e[0;31m---------------------------------------------------------------------------\e[0m"
# - "\e[0;31mValueError\e[0m Traceback (most recent call last)"
# - "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"
# - "\e[0;31mValueError\e[0m: "
ename: ValueError
evalue: ''
- output_type: error
# Note this traceback can be different on various machines
# Not including it means we still validate an error, just don't care about the traceback
# traceback:
# - "\e[0;31m---------------------------------------------------------------------------\e[0m"
# - "\e[0;31mValueError\e[0m Traceback (most recent call last)"
# - "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"
# - "\e[0;31mValueError\e[0m: "
ename: ValueError
evalue: ''
- title: tree with bad executable code and `raises-exception` is evaluated and passes
before:
type: root
Expand All @@ -175,12 +185,10 @@ cases:
lang: python
executable: true
value: raise ValueError
enumerator: 1
- type: outputs
children:
- type: output
id: T7FMDqDm8dM2bOT1tKeeM
identifier: nb-cell-0-output
html_id: nb-cell-0-output
data:
after:
type: root
Expand All @@ -194,20 +202,21 @@ cases:
lang: python
executable: true
value: raise ValueError
enumerator: 1
- type: outputs
children:
- type: output
data:
output_type: error
# Note this traceback can be different on various machines
# Not including it means we still validate an error, just don't care about the traceback
# traceback:
# - "\e[0;31m---------------------------------------------------------------------------\e[0m"
# - "\e[0;31mValueError\e[0m Traceback (most recent call last)"
# - "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"
# - "\e[0;31mValueError\e[0m: "
ename: ValueError
evalue: ''
- output_type: error
# Note this traceback can be different on various machines
# Not including it means we still validate an error, just don't care about the traceback
# traceback:
# - "\e[0;31m---------------------------------------------------------------------------\e[0m"
# - "\e[0;31mValueError\e[0m Traceback (most recent call last)"
# - "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"
# - "\e[0;31mValueError\e[0m: "
ename: ValueError
evalue: ''
- title: tree with bad executable code and `skip-execution` is not evaluated
before:
type: root
Expand All @@ -221,6 +230,7 @@ cases:
lang: python
executable: true
value: raise ValueError
enumerator: 1
- type: outputs
children:
- type: output
Expand All @@ -237,6 +247,7 @@ cases:
lang: python
executable: true
value: raise ValueError
enumerator: 1
- type: outputs
children:
- type: output
Expand Down

0 comments on commit d17c4a4

Please sign in to comment.