python-visualization/branca

HTML Popup

samchorlton opened this issue · 1 comments

I couldn't find a way of embedding Html in Popups without the use of iframes. Iframes are sometimes overkill for simple nested links or browser compatability issues so I create a HTMLPopup class for element.py. Would be great if you could add this it the master branch.

class HTMLPopup(Element):
    """Create a Figure object, to plot things into it.

    Parameters
    ----------
    html : str, default None
        Eventual HTML code that you want to put in the frame.
    width : str, default "100%"
        The width of the Figure.
        It may be a percentage or pixel value (like "300px").
    height : str, default None
        The height of the Figure.
        It may be a percentage or a pixel value (like "300px").
    ratio : str, default "60%"
        A percentage defining the aspect ratio of the Figure.
        It will be ignored if height is not None.
    figsize : tuple of two int, default None
        If you're a matplotlib addict, you can overwrite width and
        height. Values will be converted into pixels in using 60 dpi.
        For example figsize=(10, 5) will result in
        width="600px", height="300px".
    """
    def __init__(self, html=None, width="100%", height=None, ratio="60%",
                 figsize=None):
        super(HTMLPopup, self).__init__()
        self._name = 'HTMLPopup'

        self.width = width
        self.height = height
        self.ratio = ratio
        if figsize is not None:
            self.width = str(60*figsize[0])+'px'
            self.height = str(60*figsize[1])+'px'

        if isinstance(html, text_type) or isinstance(html, binary_type):
            self.add_children(Element(html))
        elif html is not None:
            self.add_children(html)

    def render(self, **kwargs):
        """Renders the HTML representation of the element."""
        html = super(HTMLPopup, self).render(**kwargs)
        html = html  # noqa

        if self.height is None:
            htmlpopup = (
            '<div style="width:{width};">'
            '<div style="position:relative;width:100%;height:0;padding-bottom:{ratio};">'  # noqa
            '{html}'
            '</div></div>').format
            htmlpopup = htmlpopup(html=html,
                            width=self.width,
                            ratio=self.ratio)
        else:
            htmlpopup = ('<div width="{width}" '
                      'height="{height}">{html}</div>').format
            htmlpopup = htmlpopup(html=html, width=self.width, height=self.height)
        return htmlpopup

Closed in favor of #6