a
    Df<                     @  s   d Z ddlmZ ddlmZmZmZmZm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 erddlmZ ddlmZ ddlmZ G dd deZdS )zP
Defines a PlotlyPane which renders a plotly plot using PlotlyPlot
bokeh model.
    )annotations)TYPE_CHECKINGAnyClassVarMappingOptionalN)ColumnDataSource)JupyterComm   )
isdatetime	lazy_load)
Layoutable   )	ModelPane)Document)Model)Commc                      s
  e Zd ZU dZejddZejddZejdddZejddZ	ej
dd	d
ZejdddZejdddZejdddZejdddZejddg ddZejddddZejddd
ZdZded< dZded< dddddddZd ed!< ed"d#d$d%d&Zd[ fd'd(	Zd)d* Zed+d, Zed\d.d/Z ej!d0d1dd2d3d4 Z"d]d5d6Z#d^d7d8Z$ej!d9dd2d:d; Z%ej!d<dd2d=d> Z&ed?d@ Z'd_dAdBZ(dCdD Z)dEdF Z*edGdH Z+dIdJ Z, fdKdLZ-d`dMdNdNdOdPdQ fdRdSZ.dTdU Z/dVdPdWdXdYdZZ0  Z1S )aPlotlya"  
    The `Plotly` pane renders Plotly plots inside a panel.

    Note that

    - the Panel `extension` has to be loaded with `plotly` as an argument to
    ensure that Plotly.js is initialized.
    - it supports click, hover and selection events.
    - it optimizes the plot rendering by using binary serialization for any
    array data found on the Plotly object.

    Reference: https://panel.holoviz.org/reference/panes/Plotly.html

    :Example:

    >>> pn.extension('plotly')
    >>> Plotly(some_plotly_figure, width=500, height=500)
    z+Click event data from `plotly_click` event.)docz?Clickannotation event data from `plotly_clickannotation` event.Tz_
        Plotly configuration options. See https://plotly.com/javascript/configuration-options/)Znested_refsr   zAHover event data from `plotly_hover` and `plotly_unhover` events.zc
       Attach callbacks to the Plotly figure to update output when it
       is modified in place.)defaultr   z0Relayout event data from `plotly_relayout` eventz.Restyle event data from `plotly_restyle` eventzHSelected event data from `plotly_selected` and `plotly_deselect` events.zCurrent viewport state, i.e. the x- and y-axis limits of the displayed plot.
                          Updated on `plotly_relayout`, `plotly_relayouting` and `plotly_restyle` events.mouseupa  
        Policy by which the viewport parameter is updated during user interactions.

        * "mouseup": updates are synchronized when mouse button is
          released after panning
        * "continuous": updates are synchronized continually while panning
        * "throttle": updates are synchronized while panning, at
          intervals determined by the viewport_update_throttle parameter
        )r   Z
continuousZthrottle)r   r   Zobjects   )r   Nz
        Time interval in milliseconds at which viewport updates are
        synchronized when viewport_update_policy is "throttle".)r   Zboundsr   r   z:
        Number of renders, increment to trigger re-renderg?zClassVar[float | bool | None]priorityzClassVar[bool]_updatesN)link_figureobject
click_dataclickannotation_data
hover_dataselected_dataz"ClassVar[Mapping[str, str | None]]_renamer   zfloat | bool | None)objreturnc                   sH   t |tr$|r$t fdd|D pFt|dpFt |toFd|v oFd|v S )Nc                 3  s   | ]}  |V  qd S N)applies).0ocls ^/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/panel/pane/plotly.py	<genexpr>a       z!Plotly.applies.<locals>.<genexpr>to_plotly_jsondatalayout)
isinstancelistallhasattrdict)r(   r!   r)   r'   r*   r$   _   s    $
zPlotly.appliesc                   s,   t  j|fi | d | _d | _|   d S r#   )super__init___figure_event_update_figure)selfr   params	__class__r)   r*   r6   e   s    zPlotly.__init__c                 C  sz   dd l m} t||jr|S t|tr:|d |d  }}nt|trN|\}}n
|i  }}t|trf|n|g}|j||dS )Nr   r.   r/   r.   r/   )plotly.graph_objs
graph_objsr0   Figurer4   tupler1   )r:   r!   gor.   r/   r)   r)   r*   
_to_figurek   s    



