-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from maartenbreddels/feature_render_images_in…
…line new: render image inline in html
- Loading branch information
Showing
5 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# tests the --template argument of voila | ||
import pytest | ||
import base64 | ||
import os | ||
|
||
@pytest.fixture | ||
def voila_notebook(notebook_directory): | ||
return os.path.join(notebook_directory, 'images.ipynb') | ||
|
||
|
||
@pytest.mark.gen_test | ||
def test_template_gridstack(http_client, base_url, notebook_directory): | ||
response = yield http_client.fetch(base_url) | ||
html_text = response.body.decode('utf-8') | ||
|
||
assert 'data:image/svg+xml;base64,' in html_text | ||
assert 'data:image/png;base64,' in html_text | ||
|
||
# check if the external file is inline | ||
with open(os.path.join(notebook_directory, notebook_directory, 'quantstack.svg'), 'rb') as f: | ||
svg_data = f.read() | ||
svg_data_base64 = base64.b64encode(svg_data).decode('ascii') | ||
assert svg_data_base64 in html_text | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import mimetypes | ||
|
||
import nbconvert.exporters.html | ||
from jinja2 import contextfilter | ||
from nbconvert.filters.markdown_mistune import IPythonRenderer, MarkdownWithMath | ||
|
||
|
||
class VoilaMarkdownRenderer(IPythonRenderer): | ||
"""Custom markdown renderer that inlines images""" | ||
def image(self, src, title, text): | ||
contents_manager = self.options['contents_manager'] | ||
if contents_manager.file_exists(src): | ||
content = contents_manager.get(src, format='base64') | ||
data = content['content'].replace('\n', '') # remove the newline | ||
mime_type, encoding = mimetypes.guess_type(src) | ||
src = 'data:{mime_type};base64,{data}'.format(mime_type=mime_type, data=data) | ||
return super(VoilaMarkdownRenderer, self).image(src, title, text) | ||
|
||
|
||
class HTMLExporter(nbconvert.exporters.html.HTMLExporter): | ||
"""Custom HTMLExporter that inlines the images using VoilaMarkdownRenderer""" | ||
@contextfilter | ||
def markdown2html(self, context, source): | ||
cell = context['cell'] | ||
attachments = cell.get('attachments', {}) | ||
renderer = VoilaMarkdownRenderer(escape=False, attachments=attachments, | ||
contents_manager=self.contents_manager, | ||
anchor_link_text=self.anchor_link_text) | ||
return MarkdownWithMath(renderer=renderer).render(source) |