a
    ~b                     @   s   d dl mZmZmZ d dlZd dlmZ d dlZd dlZ	e
d d dlmZ W d   n1 sd0    Y  d dlmZ ddlmZ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 ZG dd deZdd Zeje dS )    )absolute_importdivisionunicode_literalsN)BytesIOZCRITICAL)
bothmethod   )Renderer
MIME_TYPES	HTML_TAGS   )Store)HoloMap   )	callbacks) clean_internal_figure_propertiesc                 K   sv   t |  | di }|dr&d|d< tjj| fd|d|}t D ](}|jD ]}|j	
||fdd| qRqH|S )	zG
    Custom Plotly pane constructor for use by the HoloViews Pane.
    configZ
responsiveZstretch_bothZsizing_modeZmouseup)Zviewport_update_policyr   c                 S   s   | || j| jjS )N)Z#update_streams_from_property_updatenewobjobject)ZeventclsZprop r   Alib/python3.9/site-packages/holoviews/plotting/plotly/renderer.py<lambda>)   s    z&_PlotlyHoloviewsPane.<locals>.<lambda>)r   popgetpnpaneZPlotlyr   valuesZcallback_propertiesparamZwatch)fig_dictkwargsr   Zplotly_paneZcallback_clsZcallback_propr   r   r   _PlotlyHoloviewsPane   s     

r!   c                       s   e Zd ZejdddZejdg dddZejdg dd	dZg d
g ddZ	ddgZ
dZdZed fdd	ZdddZedd ZedddZ  ZS )PlotlyRendererplotlyzThe backend name.)defaultdocauto)htmlpngsvgr&   zc
        Output render format for static figures. If None, no figure
        rendering will occur. )r$   Zobjectsr%   )scrubberwidgetsgifNr&   zu
        Output render multi-frame (typically animated) format. If
        None, no multi-frame rendering will occur.)r'   r(   r)   )r+   r*   r,   r&   )figholomapr*   r+   FTNc                    s\   t t| j||fi |}|di }t| t| }||d< |di dd |S )z
        Given a HoloViews Viewable return a corresponding figure dictionary.
        Allows cleaning the dictionary of any internal properties that were added
        r   ZlayouttemplateN)	superr"   get_plot_stater   r   goFigureZto_dictr   )Zself_or_clsr   r%   rendererr    r   r   	__class__r   r   r1   J   s    zPlotlyRenderer.get_plot_statec                 K   s  |dkrdd l m} ddlm} ddlm}m}m}	 |	jdk}
|
sJ|  t	|}g }t
|D ]N}|| t }t| |}|j|ddd}|| ||| q^|
s|  t }d	| j d
 }|d j|d|dd  d|dd |d | }nX|dv rJdd l m} t| |}|||}|dkrZ|d}ntdj|d|rt|d}t| t|  }}td j||d}|j||dd}|S |S )Nr,   r   )Image)ensure_servershutdown_serverstatusrunningr(   F)Zvalidateg      ?i  ZGIFr   T)formatZappend_imagesZsave_alldurationZloop)r(   r)   r)   zutf-8zUnsupported format: {fmt})fmtbase64)	mime_typeb64 )srcr@   Zcss)Z	plotly.ioioZPILr7   Zplotly.io.orcar8   r9   r:   statelenrangeupdater   r2   r3   r1   Zto_imagewriteappendopenZfpsZsaveseekreaddecode
ValueErrorr<   r?   Z	b64encoder	   r
   )selfplotr>   Z	as_scriptr    Zpior7   r8   r9   r:   r;   ZnframesZframesiZ	img_bytesZfigureZimgZbior=   datarA   r@   tagrC   Zdivr   r   r   _figure_data_   sN    







zPlotlyRenderer._figure_datac                 C   s   |d }t |tr|jn|}tj| j t|d }||dj	}|d|j
| }|d|j| }t|fi t|t|dS )Ng      Y@rQ   widthheight)rV   rW   )
isinstancer   Zlastr   registrybackendr   typeZlookup_optionsoptionsrV   rW   dictint)r   r   Zpercent_sizeZfactorrQ   r\   rV   rW   r   r   r   plot_options   s    zPlotlyRenderer.plot_optionsc                 C   s4   ddl }d| _dttjddgvr0tjjd dS )z6
        Loads the plotly notebook resources.
        r   NTr#   _loaded_extensions)Zpanel.models.plotly_loadedgetattrr   	extensionr`   rJ   )r   Zinlinepanelr   r   r   load_nb   s    zPlotlyRenderer.load_nb)NN)F)T)__name__
__module____qualname__r   StringrZ   ZObjectSelectorr-   r.   Zmode_formatsr+   ra   Z_render_with_panelr   r1   rU   classmethodr_   re   __classcell__r   r   r5   r   r"   2   s&   
4

r"   c                 C   s   | dkrt tjjjd< d S )Nr#   )r!   r   r   Z	HoloViewsZ_panes)r4   r   r   r   _activate_plotly_backend   s    rl   ) Z
__future__r   r   r   r?   rD   r   r   rd   r   Zlogging_levelZplotly.graph_objsZ
graph_objsr2   Zparam.parameterizedr   r4   r   r	   r
   Zcore.optionsr   corer   r   utilr   r!   r"   rl   Z_backend_switch_hooksrJ   r   r   r   r   <module>   s    *w