Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pathlib in notebook command #13761

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions IPython/core/magics/basic.py
Expand Up @@ -2,12 +2,11 @@


from logging import error
import io
import os
from pprint import pformat
import sys
from warnings import warn

from pathlib import Path
from traitlets.utils.importstring import import_item
from IPython.core import magic_arguments, page
from IPython.core.error import UsageError
Expand Down Expand Up @@ -572,21 +571,21 @@ def notebook(self, s):
For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb".
"""
args = magic_arguments.parse_argstring(self.notebook, s)
outfname = os.path.expanduser(args.filename)
outf = Path(args.filename)

from nbformat import write, v4

cells = []
hist = list(self.shell.history_manager.get_range())
if(len(hist)<=1):
raise ValueError('History is empty, cannot export')
for session, execution_count, source in hist[:-1]:
for _, execution_count, source in hist[:-1]:
cells.append(v4.new_code_cell(
execution_count=execution_count,
source=source
))
nb = v4.new_notebook(cells=cells)
with io.open(outfname, "w", encoding="utf-8") as f:
with outf.open(mode="w", encoding="utf-8") as f:
write(nb, f, version=4)

@magics_class
Expand Down