Skip to content

Commit a525f3a

Browse files
authored
Merge pull request #1044 from juhasch/fix/html_embed
Fix html_embed exporter
2 parents 6998d21 + b5e3a98 commit a525f3a

File tree

1 file changed

+20
-1
lines changed
  • src/jupyter_contrib_nbextensions/nbconvert_support

1 file changed

+20
-1
lines changed

src/jupyter_contrib_nbextensions/nbconvert_support/embedhtml.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import base64
44
import re
55
from nbconvert.exporters.html import HTMLExporter
6+
from ipython_genutils.ipstruct import Struct
7+
import os
68

79
try:
810
from urllib.request import urlopen # py3
@@ -31,8 +33,20 @@ def replfunc(self, match):
3133
elif url.startswith('data'):
3234
img = '<img src="' + url + '"'
3335
return img
36+
elif url.startswith('attachment'):
37+
imgname = url.split(':')[1]
38+
available_formats = self.attachments[imgname]
39+
# get the image based on the configured image type priority
40+
for imgformat in self.config.NbConvertBase.display_data_priority:
41+
if imgformat in available_formats.keys():
42+
b64_data = self.attachments[imgname][imgformat]
43+
img = '<img src="data:' + imgformat + \
44+
';base64,' + b64_data + '"'
45+
return img
46+
raise ValueError('Could not find attachment for image "%s" in notebook' % imgname)
3447
else:
35-
with open(url, 'rb') as f:
48+
filename = os.path.join(self.path, url)
49+
with open(filename, 'rb') as f:
3650
data = f.read()
3751

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

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

5776
embedded_output = regex.sub(self.replfunc, output)

0 commit comments

Comments
 (0)