Created
August 19, 2015 16:15
-
-
Save KristoforMaynard/3c20d243050598562b64 to your computer and use it in GitHub Desktop.
Embed raw media in an ipython notebook
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
"""Embed raw media in an ipython notebook | |
The point of embedding raw media is that one can save as html, and | |
the media will automatically be embedded in the result. | |
Installation: | |
Place in ~/.ipython/extensions/, or wherever your extensions live. | |
Note: If the notebook server is not local, then embedding media may | |
cause notebooks to take a while to load since the media is part | |
of the html as opposed to an external resource. | |
Examples: | |
>>> %load_ext iembedmedia | |
>>> iembed_media("$ENVVAR/path/to/figure.png", width='30%') | |
>>> iembed_media("/path/to/figure.pdf", width='350px', height='242px') | |
>>> iembed_media("~/Documents/video.mp4", width="50%") | |
""" | |
from __future__ import print_function, division | |
import base64 | |
import os | |
import io | |
import sys | |
from IPython.display import HTML | |
__author__ = "Kristofor Maynard" | |
__copyright__ = "Copyright 2015" | |
__license__ = "MIT License" | |
def iembed_media(fname, **kwargs): | |
"""Embed raw media in an ipython notebook | |
Args: | |
fname (str): media's file name (tildas and envvars will be expanded) | |
**kwargs: added to the html for the main tag | |
Example: | |
>>> iembed_media("~/Documents/video.mp4", width="50%") | |
Returns: | |
None | |
""" | |
fname = os.path.expandvars(os.path.expanduser(fname)) | |
ext = os.path.splitext(fname)[1][1:] | |
mtype = "" | |
markup = "" | |
if ext in ['png', 'jpg', 'jpeg', 'bmp']: | |
mtype = "image/{0}".format(ext) | |
markup = '''<img {html_attribs} type="{mtype}" src="{media}"/>''' | |
elif ext in ['pdf']: | |
mtype = "application/{0}".format(ext) | |
markup = '''<object {html_attribs} type="{mtype}" data="{media}"> | |
<p>Oops, can't display inline PDFs, try using | |
Google Chrome</p> | |
<param name="view" value="Fit" /> | |
<param name="view" value="fitH" /> | |
</object>''' | |
elif ext in ['mp4', 'ogg']: | |
mtype = "video/{0}".format(ext) | |
markup = '''<video {html_attribs} controls> | |
<source type="{mtype}" src="{media}" /> | |
<p>Oops, your browser does not support the video tag.</p> | |
</video>''' | |
else: | |
print("Unrecognized file type:", ext, file=sys.stderr) | |
if markup: | |
html_attribs = "" | |
for k, v in kwargs.items(): | |
html_attribs += "{0}='{1}' ".format(k, v) | |
enc_media = base64.b64encode(io.open(fname, 'r+b').read()) | |
media = "data:{0};base64,{1}".format(mtype, enc_media.decode('ascii')) | |
return HTML(markup.format(html_attribs=html_attribs, mtype=mtype, | |
media=media)) | |
def load_ipython_extension(ipython): | |
ipython.push("iembed_media", interactive=True) | |
def unload_ipython_extension(ipython): | |
ipython.drop_by_id(dict(iembed_media=iembed_media)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment