Skip to content

Commit

Permalink
Merge 50228c9 into d48fffd
Browse files Browse the repository at this point in the history
  • Loading branch information
jlstevens committed Aug 18, 2017
2 parents d48fffd + 50228c9 commit c1e4092
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
29 changes: 15 additions & 14 deletions holoviews/ipython/preprocessors.py
Expand Up @@ -72,17 +72,6 @@ def filter_magic(source, magic, strip=True):
return '\n'.join(filtered), magic_lines


def strip_magics(source):
"""
Given the source of a cell, filter out all cell and line magics.
"""
filtered=[]
for line in source.splitlines():
if not line.startswith('%') or line.startswith('%%'):
filtered.append(line)
return '\n'.join(filtered)


def replace_line_magic(source, magic, template='{line}'):
"""
Given a cell's source, replace line magics using a formatting
Expand Down Expand Up @@ -146,13 +135,25 @@ def __call__(self, nb, resources): return self.preprocess(nb,resources)
class StripMagicsProcessor(Preprocessor):
"""
Preprocessor to convert notebooks to Python source to strips out all
magics. To be applied after the preprocessors that can handle
holoviews magics appropriately.
magics. Should be applied after any more-specific magic
pre-processors that may be in use, to ensure that any remaining
magics are silently stripped to avoid Python syntax errors
"""

def strip_magics(self, source):
"""
Given the source of a cell, filter out all cell and line magics.
"""
filtered=[]
for line in source.splitlines():
if not line.startswith('%') or line.startswith('%%'):
filtered.append(line)
return '\n'.join(filtered)


def preprocess_cell(self, cell, resources, index):
if cell['cell_type'] == 'code':
cell['source'] = strip_magics(cell['source'])
cell['source'] = self.strip_magics(cell['source'])
return cell, resources

def __call__(self, nb, resources): return self.preprocess(nb,resources)
Expand Down
30 changes: 28 additions & 2 deletions tests/testnotebooks.py
Expand Up @@ -6,7 +6,8 @@

import os, sys
from holoviews.element.comparison import ComparisonTestCase
from holoviews.ipython.preprocessors import OptsMagicProcessor, OutputMagicProcessor
from holoviews.ipython.preprocessors import (OptsMagicProcessor, OutputMagicProcessor,
StripMagicsProcessor)


def apply_preprocessors(preprocessors, nbname):
Expand All @@ -19,6 +20,28 @@ def apply_preprocessors(preprocessors, nbname):
source, meta = exporter.from_notebook_node(nb)
return source


class TestStripMagicsPreprocessor(ComparisonTestCase):

def test_opts_image_line_magic_strip(self):
nbname = 'test_opts_image_line_magic.ipynb'
source = apply_preprocessors([StripMagicsProcessor()], nbname)
removed = "%opts Image [xaxis=None] (cmap='viridis')"
self.assertEqual(removed not in source, True)

def test_opts_image_cell_magic_strip(self):
nbname = 'test_opts_image_cell_magic.ipynb'
source = apply_preprocessors([StripMagicsProcessor()], nbname)
removed = "%%opts Image [xaxis=None] (cmap='viridis')"
self.assertEqual(removed not in source, True)

def test_opts_image_cell_magic_offset_strip(self):
nbname = 'test_opts_image_cell_magic_offset.ipynb'
source = apply_preprocessors([StripMagicsProcessor()], nbname)
removed = "%%opts Image [xaxis=None] (cmap='viridis')"
self.assertEqual(removed not in source, True)


class TestOptsPreprocessor(ComparisonTestCase):

def test_opts_image_line_magic(self):
Expand Down Expand Up @@ -55,7 +78,10 @@ def test_opts_image_cell_magic_offset(self):
source = apply_preprocessors([OptsMagicProcessor()], nbname)
self.assertEqual(source.strip().endswith(expected), False)

def test_opts_image_line_magic_svg(self):

class TestOutputPreprocessor(ComparisonTestCase):

def test_output_image_line_magic_svg(self):
nbname = 'test_output_svg_line_magic.ipynb'
if sys.version_info.major == 2:
expected = """hv.util.output(u" fig='svg'")"""
Expand Down

0 comments on commit c1e4092

Please sign in to comment.