zPlotly._to_figurec                 C  s<   g }|  dg }|D ]"}i }t|| |t| q|S )Nr.   )getr   _get_sources_for_traceappendr   )jsonsourcestracestracer.   r)   r)   r*   _get_sourcesx   s    zPlotly._get_sources c           	      C  s   t |  D ]\}}|s|n
d||}t|tjrF| |g||< qt|trbtj	|||d qt|t r|rt|d trt
|D ](\}}|d t| }tj	|||d qqd S )Nz{}.{})r.   parent_pathr   .)r1   itemsformatr0   npndarraypopr4   r   rF   	enumeratestr)	rH   r.   rN   keyvalue	full_pathielementZelement_pathr)   r)   r*   rF      s    
zPlotly._get_sources_for_tracer   r   )Zwatchc                   s   dd l m}  jd u s8t j|jus8 j ju s8 js<d S  j} fdd|_ fdd|_ fdd|_	 j
|_
 j|_ j|_ fdd|_| _d S )Nr   c                    s
     dS )Nadd_update_from_figure___r:   r)   r*   <lambda>   r,   z'Plotly._update_figure.<locals>.<lambda>c                    s
     dS )Nmover]   r_   rb   r)   r*   rc      r,   c                    s
     dS )Ndeleter]   r_   rb   r)   r*   rc      r,   c                    s
     dS )NZanimater]   r_   rb   r)   r*   rc      r,   )r?   r@   r   typerA   r7   r   Z_send_addTraces_msgZ_send_moveTraces_msgZ_send_deleteTraces_msg_send_restyle_msg_send_relayout_msg_send_update_msgZ_send_animate_msg)r:   rC   figr)   rb   r*   r9      s     
zPlotly._update_figurec                 C  s   |  i |d | d S r#   ri   )r:   relayout_datasource_view_idr)   r)   r*   rh      s    zPlotly._send_relayout_msgc                 C  s   |  |i || d S r#   rk   )r:   restyle_datatrace_indexesrm   r)   r)   r*   rg      s    zPlotly._send_restyle_msgrn   c                 C  s*   | j d u s| jd u rd S | j j| j  d S r#   )r7   rn   Zplotly_restylerb   r)   r)   r*   _update_figure_style   s    zPlotly._update_figure_stylerl   c                 C  sB   | j d u s| jd u rd S | | j}| j jj  | j | d S r#   )r7   rl   _clean_relayout_datar/   Z_compound_array_propsclearZplotly_relayout)r:   rl   r)   r)   r*   _update_figure_layout   s
    zPlotly._update_figure_layoutc                 C  s   dd |   D S )Nc                 S  s    i | ]\}}| d s||qS )z	._derived)endswith)r%   rW   valr)   r)   r*   
