From 0e627b974238194a724a7c8101a93756a850f84d Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 31 Oct 2024 09:43:31 -0700 Subject: [PATCH 1/3] do not duplicate label when creating new parameter cell --- src/resources/jupyter/notebook.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/resources/jupyter/notebook.py b/src/resources/jupyter/notebook.py index 6158960b9e..0106d29ff1 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 From d5b7ddbe34453ccb3a912a8a12ca92527ac2c9a7 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 31 Oct 2024 09:47:28 -0700 Subject: [PATCH 2/3] regression test --- tests/docs/jupyter/parameters/issue-10097.qmd | 11 ++++++++ tests/smoke/jupyter/issue-10097.test.ts | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tests/docs/jupyter/parameters/issue-10097.qmd create mode 100644 tests/smoke/jupyter/issue-10097.test.ts diff --git a/tests/docs/jupyter/parameters/issue-10097.qmd b/tests/docs/jupyter/parameters/issue-10097.qmd new file mode 100644 index 0000000000..9d121f338c --- /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 0000000000..19586200e2 --- /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", +}); From 372d1731c7060121803117a98015a1ea3b12b447 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Thu, 31 Oct 2024 09:48:23 -0700 Subject: [PATCH 3/3] changelog --- news/changelog-1.6.md | 1 + 1 file changed, 1 insertion(+) diff --git a/news/changelog-1.6.md b/news/changelog-1.6.md index cfaf20aa07..a7d020345a 100644 --- a/news/changelog-1.6.md +++ b/news/changelog-1.6.md @@ -112,6 +112,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