ó
mÜJ]c           @` sÉ  d  Z  d d l m Z m Z m Z m Z d d l Z e j e ƒ Z	 d d l
 Z
 d d l Z d d l Z d d l m Z d d l m Z d d l m Z m Z d d l m Z d d	 l m Z d d
 l m Z d d l m Z d d l m Z d' Z d d d d d d „ Z  d d d d d d „ Z! d d l" m# Z# d d l" m$ Z$ d „  Z% d d d „ Z& d d d „ Z' e d „ Z( d „  Z) d  Z* d! Z+ d" Z, d d d d d# „ Z- d$ e. f d% „  ƒ  YZ/ d& „  Z0 d S((   u   

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   abspath(   t   mkstemp(   t
   raise_fromt   bi   (   t	   file_html(   t   INLINE(   t   import_required(   t   decode_utf8i   (   t   default_filenameu   create_webdriveru
   export_pngu   export_svgsu   get_layout_htmlu   get_screenshot_as_pngu   get_svgsu   terminate_webdriveru   webdriver_controli   c      
   C` sƒ   t  |  d | d | d | d | ƒ} | d k r? t d ƒ } n  | j d k s] | j d k rl t d ƒ ‚ n  | j | ƒ t | ƒ S(	   u¢   Export the ``LayoutDOM`` object or document as a PNG.

    If the filename is not given, it is derived from the script name (e.g.
    ``/foo/myplot.py`` will create ``/foo/myplot.png``)

    Args:
        obj (LayoutDOM or Document) : a Layout (Row/Column), Plot or Widget
            object or Document to export.

        filename (str, optional) : filename to save document under (default: None)
            If None, infer from the filename.

        height (int) : the desired height of the exported layout obj only if
            it's a Plot instance. Otherwise the height kwarg is ignored.

        width (int) : the desired width of the exported layout obj only if
            it's a Plot instance. Otherwise the width kwarg is ignored.

        webdriver (selenium.webdriver) : a selenium webdriver instance to use
            to export the image.

        timeout (int) : the maximum amount of time (in seconds) to wait for
            Bokeh to initialize (default: 5) (Added in 1.1.1).

    Returns:
        filename (str) : the filename where the static file is saved.

    If you would like to access an Image object directly, rather than save a
    file to disk, use the lower-level :func:`~bokeh.io.export.get_screenshot_as_png`
    function.

    .. warning::
        Responsive sizing_modes may generate layouts with unexpected size and
        aspect ratios. It is recommended to use the default ``fixed`` sizing mode.

    t   heightt   widtht   drivert   timeoutu   pngi    u   unable to save an empty imageN(   t   get_screenshot_as_pngt   NoneR   R   R   t
   ValueErrort   saveR   (   t   objt   filenameR   R   t	   webdriverR   t   image(    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyt
   export_png;   s    &$c      
   C` s
  t  |  d | d | d | d | ƒ} t | ƒ d k rG t j d ƒ d S| d k rb t d ƒ } n  g  } x› t | ƒ D] \ } }	 | d k r– | } n. | j d	 ƒ }
 | |
  d
 j | ƒ | |
 } t	 j
 | d d d d ƒ } | j |	 ƒ Wd QX| j | ƒ qu W| S(   u   Export the SVG-enabled plots within a layout. Each plot will result
    in a distinct SVG file.

    If the filename is not given, it is derived from the script name
    (e.g. ``/foo/myplot.py`` will create ``/foo/myplot.svg``)

    Args:
        obj (LayoutDOM object) : a Layout (Row/Column), Plot or Widget object to display

        filename (str, optional) : filename to save document under (default: None)
            If None, infer from the filename.

        height (int) : the desired height of the exported layout obj only if
            it's a Plot instance. Otherwise the height kwarg is ignored.

        width (int) : the desired width of the exported layout obj only if
            it's a Plot instance. Otherwise the width kwarg is ignored.

        webdriver (selenium.webdriver) : a selenium webdriver instance to use
            to export the image.

        timeout (int) : the maximum amount of time (in seconds) to wait for
            Bokeh to initialize (default: 5) (Added in 1.1.1).

    Returns:
        filenames (list(str)) : the list of filenames where the SVGs files are
        saved.

    .. warning::
        Responsive sizing_modes may generate layouts with unexpected size and
        aspect ratios. It is recommended to use the default ``fixed`` sizing mode.

    R   R   R   R   i    u   No SVG Plots were found.Nu   svgu   .svgu   _{}t   modeu   wt   encodingu   utf-8(   t   get_svgst   lent   logt   warningR   R   t	   enumeratet   findt   formatt   iot   opent   writet   append(   R   R   R   R   R   R   t   svgst	   filenamest   it   svgt   idxt   f(    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyt   export_svgsm   s     "$	(   t   webdriver_control(   t   terminate_webdriverc           C` s
   t  j ƒ  S(   u†    Create a new webdriver.

    .. note ::
        Here for compatibility. Prefer methods on the webdriver_control
        object.

    (   R.   t   create(    (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyt   create_webdriver°   s    c         K` s
  t  d d d ƒ } t ƒ  Á } t |  |  } t j | j d d d d ƒ } | j t | ƒ ƒ Wd QX| d k	 rz | n	 t	 j
 ƒ  } | j
 d	 | j ƒ | j ƒ  | j d
 ƒ t | | ƒ | j ƒ  }	 | j t ƒ }
 Wd QX| j t j |	 ƒ ƒ } t | |
  } | S(   uô   Get a screenshot of a ``LayoutDOM`` object.

    Args:
        obj (LayoutDOM or Document) : a Layout (Row/Column), Plot or Widget
            object or Document to export.

        driver (selenium.webdriver) : a selenium webdriver instance to use
            to export the image.

        timeout (int) : the maximum amount of time to wait for initialization.
            It will be used as a timeout for loading Bokeh, then when waiting for
            the layout to be rendered.

    Returns:
        cropped_image (PIL.Image.Image) : a pillow image loaded from PNG.

    .. warning::
        Responsive sizing_modes may generate layouts with unexpected size and
        aspect ratios. It is recommended to use the default ``fixed`` sizing mode.

    u	   PIL.Imageu+   To use bokeh.io.export_png you need pillow u0   ("conda install pillow" or "pip install pillow")R   u   wR   u   utf-8Nu   file:///u#   document.body.style.width = '100%';(   R
   t	   _tmp_htmlt   get_layout_htmlR#   R$   t   pathR%   R   R   R.   t   gett   maximize_windowt   execute_scriptt   wait_until_render_completeR   t   _BOUNDING_RECT_SCRIPTt   BytesIOt   _crop_image(   R   R   R   t   kwargst   Imaget   tmpt   htmlt   filet
   web_drivert   pngt   b_rectR   t   cropped_image(    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyR   º   s"    
!
c   	      K` s§   t  ƒ  ˜ } t |  |  } t j | j d d ƒ } | j t | ƒ ƒ Wd QX| d k	 ra | n	 t j	 ƒ  } | j	 d | j ƒ t
 | | ƒ | j t ƒ } Wd QX| S(   u   

    R   u   wbNu   file:///(   R2   R3   R#   R$   R4   R%   R   R   R.   R5   R8   R7   t   _SVG_SCRIPT(	   R   R   R   R<   R>   R?   R@   RA   R'   (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyR   ì   s    c      	   K` sì   t  } | j d ƒ d
 k	 s0 | j d ƒ d
 k	 r¤ d d l m } t |  | ƒ s_ t j d ƒ q¤ t } |  j	 } |  j
 } | j d | ƒ |  _	 | j d | ƒ |  _
 n  z% t |  | d d d t d	 t ƒ} Wd
 | rç | |  _	 | |  _
 n  X| S(   u   

    u   heightu   widthi   (   t   Plotug   Export method called with height or width kwargs on a non-Plot layout. The size values will be ignored.t   titleu    t   suppress_callback_warningt   _always_newN(   t   FalseR5   R   t   models.plotsRF   t
   isinstancet   warningst   warnt   Truet   plot_heightt
   plot_widthR   (   R   t	   resourcesR<   t   resizeRF   t
   old_heightt	   old_widthR?   (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyR3   ÿ   s     *		%	c      	   C` s}  d d l  m } d d l m } d d l m } d „  } y  | |  | d d ƒj | ƒ Wn& | k
 r } t t d ƒ | ƒ n X|  j	 t
 ƒ d	 „  } zE y  | |  | d d ƒj | ƒ Wn | k
 rÛ t j d
 ƒ n XWd t |  | ƒ sx|  j d ƒ } g  | D]* }	 |	 j d ƒ d k r|	 j d ƒ ^ q}
 t |
 ƒ d k rxt j d ƒ x |
 D] } t j | ƒ q[Wqxn  Xd S(   u   

    i    (   t   WebDriverWait(   t   TimeoutException(   t   Firefoxc         S` s   |  j  d ƒ S(   Nul   
            const b = window.Bokeh;
            return b && b.documents && b.documents.length > 0;
        (   R7   (   R   (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyt   is_bokeh_loaded!  s    t   poll_frequencygš™™™™™¹?u<   Bokeh was not loaded in time. Something may have gone wrong.c         S` s   |  j  d ƒ S(   Nu%   return window._bokeh_render_complete;(   R7   (   R   (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyt   is_bokeh_render_complete.  s    u–   The webdriver raised a TimeoutException while waiting for a 'bokeh:idle' event to signify that the layout has rendered. Something may have gone wrong.Nu   browseru   levelu   WARNINGu   ERRORu   SEVEREu   messageuL   There were browser warnings and/or errors that may have affected your export(   u   WARNINGu   ERRORu   SEVERE(   t   selenium.webdriver.support.uiRV   t   selenium.common.exceptionsRW   t   selenium.webdriverRX   t   untilR   t   RuntimeErrorR7   t   _WAIT_SCRIPTR   R   RL   t   get_logR5   R   (   R   R   RV   RW   RX   RY   t   eR[   t   browser_logst   lt   messagest   message(    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyR8     s,    	 	  7uZ   
return document.getElementsByClassName('bk-root')[0].children[0].getBoundingClientRect()
u  
var serialized_svgs = [];
var svgs = document.getElementsByClassName('bk-root')[0].getElementsByTagName("svg");
for (var i = 0; i < svgs.length; i++) {
    var source = (new XMLSerializer()).serializeToString(svgs[i]);
    serialized_svgs.push(source);
};
return serialized_svgs
u  
// add private window prop to check that render is complete
window._bokeh_render_complete = false;
function done() {
  window._bokeh_render_complete = true;
}

var doc = window.Bokeh.documents[0];

if (doc.is_idle)
  done();
else
  doc.idle.connect(done);
c         K` s   |  j  | | | | f ƒ S(   u&    Crop the border from the layout

    (   t   crop(   R   t   leftt   topt   rightt   bottomR<   (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyR;   b  s    t	   _TempFilec           B` sA   e  Z e Z d  d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   u   tmpu    c         C` s%   t  d | d | ƒ \ |  _ |  _ d  S(   Nt   prefixt   suffix(   R   t   fdR4   (   t   selfRn   Ro   (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyt   __init__l  s    c         C` s   |  S(   N(    (   Rq   (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyt	   __enter__o  s    c         C` s   |  j  ƒ  d  S(   N(   t   close(   Rq   t   exct   valuet   tb(    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyt   __exit__r  s    c         C` s   |  j  ƒ  d  S(   N(   Rt   (   Rq   (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyt   __del__u  s    c         C` s˜   |  j  r d  Sz2 y t j |  j ƒ Wn t t f k
 r= n XWd  d  |  _ Xz2 y t j |  j ƒ Wn t t f k
 r| n XWd  d  |  _ Xt	 |  _  d  S(   N(
   t   _closedt   osRt   Rp   t   OSErrort   IOErrorR   t   unlinkR4   RO   (   Rq   (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyRt   x  s    	 
 
(	   t   __name__t
   __module__RJ   Rz   Rr   Rs   Rx   Ry   Rt   (    (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyRm   h  s   			c           C` s   t  d d d d ƒ S(   NRn   u   bokehRo   u   .html(   Rm   (    (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyR2   Œ  s    (   u   create_webdriveru
   export_pngu   export_svgsu   get_layout_htmlu   get_screenshot_as_pngu   get_svgsu   terminate_webdriveru   webdriver_control(1   t   __doc__t
   __future__R    R   R   R   t   loggingt	   getLoggerR   R   R{   R#   RM   t   os.pathR   t   tempfileR   t   sixR   R   t   embedR   RR   R	   t   util.dependenciesR
   t   util.stringR   t   utilR   t   __all__R   R   R-   R   R.   R/   R1   R   R   R3   R8   R9   RE   Ra   R;   t   objectRm   R2   (    (    (    s.   lib/python2.7/site-packages/bokeh/io/export.pyt   <module>	   sJ   "       2@	
2	.
$