Skip to content

Commit

Permalink
Fix #2733 -- allow only modern Jupyter versions
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed May 25, 2017
1 parent 9e901ed commit 4604b16
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
Expand Up @@ -28,6 +28,12 @@ Bugfixes
* Use page.tmpl by default, which is inherited from story.tmpl (Issue
#1891)

Other
-----

* Limit Jupyter support to notebook >= 4.0.0 (it already was in
requirements-extras.txt; Issue #2733)

New in v7.8.5
=============

Expand Down
47 changes: 11 additions & 36 deletions nikola/plugins/compile/ipynb.py
Expand Up @@ -39,27 +39,8 @@
from jupyter_client import kernelspec
from traitlets.config import Config
flag = True
ipy_modern = True
except ImportError:
try:
import IPython
from IPython.nbconvert.exporters import HTMLExporter
if IPython.version_info[0] >= 3: # API changed with 3.0.0
from IPython import nbformat
current_nbformat = nbformat.current_nbformat
from IPython.kernel import kernelspec
ipy_modern = True
else:
import IPython.nbformat.current as nbformat
current_nbformat = 'json'
kernelspec = None
ipy_modern = False

from IPython.config import Config
flag = True
except ImportError:
flag = None
ipy_modern = None
flag = None

from nikola import shortcodes as sc
from nikola.plugin_categories import PageCompiler
Expand All @@ -81,8 +62,7 @@ def set_site(self, site):

def _compile_string(self, nb_json):
"""Export notebooks as HTML strings."""
if flag is None:
req_missing(['ipython[notebook]>=2.0.0'], 'build this site (compile ipynb)')
self._req_missing_ipynb()
c = Config(self.site.config['IPYNB_CONFIG'])
c.update(get_default_jupyter_config())
exportHtml = HTMLExporter(config=c)
Expand All @@ -93,6 +73,10 @@ def _compile_string(self, nb_json):
def _nbformat_read(in_file):
return nbformat.read(in_file, current_nbformat)

def _req_missing_ipynb(self):
if flag is None:
req_missing(['notebook>=4.0.0'], 'build this site (compile ipynb)')

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile notebooks into HTML strings."""
new_data, shortcodes = sc.extract_shortcodes(data)
Expand Down Expand Up @@ -128,8 +112,7 @@ def read_metadata(self, post, file_metadata_regexp=None, unslugify_titles=False,
As ipynb file support arbitrary metadata as json, the metadata used by Nikola
will be assume to be in the 'nikola' subfield.
"""
if flag is None:
req_missing(['ipython[notebook]>=2.0.0'], 'build this site (compile ipynb)')
self._req_missing_ipynb()
source = post.source_path
with io.open(source, "r", encoding="utf8") as in_file:
nb_json = nbformat.read(in_file, current_nbformat)
Expand All @@ -139,8 +122,7 @@ def read_metadata(self, post, file_metadata_regexp=None, unslugify_titles=False,

def create_post(self, path, **kw):
"""Create a new post."""
if flag is None:
req_missing(['ipython[notebook]>=2.0.0'], 'build this site (compile ipynb)')
self._req_missing_ipynb()
content = kw.pop('content', None)
onefile = kw.pop('onefile', False)
kernel = kw.pop('ipython_kernel', None)
Expand All @@ -157,12 +139,8 @@ def create_post(self, path, **kw):
# imported .ipynb file, guaranteed to start with "{" because it’s JSON.
nb = nbformat.reads(content, current_nbformat)
else:
if ipy_modern:
nb = nbformat.v4.new_notebook()
nb["cells"] = [nbformat.v4.new_markdown_cell(content)]
else:
nb = nbformat.new_notebook()
nb["worksheets"] = [nbformat.new_worksheet(cells=[nbformat.new_text_cell('markdown', [content])])]
nb = nbformat.v4.new_notebook()
nb["cells"] = [nbformat.v4.new_markdown_cell(content)]

if kernelspec is not None:
if kernel is None:
Expand Down Expand Up @@ -190,10 +168,7 @@ def create_post(self, path, **kw):
nb["metadata"]["nikola"] = metadata

with io.open(path, "w+", encoding="utf8") as fd:
if ipy_modern:
nbformat.write(nb, fd, 4)
else:
nbformat.write(nb, fd, 'ipynb')
nbformat.write(nb, fd, 4)


def get_default_jupyter_config():
Expand Down

0 comments on commit 4604b16

Please sign in to comment.