Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba29858

Browse files
committed
trust-exported-notebooks
1 parent 78d65f0 commit ba29858

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

IPython/core/magics/basic.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ def notebook(self, s):
559559
outfname = os.path.expanduser(args.filename)
560560

561561
from nbformat import write, v4
562+
from nbformat.sign import NotebookNotary
562563

563564
cells = []
564565
hist = list(self.shell.history_manager.get_range())
@@ -629,6 +630,9 @@ def notebook(self, s):
629630
},
630631
},
631632
)
633+
# Sign the notebook to make it trusted
634+
notary = NotebookNotary()
635+
notary.sign(nb)
632636
with io.open(outfname, "w", encoding="utf-8") as f:
633637
write(nb, f, version=4)
634638

tests/test_magic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ def test_extension():
10001000

10011001
def test_notebook_export_json():
10021002
pytest.importorskip("nbformat")
1003+
from nbformat import read, sign
10031004
_ip = get_ipython()
10041005
_ip.history_manager.reset() # Clear any existing history.
10051006
cmds = ["a=1", "def b():\n return a**2", "print('noël, été', b())"]
@@ -1010,6 +1011,7 @@ def test_notebook_export_json():
10101011
_ip.run_line_magic("notebook", "%s" % outfile)
10111012
with open(outfile) as f:
10121013
exported = json.load(f)
1014+
nb = read(outfile, as_version=4)
10131015

10141016
# check metadata
10151017
language_info = exported["metadata"]["language_info"]
@@ -1020,6 +1022,10 @@ def test_notebook_export_json():
10201022
kernelspec = exported["metadata"]["kernelspec"]
10211023
assert kernelspec["language"] == "python"
10221024

1025+
# Check if notebook is trusted
1026+
notary = sign.NotebookNotary()
1027+
is_trusted = notary.check_signature(nb)
1028+
assert is_trusted, "Exported notebook should be trusted"
10231029

10241030
def test_notebook_export_json_with_output():
10251031
"""Tests if notebook export correctly captures outputs, errors, display outputs, and stream outputs."""

0 commit comments

Comments
 (0)