diff --git a/news/changelog-1.6.md b/news/changelog-1.6.md index 042a99dea9f..efa1059b7da 100644 --- a/news/changelog-1.6.md +++ b/news/changelog-1.6.md @@ -124,6 +124,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 diff --git a/src/resources/jupyter/notebook.py b/src/resources/jupyter/notebook.py index 6158960b9ec..0106d29ff15 100644 --- a/src/resources/jupyter/notebook.py +++ b/src/resources/jupyter/notebook.py @@ -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 @@ -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 diff --git a/tests/docs/jupyter/parameters/issue-10097.qmd b/tests/docs/jupyter/parameters/issue-10097.qmd new file mode 100644 index 00000000000..9d121f338c5 --- /dev/null +++ b/tests/docs/jupyter/parameters/issue-10097.qmd @@ -0,0 +1,11 @@ +--- +format: html +engine: jupyter +keep-ipynb: true +--- + +```{python} +#| label: test +#| tags: [parameters] +datapath = None +``` \ No newline at end of file diff --git a/tests/smoke/jupyter/issue-10097.test.ts b/tests/smoke/jupyter/issue-10097.test.ts new file mode 100644 index 00000000000..19586200e28 --- /dev/null +++ b/tests/smoke/jupyter/issue-10097.test.ts @@ -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", +});