Skip to content

Commit

Permalink
Merge pull request #1044 from juhasch/fix/html_embed
Browse files Browse the repository at this point in the history
Fix html_embed exporter
  • Loading branch information
juhasch authored Aug 19, 2017
2 parents 6998d21 + b5e3a98 commit a525f3a
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/jupyter_contrib_nbextensions/nbconvert_support/embedhtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import base64
import re
from nbconvert.exporters.html import HTMLExporter
from ipython_genutils.ipstruct import Struct
import os

try:
from urllib.request import urlopen # py3
Expand Down Expand Up @@ -31,8 +33,20 @@ def replfunc(self, match):
elif url.startswith('data'):
img = '<img src="' + url + '"'
return img
elif url.startswith('attachment'):
imgname = url.split(':')[1]
available_formats = self.attachments[imgname]
# get the image based on the configured image type priority
for imgformat in self.config.NbConvertBase.display_data_priority:
if imgformat in available_formats.keys():
b64_data = self.attachments[imgname][imgformat]
img = '<img src="data:' + imgformat + \
';base64,' + b64_data + '"'
return img
raise ValueError('Could not find attachment for image "%s" in notebook' % imgname)
else:
with open(url, 'rb') as f:
filename = os.path.join(self.path, url)
with open(filename, 'rb') as f:
data = f.read()

self.log.info("embedding url: %s, format: %s" % (url, imgformat))
Expand All @@ -52,6 +66,11 @@ def from_notebook_node(self, nb, resources=None, **kw):
output, resources = super(
EmbedHTMLExporter, self).from_notebook_node(nb, resources)

self.path = resources['metadata']['path']
self.attachments = Struct()
for cell in nb.cells:
if 'attachments' in cell.keys():
self.attachments += cell['attachments']
regex = re.compile('<img\s+src="([^"]+)"')

embedded_output = regex.sub(self.replfunc, output)
Expand Down

0 comments on commit a525f3a

Please sign in to comment.