U
    L»{jY  ã                   @   s@   d dl mZ d dlmZ d dlmZ d dlZG dd„ deƒZdS )é    )Úabsolute_import)Ú	BaseScope)ÚPlotlyJSONEncoderNc                       s´   e Zd ZdZeZdZdZdZdZ	d‡ fdd„	Z
e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                    sF   || _ || _|| _|| _d| _d| _d| _d| _tt	| ƒj
f |Ž d S )Nr   i¼  iô  é   )Ú	_plotlyjsÚ_mathjaxÚ	_topojsonÚ_mapbox_access_tokenÚdefault_formatÚdefault_widthÚdefault_heightÚdefault_scaleÚsuperr   Ú__init__)Úselfr   r   r   r   Úkwargs©Ú	__class__© úT/var/www/html/bwave/python/venv/lib/python3.8/site-packages/kaleido/scopes/plotly.pyr      s    zPlotlyScope.__init__c                 C   s   dS )NZplotlyr   ©r   r   r   r    Ú
scope_name"   s    zPlotlyScope.scope_namec                 C   s.  ddl m} t||ƒr| ¡ }|dk	r*|n| j}|dk	r<|n| j}| di ¡}|pb| dd¡pb| j}|px| dd¡px| j}|}| 	¡ }|dkr’d}|| j
kr¼tt| j
ƒƒ}	td	j||	d
�ƒ‚| j|||||d�}
|
 dd¡}|dk�r|
 dd¡}tdj||d�ƒ‚|
 d¡ d¡}|| jk�r*t |¡}|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Ú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})r+   r,   Úresultzutf-8)Zplotly.graph_objectsr#   Ú
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*   r#   r$   r'   r(   Úresponser+   r,   Zimgr   r   r    Ú	transform&   sP    

üÿ    ÿ
 ÿÿ
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   Úvalr   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 r<   )r   r=   r>   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 r<   )r   r=   r>   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 r<   )r   r=   r>   r   r   r    r   ®   s    )NNNN)NNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Z_json_encoderr2   r7   Z_scope_flagsZ_scope_chromium_argsr   Úpropertyr"   r;   r   Úsetterr   r   r   Ú__classcell__r   r   r   r    r      s4   

X






r   )Ú
__future__r   Zkaleido.scopes.baser   Z_plotly_utils.utilsr   r8   r   r   r   r   r    Ú<module>   s   