##// END OF EJS Templates
Expand ~ in filename arguments for %notebook, %save and %history magics
nicolaslazo -
Show More
@@ -4,6 +4,7 b''
4 import argparse
4 import argparse
5 from logging import error
5 from logging import error
6 import io
6 import io
7 import os
7 from pprint import pformat
8 from pprint import pformat
8 import sys
9 import sys
9 from warnings import warn
10 from warnings import warn
@@ -572,6 +573,7 b' Currently the magic system has the following functions:""",'
572 For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb".
573 For example, to export the history to "foo.ipynb" do "%notebook foo.ipynb".
573 """
574 """
574 args = magic_arguments.parse_argstring(self.notebook, s)
575 args = magic_arguments.parse_argstring(self.notebook, s)
576 outfname = os.path.expanduser(args.filename)
575
577
576 from nbformat import write, v4
578 from nbformat import write, v4
577
579
@@ -585,7 +587,7 b' Currently the magic system has the following functions:""",'
585 source=source
587 source=source
586 ))
588 ))
587 nb = v4.new_notebook(cells=cells)
589 nb = v4.new_notebook(cells=cells)
588 with io.open(args.filename, 'w', encoding='utf-8') as f:
590 with io.open(outfname, "w", encoding="utf-8") as f:
589 write(nb, f, version=4)
591 write(nb, f, version=4)
590
592
591 @magics_class
593 @magics_class
@@ -222,6 +222,7 b' class CodeMagics(Magics):'
222 fname, codefrom = args[0], " ".join(args[1:])
222 fname, codefrom = args[0], " ".join(args[1:])
223 if not fname.endswith(('.py','.ipy')):
223 if not fname.endswith(('.py','.ipy')):
224 fname += ext
224 fname += ext
225 fname = os.path.expanduser(fname)
225 file_exists = os.path.isfile(fname)
226 file_exists = os.path.isfile(fname)
226 if file_exists and not force and not append:
227 if file_exists and not force and not append:
227 try:
228 try:
@@ -151,6 +151,7 b' class HistoryMagics(Magics):'
151 # We don't want to close stdout at the end!
151 # We don't want to close stdout at the end!
152 close_at_end = False
152 close_at_end = False
153 else:
153 else:
154 outfname = os.path.expanduser(outfname)
154 if os.path.exists(outfname):
155 if os.path.exists(outfname):
155 try:
156 try:
156 ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname)
157 ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname)
General Comments 0
You need to be logged in to leave comments. Login now