test_html.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """Tests for HTMLExporter"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import re
  5. from .base import ExportersTestsBase
  6. from ..html import HTMLExporter
  7. from traitlets.config import Config
  8. from nbformat import v4
  9. class TestHTMLExporter(ExportersTestsBase):
  10. """Tests for HTMLExporter"""
  11. exporter_class = HTMLExporter
  12. should_include_raw = ['html']
  13. def test_constructor(self):
  14. """
  15. Can a HTMLExporter be constructed?
  16. """
  17. HTMLExporter()
  18. def test_export(self):
  19. """
  20. Can a HTMLExporter export something?
  21. """
  22. (output, resources) = HTMLExporter().from_filename(self._get_notebook())
  23. assert len(output) > 0
  24. def test_export_basic(self):
  25. """
  26. Can a HTMLExporter export using the 'basic' template?
  27. """
  28. (output, resources) = HTMLExporter(template_file='basic').from_filename(self._get_notebook())
  29. assert len(output) > 0
  30. def test_export_full(self):
  31. """
  32. Can a HTMLExporter export using the 'full' template?
  33. """
  34. (output, resources) = HTMLExporter(template_file='full').from_filename(self._get_notebook())
  35. assert len(output) > 0
  36. def test_prompt_number(self):
  37. """
  38. Does HTMLExporter properly format input and output prompts?
  39. """
  40. (output, resources) = HTMLExporter(template_file='full').from_filename(
  41. self._get_notebook(nb_name="prompt_numbers.ipynb"))
  42. in_regex = r"In \[(.*)\]:"
  43. out_regex = r"Out\[(.*)\]:"
  44. ins = ["2", "10", " ", " ", "0"]
  45. outs = ["10"]
  46. assert re.findall(in_regex, output) == ins
  47. assert re.findall(out_regex, output) == outs
  48. def test_prompt_number(self):
  49. """
  50. Does HTMLExporter properly format input and output prompts?
  51. """
  52. no_prompt_conf = Config(
  53. {"TemplateExporter":{
  54. "exclude_input_prompt": True,
  55. "exclude_output_prompt": True,
  56. }
  57. }
  58. )
  59. exporter = HTMLExporter(config=no_prompt_conf, template_file='full')
  60. (output, resources) = exporter.from_filename(
  61. self._get_notebook(nb_name="prompt_numbers.ipynb"))
  62. in_regex = r"In \[(.*)\]:"
  63. out_regex = r"Out\[(.*)\]:"
  64. assert not re.findall(in_regex, output)
  65. assert not re.findall(out_regex, output)
  66. def test_png_metadata(self):
  67. """
  68. Does HTMLExporter with the 'basic' template treat pngs with width/height metadata correctly?
  69. """
  70. (output, resources) = HTMLExporter(template_file='basic').from_filename(
  71. self._get_notebook(nb_name="pngmetadata.ipynb"))
  72. check_for_png = re.compile(r'<img src="[^"]*?"([^>]*?)>')
  73. result = check_for_png.search(output)
  74. attr_string = result.group(1)
  75. assert 'width' in attr_string
  76. assert 'height' in attr_string
  77. def test_javascript_output(self):
  78. nb = v4.new_notebook(
  79. cells=[
  80. v4.new_code_cell(
  81. outputs=[v4.new_output(
  82. output_type='display_data',
  83. data={
  84. 'application/javascript': "javascript_output();"
  85. }
  86. )]
  87. )
  88. ]
  89. )
  90. (output, resources) = HTMLExporter(template_file='basic').from_notebook_node(nb)
  91. self.assertIn('javascript_output', output)
  92. def test_attachments(self):
  93. (output, resources) = HTMLExporter(template_file='basic').from_file(
  94. self._get_notebook(nb_name='attachment.ipynb')
  95. )
  96. check_for_png = re.compile(r'<img src="[^"]*?"([^>]*?)>')
  97. result = check_for_png.search(output)
  98. self.assertTrue(result.group(0).strip().startswith('<img src="data:image/png;base64,iVBOR'))
  99. self.assertTrue(result.group(1).strip().startswith('alt="image.png"'))
  100. check_for_data = re.compile(r'<img src="(?P<url>[^"]*?)"')
  101. results = check_for_data.findall(output)
  102. assert results[0] != results[1], 'attachments only need to be unique within a cell'
  103. assert 'image/svg' in results[1], 'second image should use svg'
  104. def test_custom_filter_highlight_code(self):
  105. # Overwriting filters takes place at: Exporter.from_notebook_node
  106. nb = v4.new_notebook()
  107. nb.cells.append(v4.new_code_cell("some_text"))
  108. def custom_highlight_code(source, language="python", metadata=None):
  109. return source + " ADDED_TEXT"
  110. filters = {
  111. "highlight_code": custom_highlight_code
  112. }
  113. (output, resources) = HTMLExporter(template_file='basic', filters=filters).from_notebook_node(nb)
  114. self.assertTrue("ADDED_TEXT" in output)