<dictcomp>   s   z/Plotly._clean_relayout_data.<locals>.<dictcomp>)rP   )rl   r)   r)   r*   rq      s    zPlotly._clean_relayout_datac           	      C  sb   |rd S | j |}i }|r$||d< |r6||d|d< | j D ]\}\}}| g ||| q@d S )NZrelayout)r.   rJ   Zrestyle)r7   Z_normalize_trace_indexes_modelsrP   Z_apply_update)	r:   rn   rl   ro   rm   msgrefmr`   r)   r)   r*   ri      s    zPlotly._send_update_msgc                 O  s*   || _ z| jd W d | _ nd | _ 0 d S )Nr   )r8   paramtrigger)r:   eventargskwargsr)   r)   r*   r^      s    zPlotly._update_from_figurec           
   	   C  s   i }t || d}| D ]x\}}|d }z<|j|d }t|t|kpd|j|jkpd||k }	W n ty~   d}	Y n0 |	rd}|g|j|< qt	|jD ]}||vr|j|= d}q|S )NFr   T)
r   rF   rP   r.   rE   rf   shapeany	Exceptionr1   )
r:   cdsrK   Ztrace_arraysupdate_sourcesrW   Znew_colnewoldZupdate_arrayr)   r)   r*   _update_data_sources   s,    


zPlotly._update_data_sourcesc                 C  s   |   }|d }tt|D ]`}|| D ]R}t|| | r(|| | }t|tjr`|t}ndd |D }||| |< q(q|S )zvWraps around to_plotly_json and applies necessary fixes.

        For #382: Map datetime elements to strings.
        r.   c                 S  s   g | ]}t |qS r)   )rV   )r%   vr)   r)   r*   
<listcomp>  r,   z/Plotly._plotly_json_wrapper.<locals>.<listcomp>)	r-   rangelenr   r0   rR   rS   ZastyperV   )rj   rH   r.   idxrW   Zarrr)   r)   r*   _plotly_json_wrapper   s    zPlotly._plotly_json_wrapperc                   s   dd  j D }ttj | } fdd|D } jd u rFi g  }}n   j} |}t|} j|d<  j	pxi |d< |
dg |d< ||d< |
d	i  |d	< }|
d
g |d
< |
dr j j jju rd|d< d|vri |d< |S )Nc                 S  s   g | ]}d |v r|qS )viewportr)   r%   pr)   r)   r*   r     r,   z'Plotly._init_params.<locals>.<listcomp>c                   s&   i | ]}t  |d ur|t  |qS r#   )getattrr   rb   r)   r*   rv     s   z'Plotly._init_params.<locals>.<dictcomp>_render_countconfigr.   data_sourcesr/   framesautosizestretch_bothsizing_modestyles)r{   r1   r   r   rD   r   r   rL   r   r   rE   r   r   )r:   Zviewport_params
parametersr;   rH   rI   rj   r/   r)   rb   r*   _init_params  s&    



zPlotly._init_paramsc                   s   t  |}d|v sd|v rd|v r.|d }n$| jrNt| j d d j}n|S |di di di dd}|dg d| d	g |d< |S )
Nr/   Zstylesheetsr   templatefontcolorZblackz?:host { --plotly-icon-color: gray; --plotly-active-icon-color: z; })r5   _process_param_changerw   r1   valuesr/   rE   )r:   r;   propsr/   Z	btn_colorr<   r)   r*   r   $  s    
$


zPlotly._process_param_changer   zOptional[Model]zOptional[Comm]r   )r   rootparentcommr"   c                   sH   t | ds tddt|t|| _t ||||}| jd|||d |S )N_bokeh_modelzpanel.models.plotlyZ
PlotlyPlotZplotly_event)modelr   r   )r3   r   r0   r	   r   r5   
_get_modelZ_register_events)r:   r   r   r   r   r   r<   r)   r*   r   4  s    
zPlotly._get_modelc                 C  sV   |j d }| d}t| ||j d kr6| j| n| jjf i ||j d i d S )Nrf   _datar.   )r.   r   r{   r|   update)r:   r}   etypeZpnamer)   r)   r*   _process_event@  s
    

zPlotly._process_eventrV   None)ry   r   r"   c              	   C  s|  | j d u r*|jg i d | jd7  _d S | | j }| |}|d}|d}|d }g }d}	t|D ]B\}
}|
t|jk r|j|
 }nt	 }|
| | ||p|	}	qlz|j|k}W n ty   d}Y n0 t|jt|krd}nfd}t||jD ]T\}}z(dd	 | D d
d	 | D k}W n tyF   d}Y n0 |r qTqz|j|k}W n tyz   d}Y n0 i }| j| jjju rd|v r|d}t|j}|r|jdkrd|d< d|d< n*|s|jdkrd|d< d|v r|d= |r|j| |d< |r&|d|d< |r4||d< |rH|pBg |d< |r^|jf i | |sj|	rx| jd7  _d S )Nr>   r   r/   r   r.   FTc                 S  s   i | ]\}}|d kr||qS uidr)   r%   kr   r)   r)   r*   rv   m  r,   z"Plotly._update.<locals>.<dictcomp>c                 S  s   i | ]\}}|d kr||qS r   r)   r   r)   r)   r*   rv   n  r,   r   r   r   contentsZdisplayfixedr   )r   r   r   rD   r   rE   rU   r   r   r   rG   r   r/   r   r.   ziprP   r   r   r{   r   r4   r   )r:   ry   r   rj   rH   r/   r   rJ   Znew_sourcesr   rZ   rK   r   Zupdate_layoutZupdate_datar   r   Zupdate_framesZupdatesr   r   r)   r)   r*   _updateH  sz    











zPlotly._update)N)rM   )N)NN)NN)NNN)2__name__
__module____qualname____doc__r{   Dictr   r   r   r   Booleanr   rl   Listrn   r   r   ZSelectorZviewport_update_policyZIntegerZviewport_update_throttler   r   __annotations__r   r    classmethodr$   r6   rD   staticmethodrL   rF   dependsr9   rh   rg   rp   rs   rq   ri   r^   r   r   r   r   r   r   r   __classcell__r)   r)   r<   r*   r      sf   


	






 

  r   )r   
__future__r   typingr   r   r   r   r   numpyrR   r{   Zbokeh.modelsr   Zpyviz_commsr	   utilr   r   Zviewabler   baser   Zbokeh.documentr   Zbokeh.modelr   r   r   r)   r)   r)   r*   <module>   s   