B
    @\H.                 @   s<  d Z ddlmZmZmZmZ ddlZe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Zd)ddZ ddl!m"Z" ddl!m#Z# dd Z$d*ddZ%d+ddZ&efddZ'dd Z(dZ)d Z*d!Z+d,d"d#Z,G d$d% d%e-Z.d&d' Z/dS )-z

    )absolute_importdivisionprint_functionunicode_literalsN)abspath)mkstemp)
raise_fromb   )	file_html)INLINE)import_required)decode_utf8   )default_filename)create_webdriver
export_pngexport_svgsget_layout_htmlget_screenshot_as_pngget_svgsterminate_webdriverwebdriver_controlc             C   s2   t | |||d}|dkr td}|| t|S )ae   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.

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

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

    )heightwidthdriverNpng)r   r   Zsaver   )objfilenamer   r   	webdriverimage r!   .lib/python3.7/site-packages/bokeh/io/export.pyr   ;   s
    
r   c          
   C   s   t | |||d}t|dkr*td dS |dkr:td}g }x~t|D ]r\}}|dkr^|}n,|d}	|d|	 d| ||	d  }tj	|dd	d
}
|

| W dQ R X || qHW |S )a   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.

    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   zNo SVG Plots were found.Nsvgz.svgz_{}wzutf-8)modeencoding)r   lenlogwarningr   	enumeratefindformatioopenwriteappend)r   r   r   r   r   svgs	filenamesir#   idxfr!   r!   r"   r   c   s     

"r   )r   )r   c               C   s   t  S )z Create a new webdriver.

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

    )r   Zcreater!   r!   r!   r"   r      s    r   c          
   K   s   t dd}t }t| f|}tj|jddd}|t| W dQ R X |dk	rV|nt	 }|	d|j  |
  |d t| | }|t}	W dQ R X |t|}
t|
f|	}|S )	a+   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.

    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.

    z	PIL.Imagez[To use bokeh.io.export_png you need pillow ("conda install pillow" or "pip install pillow")r$   zutf-8)r%   r&   Nzfile:///z#document.body.style.width = '100%';)r   	_tmp_htmlr   r-   r.   pathr/   r   r   getZmaximize_windowexecute_scriptwait_until_render_completer   _BOUNDING_RECT_SCRIPTBytesIO_crop_image)r   r   kwargsZImagetmphtmlfile
web_driverr   Zb_rectr    Zcropped_imager!   r!   r"   r      s     
r   c          
   K   s   t  r}t| f|}tj|jdd}|t| W dQ R X |dk	rJ|nt }|d|j  t	| |
t}W dQ R X |S )z

    wb)r%   Nzfile:///)r6   r   r-   r.   r7   r/   r	   r   r8   r:   r9   _SVG_SCRIPT)r   r   r>   r?   r@   rA   rB   r1   r!   r!   r"   r      s    r   c             K   s   d}| ddk	s | ddk	rnddlm} t| |sBtd n,d}| j}| j}| d|| _| d|| _zt| |d	ddd
}W d|r|| _|| _X |S )z

    Fr   Nr   r
   )PlotzgExport method called with height or width kwargs on a non-Plot layout. The size values will be ignored.T )titleZsuppress_callback_warningZ_always_new)	r8   Zmodels.plotsrE   
isinstancewarningswarnZplot_heightZ
plot_widthr   )r   	resourcesr>   ZresizerE   Z
old_heightZ	old_widthr@   r!   r!   r"   r      s     
r   c             C   s   ddl m} ddlm} dd }y|| ddd| W n0 |k
rh } zttd	| W d
d
}~X Y nX | t dd }z<y|| ddd| W n |k
r   t	
d Y nX W d
| d}dd |D }t|dkrt	
d| X d
S )z

    r   )WebDriverWait)TimeoutExceptionc             S   s
   |  dS )Nzl
            const b = window.Bokeh;
            return b && b.documents && b.documents.length > 0;
        )r9   )r   r!   r!   r"   is_bokeh_loaded  s    z3wait_until_render_complete.<locals>.is_bokeh_loaded   g?)Zpoll_frequencyz<Bokeh was not loaded in time. Something may have gone wrong.Nc             S   s
   |  dS )Nz%return window._bokeh_render_complete;)r9   )r   r!   r!   r"   is_bokeh_render_complete  s    z<wait_until_render_complete.<locals>.is_bokeh_render_completezThe webdriver raised a TimeoutException while waiting for                      a 'bokeh:idle' event to signify that the layout has rendered.                      Something may have gone wrong.Zbrowserc             S   s   g | ]}| d dkr|qS )levelZSEVERE)r8   ).0lr!   r!   r"   
<listcomp>'  s    z.wait_until_render_complete.<locals>.<listcomp>zGThere were severe browser errors that may have affected your export: {})Zselenium.webdriver.support.uirL   Zselenium.common.exceptionsrM   Zuntilr   RuntimeErrorr9   _WAIT_SCRIPTr(   r)   Zget_logr'   r,   )r   rL   rM   rN   erP   Zbrowser_logsZsevere_errorsr!   r!   r"   r:     s$     

r:   zZ
return document.getElementsByClassName('bk-root')[0].children[0].getBoundingClientRect()
a  
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
a  
// 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   |  ||||fS )z& Crop the border from the layout

    )Zcrop)r    lefttoprightZbottomr>   r!   r!   r"   r=   L  s    r=   c               @   s:   e Zd ZdZdddZdd Zdd	 Zd
d Zdd ZdS )	_TempFileFr?   rF   c             C   s   t ||d\| _| _d S )N)prefixsuffix)r   fdr7   )selfr\   r]   r!   r!   r"   __init__V  s    z_TempFile.__init__c             C   s   | S )Nr!   )r_   r!   r!   r"   	__enter__Y  s    z_TempFile.__enter__c             C   s   |    d S )N)close)r_   excvaluetbr!   r!   r"   __exit__\  s    z_TempFile.__exit__c             C   s   |    d S )N)rb   )r_   r!   r!   r"   __del___  s    z_TempFile.__del__c          	   C   s   | j r
d S z.yt| j W n ttfk
r4   Y nX W d d | _X z.yt| j W n ttfk
rl   Y nX W d d | _X d| _ d S )NT)_closedosrb   r^   OSErrorIOErrorunlinkr7   )r_   r!   r!   r"   rb   b  s    

z_TempFile.closeN)r?   rF   )	__name__
__module____qualname__rh   r`   ra   rf   rg   rb   r!   r!   r!   r"   r[   R  s   
r[   c               C   s   t dddS )NZbokehz.html)r\   r]   )r[   r!   r!   r!   r"   r6   v  s    r6   )NNNN)NNNN)N)N)r   r   r   r   )0__doc__Z
__future__r   r   r   r   ZloggingZ	getLoggerrm   r(   ri   r-   rI   os.pathr   Ztempfiler   Zsixr   r	   Zembedr   rK   r   Zutil.dependenciesr   Zutil.stringr   utilr   __all__r   r   r   r   r   r   r   r   r   r:   r;   rD   rV   r=   objectr[   r6   r!   r!   r!   r"   <module>	   s<   

(
=

.
)

$