Skip to content

Commit

Permalink
Added support for inline and exported pdf output
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed May 27, 2015
1 parent 560e5da commit 1e8a59b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
3 changes: 3 additions & 0 deletions holoviews/ipython/display_hooks.py
Expand Up @@ -177,6 +177,9 @@ def display_figure(fig, message=None, allow_nbagg=True, max_width='100%'):
figdata = renderer.figure_data(fig, figure_format)
if figure_format=='svg':
figdata = figdata.encode("utf-8")
if figure_format=='pdf':
w, h = fig.get_size_inches()
css['height'] = '%dpx' % (h*fig.get_dpi())
b64 = base64.b64encode(figdata).decode("utf-8")
(mime_type, tag) = MIME_TYPES[figure_format], HTML_TAGS[figure_format]
src = HTML_TAGS['base64'].format(mime_type=mime_type, b64=b64)
Expand Down
15 changes: 10 additions & 5 deletions holoviews/ipython/magics.py
Expand Up @@ -14,6 +14,8 @@
from IPython.display import display, HTML
from ..operation import Compositor

import param

#========#
# Magics #
#========#
Expand Down Expand Up @@ -155,6 +157,10 @@ def _extract_keywords(cls, line, items):
return items


class OutputWarning(param.Parameterized):pass
outputwarning = OutputWarning(name='Warning')


@magics_class
class OutputMagic(OptionsMagic):
"""
Expand All @@ -169,7 +175,7 @@ class OutputMagic(OptionsMagic):

# Lists: strict options, Set: suggested options, Tuple: numeric bounds.
allowed = {'backend' : ['mpl','d3', 'nbagg'],
'fig' : ['svg', 'png', 'repr'],
'fig' : ['svg', 'png', 'repr', 'pdf'],
'holomap' : inbuilt_formats,
'widgets' : ['embed', 'live'],
'fps' : (0, float('inf')),
Expand Down Expand Up @@ -277,10 +283,9 @@ def _validate(cls, options):
if options['holomap'] not in allowed:
raise ValueError("The D3 backend only supports holomap options %r" % allowed)

if (options['holomap']=='widgets'
and options['widgets']!='embed'
and options['fig']=='svg'):
raise ValueError("SVG mode not supported by widgets unless in embed mode")
if options['fig']=='pdf' and not cls.options['fig'] == 'pdf':
outputwarning.warning("PDF output is experimental, may not be supported"
"by your browser and may change in future.")
return options


Expand Down
13 changes: 8 additions & 5 deletions holoviews/plotting/__init__.py
Expand Up @@ -29,10 +29,11 @@
# Tags used when matplotlib output is to be embedded in HTML
IMAGE_TAG = "<img src='{src}' style='max-width:100%; margin: auto; display: block; {css}'/>"
VIDEO_TAG = """
<center><video controls style='max-width:100%; margin: auto; display: block; {css}'>
<video controls style='max-width:100%; margin: auto; display: block; {css}'>
<source src='{src}' type='{mime_type}'>
Your browser does not support the video tag.
</video><center/>"""
</video>"""
PDF_TAG = "<iframe src='{src}' style='width:100%; margin: auto; display: block; {css}'></iframe>"


HTML_TAGS = {
Expand All @@ -41,15 +42,17 @@
'png': IMAGE_TAG,
'gif': IMAGE_TAG,
'webm': VIDEO_TAG,
'mp4': VIDEO_TAG
'mp4': VIDEO_TAG,
'pdf': PDF_TAG
}

MIME_TYPES = {
'svg': 'image/svg+xml',
'png': 'image/png',
'gif': 'image/gif',
'webm': 'video/webm',
'mp4': 'video/mp4'
'mp4': 'video/mp4',
'pdf': 'application/pdf'
}

# <format name> : (animation writer, format, anim_kwargs, extra_args)
Expand Down Expand Up @@ -109,7 +112,7 @@ class MPLPlotRenderer(Exporter):
"""

fig = param.ObjectSelector(default='svg',
objects=['png', 'svg', None], doc="""
objects=['png', 'svg', 'pdf', None], doc="""
Output render format for static figures. If None, no figure
rendering will occur. """)

Expand Down

0 comments on commit 1e8a59b

Please sign in to comment.