Skip to content

Make kernel_name override update notebook kernelspec metadata#868

Open
huyhoang171106 wants to merge 4 commits intonteract:mainfrom
huyhoang171106:fix/make-kernel-name-override-update-noteboo
Open

Make kernel_name override update notebook kernelspec metadata#868
huyhoang171106 wants to merge 4 commits intonteract:mainfrom
huyhoang171106:fix/make-kernel-name-override-update-noteboo

Conversation

@huyhoang171106
Copy link
Copy Markdown

Summary

The current execution flow appears to pass kernel_name to the execution engine, but does not reliably persist that override into the output notebook metadata. As a result, notebook consumers (e.g., JupyterHub UI) still show the original kernelspec.display_name and/or kernelspec.name. This file should be updated so that when kernel_name is provided, the in-memory notebook object has its metadata rewritten before output is saved.

Files changed

  • papermill/execute.py (modified)
  • papermill/cli.py (modified)
  • papermill/tests/__init__.py (modified)
  • papermill/tests/test_cli.py (modified)

Testing

  • Not run in this environment.

What does this PR do?

Fixes #<issue_number>

Closes #856

…tadata

Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
…tadata

Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
…tadata

Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
…tadata

Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 26, 2026 14:42
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses issue #856 by ensuring that when a kernel_name override is provided to papermill (including via the CLI), the output notebook’s metadata.kernelspec is rewritten so downstream notebook consumers display the overridden kernel.

Changes:

  • Add logic to persist kernel_name into nb.metadata.kernelspec.name and display_name before writing the output notebook.
  • Thread kernel_name into prepare_notebook_metadata(...) so the rewrite happens consistently (including --prepare-only).
  • Add a CLI test asserting the output notebook metadata reflects the overridden kernel.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
papermill/execute.py Introduces metadata rewrite helper and passes kernel_name into metadata preparation.
papermill/cli.py Updates --kernel help text to reflect overriding notebook metadata.
papermill/tests/init.py Adds a test constant for a kernel override name.
papermill/tests/test_cli.py Adds a regression test validating kernelspec metadata is updated in the output notebook.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if not kernel_name:
return nb

kernelspec = nb.metadata.get('kernelspec', {})
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_override_kernel_name_in_metadata assumes nb.metadata['kernelspec'] is a mapping; if a notebook has kernelspec=None (or any non-dict), this will raise when assigning ['name']/['display_name']. Consider normalizing kernelspec to an empty dict when it's missing or not a dict-like object before writing into it.

Suggested change
kernelspec = nb.metadata.get('kernelspec', {})
kernelspec = nb.metadata.get('kernelspec')
if not kernelspec:
kernelspec = {}
elif not isinstance(kernelspec, dict):
# Normalize non-dict, mapping-like objects to a plain dict; fall back to empty dict otherwise.
try:
kernelspec = dict(kernelspec)
except TypeError:
kernelspec = {}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kernel_name argument of papermill.execute seems to have no effect.

2 participants