Skip to content

Commit

Permalink
Merge pull request #11262 from quarto-dev/bugfix/10097
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv authored Nov 15, 2024
2 parents 9286ded + 19e4532 commit aca81c5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/changelog-1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ All changes included in 1.6:
### `jupyter`

- ([#9134](https://github.com/quarto-dev/quarto-cli/issues/9134)): Add proper fix for `multiprocessing` in notebooks with the Python kernel.
- ([#10097](https://github.com/quarto-dev/quarto-cli/issues/10097)): Ensure papermill parameterization works when default values are set in a cell with labels.

## Chromium support

Expand Down
15 changes: 15 additions & 0 deletions src/resources/jupyter/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pathlib import Path

from yaml import safe_load as parse_string
from yaml import safe_dump

from log import trace
import nbformat
Expand Down Expand Up @@ -641,6 +642,20 @@ def nb_parameterize(nb, params):

# prepend options
if len(params_cell_yaml):
# https://github.com/quarto-dev/quarto-cli/issues/10097
# We need to find and drop `label: ` from the yaml options
# to avoid label duplication
# The only way to do this robustly is to parse the yaml
# and then re-encode it
try:
params_cell_yaml = parse_string("\n".join(params_cell_yaml))
del params_cell_yaml['label']
params_cell_yaml = safe_dump(params_cell_yaml).strip().splitlines()
except Exception as e:
sys.stderr.write("\nWARNING: Invalid YAML option format in cell:\n" + "\n".join(params_cell_yaml) + "\n")
sys.stderr.flush()
params_cell_yaml = []

comment_chars = nb_language_comment_chars(language)
option_prefix = comment_chars[0] + "| "
option_suffix = comment_chars[1] if len(comment_chars) > 1 else None
Expand Down
11 changes: 11 additions & 0 deletions tests/docs/jupyter/parameters/issue-10097.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
format: html
engine: jupyter
keep-ipynb: true
---

```{python}
#| label: test
#| tags: [parameters]
datapath = None
```
26 changes: 26 additions & 0 deletions tests/smoke/jupyter/issue-10097.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* parameter-label-duplication.test.ts
*
* https://github.com/quarto-dev/quarto-cli/issues/10097
*
* Copyright (C) 2023 Posit Software, PBC
*/

import { quarto } from "../../../src/quarto.ts";
import { test } from "../../test.ts";
import { assertEquals } from "testing/asserts";
import { noErrors } from "../../verify.ts";

test({
name: "jupyter:parameter:label-duplication",
context: {},
execute: async () => {
// https://github.com/quarto-dev/quarto-cli/issues/10097
await quarto(["render",
"docs/jupyter/parameters/issue-10097.qmd",
"--execute-param", 'datapath:"weird"',
"--no-execute-daemon", "--execute"]);
},
verify: [noErrors],
type: "smoke",
});

0 comments on commit aca81c5

Please sign in to comment.