Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2733 -- allow only modern Jupyter versions #2797

Merged
merged 1 commit into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(That was supposed to appear under line 76…)



def get_default_jupyter_config():
Expand Down