Browse Source

Merge pull request #543 from aavanian/liquid_tags_forward

Improve ipython forward compatibility (v4.x), also ref #542, #536
Justin Mayer 9 years ago
parent
commit
da4487d6be
1 changed files with 38 additions and 11 deletions
  1. 38 11
      liquid_tags/notebook.py

+ 38 - 11
liquid_tags/notebook.py

@@ -53,27 +53,51 @@ from .mdx_liquid_tags import LiquidTags
 import IPython
 IPYTHON_VERSION = IPython.version_info[0]
 
+try:
+    import nbformat
+except:
+    pass
+
 if not IPYTHON_VERSION >= 1:
     raise ValueError("IPython version 1.0+ required for notebook tag")
 
 try:
-    from IPython.nbconvert.filters.highlight import _pygments_highlight
+    from nbconvert.filters.highlight import _pygments_highlight
 except ImportError:
-    # IPython < 2.0
-    from IPython.nbconvert.filters.highlight import _pygment_highlight as _pygments_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 IPython.nbconvert.exporters import HTMLExporter
-from IPython.config import Config
+try:
+    from nbconvert.exporters import HTMLExporter
+except ImportError:
+        print("I was here 3")
+        from IPython.nbconvert.exporters import HTMLExporter
 
 try:
-    from IPython.nbconvert.preprocessors import Preprocessor
+    from traitlets.config import Config
 except ImportError:
-    # IPython < 2.0
-    from IPython.nbconvert.transformers import Transformer as Preprocessor
+    from IPython.config import Config
+
+try:
+    from nbconvert.preprocessors import Preprocessor
+except ImportError:
+    try:
+        print("I was here 4")
+        from IPython.nbconvert.preprocessors import Preprocessor
+    except ImportError:
+        # IPython < 2.0
+        from IPython.nbconvert.transformers import Transformer as Preprocessor
+
+try:
+    from traitlets import Integer
+except ImportError:
+    from IPython.utils.traitlets import Integer
 
-from IPython.utils.traitlets import Integer
 from copy import deepcopy
 
 #----------------------------------------------------------------------
@@ -209,7 +233,7 @@ class SubCell(Preprocessor):
                 worksheet.cells = cells[self.start:self.end]
         else:
             nbc.cells = nbc.cells[self.start:self.end]
-        
+
         return nbc, resources
 
     call = preprocess # IPython < 2.0
@@ -298,7 +322,10 @@ def notebook(preprocessor, tag, markup):
         if IPYTHON_VERSION < 3:
             nb_json = IPython.nbformat.current.reads_json(nb_text)
         else:
-            nb_json = IPython.nbformat.reads(nb_text, as_version=4)
+            try:
+                nb_json = nbformat.reads(nb_text, as_version=4)
+            except:
+                nb_json = IPython.nbformat.reads(nb_text, as_version=4)
 
     (body, resources) = exporter.from_notebook_node(nb_json)