emacs-ipython-notebook/lisp/ein.py
John Miller ec537398d1 New feature ein:pytools-export-buffer
Export contents of notebook using nbconvert_ to user-specified format
\(options will depend on the version of nbconvert available\) to a new
buffer.

Currently EIN/IPython supports exporting to the following formats:

- HTML
- JSON (this is basically the sames opening the ipynb file in a buffer).
- Latex
- Markdown
- Python
- RST
- Slides

.. _nbconvert:
http://ipython.org/ipython-doc/stable/notebook/nbconvert.html

Also making ein.py compatible with IPython 3.
2015-02-16 11:28:35 -06:00

66 lines
2.1 KiB
Python

"""
Python utilities to use it from ein.el
Copyright (C) 2012- Takafumi Arakaki
Author: Takafumi Arakaki <aka.tkf at gmail.com>
ein.py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ein.py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ein.py. If not, see <http://www.gnu.org/licenses/>.
"""
def export_nb(nb_filename, format):
import IPython.nbconvert as nbconvert
with open(nb_filename, 'r') as f:
output = nbconvert.export_by_name(format, nb_filename)
print(output[0])
def _find_edit_target_012(*args, **kwds):
from IPython.core.interactiveshell import InteractiveShell
inst = InteractiveShell.instance()
return inst._find_edit_target(*args, **kwds)
def _find_edit_target_013(*args, **kwds):
from IPython.core.interactiveshell import InteractiveShell
inst = InteractiveShell.instance()
return CodeMagics._find_edit_target(inst, *args, **kwds)
try:
from IPython.core.magics import CodeMagics
_find_edit_target = _find_edit_target_013
except ImportError:
_find_edit_target = _find_edit_target_012
def find_source(name):
"""Given an object as string, `name`, print its place in source code."""
# FIXME: use JSON display object instead of stdout
ret = _find_edit_target(name, {}, [])
if ret:
(filename, lineno, use_temp) = ret
if not use_temp:
print(filename)
print(lineno)
return
raise RuntimeError("Source code for {0} cannot be found".format(name))
def run_docstring_examples(obj, verbose=True):
from IPython.core.interactiveshell import InteractiveShell
import doctest
inst = InteractiveShell.instance()
globs = inst.user_ns
return doctest.run_docstring_examples(obj, globs, verbose=verbose)