Merge pull request #175 from minrk/ipython-2.0
Support IPython 2.0 in notebook liquid tag
This commit is contained in:
@@ -55,7 +55,12 @@ if not LooseVersion(IPython.__version__) >= '1.0':
|
|||||||
|
|
||||||
from IPython import nbconvert
|
from IPython import nbconvert
|
||||||
|
|
||||||
from IPython.nbconvert.filters.highlight import _pygment_highlight
|
try:
|
||||||
|
from IPython.nbconvert.filters.highlight import _pygments_highlight
|
||||||
|
except ImportError:
|
||||||
|
# IPython < 2.0
|
||||||
|
from IPython.nbconvert.filters.highlight import _pygment_highlight as _pygments_highlight
|
||||||
|
|
||||||
from pygments.formatters import HtmlFormatter
|
from pygments.formatters import HtmlFormatter
|
||||||
|
|
||||||
from IPython.nbconvert.exporters import HTMLExporter
|
from IPython.nbconvert.exporters import HTMLExporter
|
||||||
@@ -64,9 +69,10 @@ from IPython.config import Config
|
|||||||
from IPython.nbformat import current as nbformat
|
from IPython.nbformat import current as nbformat
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from IPython.nbconvert.transformers import Transformer
|
from IPython.nbconvert.preprocessors import Preprocessor
|
||||||
except ImportError:
|
except ImportError:
|
||||||
raise ValueError("IPython version 2.0 is not yet supported")
|
# IPython < 2.0
|
||||||
|
from IPython.nbconvert.transformers import Transformer as Preprocessor
|
||||||
|
|
||||||
from IPython.utils.traitlets import Integer
|
from IPython.utils.traitlets import Integer
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
@@ -111,6 +117,17 @@ pre.ipynb {
|
|||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* remove the prompt div from text cells */
|
||||||
|
div.text_cell .prompt {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* remove horizontal padding from text cells, */
|
||||||
|
/* so it aligns with outer body text */
|
||||||
|
div.text_cell_render {
|
||||||
|
padding: 0.5em 0em;
|
||||||
|
}
|
||||||
|
|
||||||
img.anim_icon{padding:0; border:0; vertical-align:middle; -webkit-box-shadow:none; -box-shadow:none}
|
img.anim_icon{padding:0; border:0; vertical-align:middle; -webkit-box-shadow:none; -box-shadow:none}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
@@ -144,7 +161,7 @@ CSS_WRAPPER = """
|
|||||||
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
# Create a custom transformer
|
# Create a custom preprocessor
|
||||||
class SliceIndex(Integer):
|
class SliceIndex(Integer):
|
||||||
"""An integer trait that accepts None"""
|
"""An integer trait that accepts None"""
|
||||||
default_value = None
|
default_value = None
|
||||||
@@ -156,20 +173,22 @@ class SliceIndex(Integer):
|
|||||||
return super(SliceIndex, self).validate(obj, value)
|
return super(SliceIndex, self).validate(obj, value)
|
||||||
|
|
||||||
|
|
||||||
class SubCell(Transformer):
|
class SubCell(Preprocessor):
|
||||||
"""A transformer to select a slice of the cells of a notebook"""
|
"""A transformer to select a slice of the cells of a notebook"""
|
||||||
start = SliceIndex(0, config=True,
|
start = SliceIndex(0, config=True,
|
||||||
help="first cell of notebook to be converted")
|
help="first cell of notebook to be converted")
|
||||||
end = SliceIndex(None, config=True,
|
end = SliceIndex(None, config=True,
|
||||||
help="last cell of notebook to be converted")
|
help="last cell of notebook to be converted")
|
||||||
|
|
||||||
def call(self, nb, resources):
|
def preprocess(self, nb, resources):
|
||||||
nbc = deepcopy(nb)
|
nbc = deepcopy(nb)
|
||||||
for worksheet in nbc.worksheets:
|
for worksheet in nbc.worksheets:
|
||||||
cells = worksheet.cells[:]
|
cells = worksheet.cells[:]
|
||||||
worksheet.cells = cells[self.start:self.end]
|
worksheet.cells = cells[self.start:self.end]
|
||||||
return nbc, resources
|
return nbc, resources
|
||||||
|
|
||||||
|
call = preprocess # IPython < 2.0
|
||||||
|
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
# Customize the html template:
|
# Customize the html template:
|
||||||
@@ -205,9 +224,11 @@ pelican_loader = DictLoader({'pelicanhtml.tpl':
|
|||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
# Custom highlighter:
|
# Custom highlighter:
|
||||||
# instead of using class='highlight', use class='highlight-ipynb'
|
# instead of using class='highlight', use class='highlight-ipynb'
|
||||||
def custom_highlighter(source, language='ipython'):
|
def custom_highlighter(source, language='ipython', metadata=None):
|
||||||
formatter = HtmlFormatter(cssclass='highlight-ipynb')
|
formatter = HtmlFormatter(cssclass='highlight-ipynb')
|
||||||
output = _pygment_highlight(source, formatter, language)
|
if not language:
|
||||||
|
language = 'ipython'
|
||||||
|
output = _pygments_highlight(source, formatter, language)
|
||||||
return output.replace('<pre>', '<pre class="ipynb">')
|
return output.replace('<pre>', '<pre class="ipynb">')
|
||||||
|
|
||||||
|
|
||||||
@@ -253,11 +274,16 @@ def notebook(preprocessor, tag, markup):
|
|||||||
'SubCell':
|
'SubCell':
|
||||||
{'enabled':True, 'start':start, 'end':end}})
|
{'enabled':True, 'start':start, 'end':end}})
|
||||||
|
|
||||||
|
if LooseVersion(IPython.__version__) >= '2.0':
|
||||||
|
subcell_kwarg = dict(preprocessors=[SubCell])
|
||||||
|
else:
|
||||||
|
subcell_kwarg = dict(transformers=[SubCell])
|
||||||
|
|
||||||
exporter = HTMLExporter(config=c,
|
exporter = HTMLExporter(config=c,
|
||||||
template_file='basic',
|
template_file='basic',
|
||||||
filters={'highlight2html': custom_highlighter},
|
filters={'highlight2html': custom_highlighter},
|
||||||
transformers=[SubCell],
|
extra_loaders=[pelican_loader],
|
||||||
extra_loaders=[pelican_loader])
|
**subcell_kwarg)
|
||||||
|
|
||||||
# read and parse the notebook
|
# read and parse the notebook
|
||||||
with open(nb_path) as f:
|
with open(nb_path) as f:
|
||||||
|
|||||||
Reference in New Issue
Block a user