@magic_arguments.magic_arguments()<EOL><INDENT>@magic_arguments.argument(<EOL>'<STR_LIT>', '<STR_LIT>', action='<STR_LIT:store_true>', default=False,<EOL>help='<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL>@magic_arguments.argument(<EOL>'<STR_LIT>', '<STR_LIT>',<EOL>help='<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>)<EOL>@magic_arguments.argument(<EOL>'<STR_LIT:filename>', type=unicode,<EOL>help='<STR_LIT>'<EOL>)<EOL>@line_magic<EOL>def notebook(self, s):<DEDENT>
args = magic_arguments.parse_argstring(self.notebook, s)<EOL>from IPython.nbformat import current<EOL>args.filename = unquote_filename(args.filename)<EOL>if args.export:<EOL><INDENT>fname, name, format = current.parse_filename(args.filename)<EOL>cells = []<EOL>hist = list(self.shell.history_manager.get_range())<EOL>for session, prompt_number, input in hist[:-<NUM_LIT:1>]:<EOL><INDENT>cells.append(current.new_code_cell(prompt_number=prompt_number,<EOL>input=input))<EOL><DEDENT>worksheet = current.new_worksheet(cells=cells)<EOL>nb = current.new_notebook(name=name,worksheets=[worksheet])<EOL>with io.open(fname, '<STR_LIT:w>', encoding='<STR_LIT:utf-8>') as f:<EOL><INDENT>current.write(nb, f, format);<EOL><DEDENT><DEDENT>elif args.format is not None:<EOL><INDENT>old_fname, old_name, old_format = current.parse_filename(args.filename)<EOL>new_format = args.format<EOL>if new_format == u'<STR_LIT>':<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>elif new_format == u'<STR_LIT>' or new_format == u'<STR_LIT>':<EOL><INDENT>new_fname = old_name + u'<STR_LIT>'<EOL>new_format = u'<STR_LIT>'<EOL><DEDENT>elif new_format == u'<STR_LIT>':<EOL><INDENT>new_fname = old_name + u'<STR_LIT>'<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>' % new_format)<EOL><DEDENT>with io.open(old_fname, '<STR_LIT:r>', encoding='<STR_LIT:utf-8>') as f:<EOL><INDENT>nb = current.read(f, old_format)<EOL><DEDENT>with io.open(new_fname, '<STR_LIT:w>', encoding='<STR_LIT:utf-8>') as f:<EOL><INDENT>current.write(nb, f, new_format)<EOL><DEDENT><DEDENT>
Export and convert IPython notebooks.

        This function can export the current IPython history to a notebook file
        or can convert an existing notebook file into a different format. For
        example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
        To export the history to "foo.py" do "%notebook -e foo.py". To convert
        "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
        formats include (json/ipynb, py).