Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/changelog-1.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ All changes included in 1.5:
- ([#8998](https://github.com/quarto-dev/quarto-cli/issues/8998)): Interpret slide separation markers `---` correctly when creating the `.ipynb` intermediate notebook from a `.qmd` file.
- ([#9133](https://github.com/quarto-dev/quarto-cli/issues/9133)): Fix issue with Jupyter engine when using paths containing special characters.
- ([#9255](https://github.com/quarto-dev/quarto-cli/issues/9255)): Support cell source fields of type `string`.
- ([#9422](https://github.com/quarto-dev/quarto-cli/issues/9422)): Improve the stream merging algorithm in output cells to avoid merging outputs that should not be merged.

## Website Listings

Expand Down
30 changes: 12 additions & 18 deletions src/core/jupyter/jupyter-fixups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,26 @@ export function fixupStreams(nb: JupyterNotebook): JupyterNotebook {
if (cell.cell_type !== "code" || cell.outputs === undefined) {
continue;
}
let i = 0;
if (cell.outputs.length === 0) {
continue;
}
const newOutputs: JupyterOutput[] = [cell.outputs[0]];
let i = 1;
while (i < cell.outputs.length) {
const prevOutput: JupyterOutput = newOutputs[newOutputs.length - 1];
const thisOutput: JupyterOutput = cell.outputs[i];
if (thisOutput.output_type === "stream") {
// collect all the stream outputs with the same name
const streams = cell.outputs.filter((output) =>
output.output_type === "stream" && output.name === thisOutput.name
);
// join them together
const joinedText = streams.map((output) => output.text ?? []).flat();
const newOutput: JupyterOutput = {
output_type: "stream",
name: thisOutput.name,
text: joinedText,
};
cell.outputs[i] = newOutput;
cell.outputs = cell.outputs.filter((output, j) =>
i === j ||
(output.output_type !== "stream" || output.name !== thisOutput.name)
);
if (
thisOutput.output_type === "stream" &&
prevOutput.output_type === "stream" &&
thisOutput.name === prevOutput.name
) {
prevOutput.text = [...prevOutput.text ?? [], ...thisOutput.text ?? []];
} else {
newOutputs.push(thisOutput);
}
i++;
}
cell.outputs = newOutputs;
}
return nb;
}
Expand Down
23 changes: 18 additions & 5 deletions src/core/jupyter/jupyter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,13 @@ async function mdFromCodeCell(
if (output.output_type === "stream") {
const stream = output as JupyterOutputStream;
if (asis && stream.name === "stdout") {
md.push(stream.text.join(""));
let text: string[] = [];
if (typeof stream.text === "string") {
text = [stream.text];
} else {
text = stream.text;
}
md.push(text.join(""));
} else {
md.push(mdOutputStream(stream));
}
Expand Down Expand Up @@ -1750,21 +1756,28 @@ function isMarkdown(output: JupyterOutput, options: JupyterToMarkdownOptions) {
}

function mdOutputStream(output: JupyterOutputStream) {
let text: string[] = [];
if (typeof output.text === "string") {
text = [output.text];
} else {
text = output.text;
}

// trim off warning source line for notebook
if (output.name === "stderr") {
if (output.text[0]) {
const firstLine = output.text[0].replace(
if (text[0]) {
const firstLine = text[0].replace(
/<ipython-input.*?>:\d+:\s+/,
"",
);
return mdCodeOutput(
[firstLine, ...output.text.slice(1)].map(colors.stripColor),
[firstLine, ...text.slice(1)].map(colors.stripColor),
);
}
}

// normal default handling
return mdCodeOutput(output.text.map(colors.stripColor));
return mdCodeOutput(text.map(colors.stripColor));
}

async function mdOutputError(
Expand Down
4 changes: 2 additions & 2 deletions src/core/jupyter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ export interface JupyterOutput {
metadata?: Record<string, unknown>;
data?: Record<string, unknown>;
name?: string;
text?: string[];
text?: string[] | string;
}

export interface JupyterOutputStream extends JupyterOutput {
name: "stdout" | "stderr";
text: string[];
text: string[] | string;
}

export interface JupyterOutputDisplayData extends JupyterOutput {
Expand Down
47 changes: 47 additions & 0 deletions tests/docs/smoke-all/2024/04/19/9422.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: "Generate Plots within Sub-Sections"
format:
html:
echo: false
# embed-resources: true
engine: jupyter
_quarto:
tests:
html:
ensureHtmlElements:
- ["section#scatter img"]
---

```{python}
#| label: setup
import matplotlib.pyplot as plt
```

```{python}
#| label: define-data

data = {'apple': 10, 'orange': 15, 'lemon': 5, 'lime': 20}
names = list(data.keys())
values = list(data.values())
```

## Plots and Subsections

```{python}
#| output: asis

print("\n### Bar\n")

plt.bar(names, values)
plt.show(block=False)

print("\n### Scatter\n")

plt.scatter(names, values)
plt.show(block=False)

print("\n### Line\n")

plt.plot(names, values)
plt.show(block=False)
```