B
    HJd                 @   sT   d dl mZ d dlmZmZ d dlZd dlZd dlmZ d dl	Z	G dd deZ
dS )    )absolute_import)	BaseScopewhichN)Pathc                   s   e Zd ZdZdZdZdZdZd fdd	Zdd	d
Z	e
dd Zdd ZdddZe
dd Zejdd Ze
dd Zejdd Ze
dd Zejdd Ze
dd Zejdd Z  ZS ) PlotlyScopez@
    Scope for transforming Plotly figures to static images
    )pngjpgjpegZwebpsvgZpdfepsjson)r
   r   r   )plotlyjsmathjaxtopojsonmapbox_access_token)z--no-sandboxNc                sb   || _ || _|| _y| | W n   d | _Y nX d| _d| _d| _d| _t	t
| jf | d S )Nr   i  i     )	_plotlyjs	_topojson_mapbox_access_token_initialize_mathax_mathjaxdefault_formatdefault_widthdefault_heightdefault_scalesuperr   __init__)selfr   r   r   r   kwargs)	__class__ G/var/www/html/venv/lib/python3.7/site-packages/kaleido/scopes/plotly.pyr      s    zPlotlyScope.__init__c          	   C   s   |d k	r|| _ d S tjtjtjtjtdddd}d }tj|rT|}n$td}|rxt	
|}|d }|rt|  }|| _ nd | _ d S )N
executableetcr   z
MathJax.jszmathjax-pathutf8)r   ospathjoindirnameabspath__file__existsr   
subprocesscheck_outputdecodestripr   absoluteas_uri)r   r   Zvendored_mathjax_pathZmathjax_pathZmathjax_path_executableZ
path_bytesZmathjax_urir    r    r!   r   '   s(    
zPlotlyScope._initialize_mathaxc             C   s   dS )NZplotlyr    )r   r    r    r!   
scope_nameF   s    zPlotlyScope.scope_namec             C   s   dd l m} |j|dddS )Nr   F)validateZremove_uids)Z	plotly.ioioto_json)r   valZpior    r    r!   _json_dumpsJ   s    zPlotlyScope._json_dumpsc             C   sf  ddl m} t||r| }|dk	r*|n| j}|dk	r<|n| j}|di }|p~|ddp~|di di ddp~| j}|p|ddp|di di ddp| j}|}|	 }|dkrd	}|| j
krtt| j
}	td
j||	d| j|||||d}
|
dd}|dkr<|
dd}tdj||d|
dd}|| jkrbt|}|S )a  
        Convert a Plotly figure into a static image

        :param figure: Plotly figure or figure dictionary
        :param format: The desired image format. One of
           'png', 'jpg', 'jpeg', 'webp', 'svg', 'pdf', or 'json'.

           If 'json', the following arguments are ignored and a full
           JSON representation of the figure is returned.

           If not specified, will default to the `scope.default_format` property
        :param width: The width of the exported image in layout pixels.
            If the `scale` property is 1.0, this will also be the width
            of the exported image in physical pixels.

            If not specified, will default to the `scope.default_width` property
        :param height: The height of the exported image in layout pixels.
            If the `scale` property is 1.0, this will also be the height
            of the exported image in physical pixels.

            If not specified, will default to the `scope.default_height` property
        :param scale: The scale factor to use when exporting the figure.
            A scale factor larger than 1.0 will increase the image resolution
            with respect to the figure's layout pixel dimensions. Whereas as
            scale factor of less than 1.0 will decrease the image resolution.

            If not specified, will default to the `scope.default_scale` property
        :return: image bytes
        r   )FigureNlayoutwidthtemplateheightr   r	   zRInvalid format '{original_format}'.
    Supported formats: {supported_formats_str})original_formatsupported_formats_str)formatr:   r<   scalecodemessagez2Transform failed with error code {code}: {message})rA   rB   resultzutf-8)Zplotly.graph_objectsr8   
isinstanceto_dictr   r   getr   r   lower_all_formatsreprlist
ValueErrorr?   Z_perform_transformencode_text_formatsbase64	b64decode)r   Zfigurer?   r:   r<   r@   r8   r9   r=   r>   responserA   rB   Zimgr    r    r!   	transformN   sH    




zPlotlyScope.transformc             C   s   | j S )z
        URL or local file path to plotly.js bundle to use for image export.
        If not specified, will default to CDN location.
        )r   )r   r    r    r!   r      s    zPlotlyScope.plotlyjsc             C   s   || _ |   d S )N)r   _shutdown_kaleido)r   r6   r    r    r!   r      s    c             C   s   | j S )z
        URL to MathJax bundle needed for LaTeX rendering.
        If not specified, LaTeX rendering support will be disabled.
        )r   )r   r    r    r!   r      s    zPlotlyScope.mathjaxc             C   s   || _ |   d S )N)r   rR   )r   r6   r    r    r!   r      s    c             C   s   | j S )z
        URL to the topojson files needed to render choropleth traces.
        If not specified, will default to CDN location.
        )r   )r   r    r    r!   r      s    zPlotlyScope.topojsonc             C   s   || _ |   d S )N)r   rR   )r   r6   r    r    r!   r      s    c             C   s   | j S )z
        Mapbox access token required to render mapbox layers.
        If not specified, mapbox layers will only be rendered
        if a valid token is specified inline in the figure specification
        )r   )r   r    r    r!   r      s    zPlotlyScope.mapbox_access_tokenc             C   s   || _ |   d S )N)r   rR   )r   r6   r    r    r!   r      s    )NNNN)N)NNNN)__name__
__module____qualname____doc__rH   rM   Z_scope_flagsZ_scope_chromium_argsr   r   propertyr2   r7   rQ   r   setterr   r   r   __classcell__r    r    )r   r!   r      s$   

b	r   )
__future__r   Zkaleido.scopes.baser   r   rN   r%   pathlibr   r,   r   r    r    r    r!   <module>   s   