U
    Y»{j!ˆ  ã                   @   sØ   d dl Z d dlmZ d dlZd dlZzd dlmZ W n  ek
rT   d dlmZ Y nX d dl	Z
d dlmZmZmZmZmZ ddlmZmZ ddlmZmZmZmZ dd	lmZ dd
lmZ e
jG dd„ dee
jƒƒZdS )é    N)Úimport_module)Úparse)Úurlparse)ÚListÚUnicodeÚDictÚobserveÚIntegeré   )Ú
BaseFigureÚBasePlotlyType)ÚBoxSelectorÚLassoSelectorÚInputDeviceStateÚPoints)Úcustom_serializers)Ú__frontend_version__c                       st  e Zd ZdZedƒjdd�Zedƒjdd�Zeeƒjdd�Z	edƒjdd�Z
edƒjdd�Zeeƒjdd�Zeƒ jf ddie—ŽZeƒ jf ddie—ŽZeƒ jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZedd�jf ddie—ŽZ edd�jf ddie—ŽZ!e"d	ƒjdd�Z#e"d	ƒjdd�Z$dZ%d
Z&d@‡ fdd„	Z'dAdd„Z(dBdd„Z)dd„ Z*dd„ Z+dCdd„Z,dd„ Z-dd„ Z.e/dƒdd„ ƒZ0e/dƒd d!„ ƒZ1e/d"ƒd#d$„ ƒZ2e/d%ƒd&d'„ ƒZ3e/d(ƒd)d*„ ƒZ4e/d+ƒd,d-„ ƒZ5d.d/„ Z6dDd0d1„Z7d2d3„ Z8d4d5„ Z9e:d6d7„ ƒZ;e;j<d8d7„ ƒZ;e=d9d:„ ƒZ>e=dEd<d=„ƒZ?e=dFd>d?„ƒZ@‡  ZAS )GÚBaseFigureWidgetza
    Base class for FigureWidget. The FigureWidget class is code-generated as a
    subclass
    Z
FigureViewT)Úsynczjupyterlab-plotlyZFigureModelr   )Ú
allow_noner   FNc                    sV   t t| ƒjf ||||dœ|—Ž | jr.t ¡  d| _d| _g | _d| _d| _	d| _
d S )N)ÚdataZlayout_plotlyÚframesÚskip_invalidr   F)Úsuperr   Ú__init__Ú_frame_objsÚ_display_frames_errorÚ_last_layout_edit_idÚ_layout_edit_in_processÚ_waiting_edit_callbacksÚ_last_trace_edit_idÚ_trace_edit_in_processZ_view_count)Úselfr   Úlayoutr   r   Úkwargs©Ú	__class__© úP/var/www/html/bwave/python/venv/lib/python3.8/site-packages/plotly/basewidget.pyr   l   s     	üûzBaseFigureWidget.__init__c                 C   s2   | j d }|| _ d| _|||dœ}|| _d| _dS )aŠ  
        Send Plotly.relayout message to the frontend

        Parameters
        ----------
        layout_data : dict
            Plotly.relayout layout data
        source_view_id : str
            UID of view that triggered this relayout operation
            (e.g. By the user clicking 'zoom' in the toolbar). None if the
            operation was not triggered by a frontend view
        r
   T)Úrelayout_dataÚlayout_edit_idÚsource_view_idN)r   r   Ú_py2js_relayout)r"   Úlayout_datar+   r*   Úmsg_datar'   r'   r(   Ú_send_relayout_msg¥   s    
ýz#BaseFigureWidget._send_relayout_msgc                 C   sV   |   |¡}| jd }|| _d| _| jd }|| _d| _|||||dœ}|| _d| _dS )a  
        Send Plotly.restyle message to the frontend

        Parameters
        ----------
        restyle_data : dict
            Plotly.restyle restyle data
        trace_indexes : list[int]
            List of trace indexes that the restyle operation
            applies to
        source_view_id : str
            UID of view that triggered this restyle operation
            (e.g. By the user clicking the legend to hide a trace).
            None if the operation was not triggered by a frontend view
        r
   T)Úrestyle_dataZrestyle_tracesÚtrace_edit_idr*   r+   N)Ú_normalize_trace_indexesr   r   r    r!   Ú_py2js_restyle)r"   r0   Útrace_indexesr+   r*   r1   Úrestyle_msgr'   r'   r(   Ú_send_restyle_msgÅ   s    


û
z"BaseFigureWidget._send_restyle_msgc                 C   sH   | j d }|| _ d| _| jd }|| _d| _|||dœ}|| _d| _dS )zÙ
        Send Plotly.addTraces message to the frontend

        Parameters
        ----------
        new_traces_data : list[dict]
            List of trace data for new traces as accepted by Plotly.addTraces
        r
   T)Z
trace_datar1   r*   N)r   r   r    r!   Ú_py2js_addTraces)r"   Znew_traces_datar*   r1   Zadd_traces_msgr'   r'   r(   Ú_send_addTraces_msgó   s    

ýz$BaseFigureWidget._send_addTraces_msgc                 C   s   ||dœ}|| _ d| _ dS )zõ
        Send Plotly.moveTraces message to the frontend

        Parameters
        ----------
        current_inds : list[int]
            List of current trace indexes
        new_inds : list[int]
            List of new trace indexes
        )Zcurrent_trace_indsZnew_trace_indsN)Ú_py2js_moveTraces)r"   Zcurrent_indsZnew_indsZmove_msgr'   r'   r(   Ú_send_moveTraces_msg  s    
z%BaseFigureWidget._send_moveTraces_msgc                 C   sX   |   |¡}| jd }|| _d| _| jd }|| _d| _||||||dœ}|| _d| _dS )a'  
        Send Plotly.update message to the frontend

        Parameters
        ----------
        restyle_data : dict
            Plotly.update restyle data
        relayout_data : dict
            Plotly.update relayout data
        trace_indexes : list[int]
            List of trace indexes that the update operation applies to
        source_view_id : str
            UID of view that triggered this update operation
            (e.g. By the user clicking a button).
            None if the operation was not triggered by a frontend view
        r
   T)Ú
style_datar-   Ústyle_tracesr1   r*   r+   N)r2   r    r!   r   r   Ú_py2js_update)r"   r0   r)   r4   r+   r1   r*   Ú
update_msgr'   r'   r(   Ú_send_update_msg)  s     


úz!BaseFigureWidget._send_update_msgc                 C   sZ   |   |¡}| jd }|| _d| _| jd }|| _d| _||||||ddœ}|| _d| _dS )aß  
        Send Plotly.update message to the frontend

        Note: there is no source_view_id parameter because animations
        triggered by the fontend are not currently supported

        Parameters
        ----------
        styles_data : list[dict]
            Plotly.animate styles data
        relayout_data : dict
            Plotly.animate relayout data
        trace_indexes : list[int]
            List of trace indexes that the animate operation applies to
        r
   TN)r;   r-   r<   Úanimation_optsr1   r*   r+   )r2   r    r!   r   r   Ú_py2js_animate)r"   Zstyles_datar)   r4   r@   r1   r*   Zanimate_msgr'   r'   r(   Ú_send_animate_msg[  s"    


ùz"BaseFigureWidget._send_animate_msgc                 C   sH   | j d }|| _ d| _| jd }|| _d| _|||dœ}|| _d| _dS )z¿
        Send Plotly.deleteTraces message to the frontend

        Parameters
        ----------
        delete_inds : list[int]
            List of trace indexes of traces to delete
        r
   T)Údelete_indsr*   r1   N)r    r!   r   r   Ú_py2js_deleteTraces)r"   rC   r1   r*   Z
delete_msgr'   r'   r(   Ú_send_deleteTraces_msg�  s    

ýz'BaseFigureWidget._send_deleteTraces_msgÚ_js2py_traceDeltasc                 C   sÖ   |d }|sd| _ dS |d }|d }|| jkrÌ|D ]v}|d }dd„ | jD ƒ}| |¡}| j| }	t |	j|¡}
|  |	j|	j¡}|rœ||dœ}|| _	d| _	|  
|
|g¡ q4d	| _| jsÌ| jrÌ| j ¡ ƒ  q¸d| _ dS )
z@
        Process trace deltas message from the frontend
        ÚnewNÚtrace_deltasr1   Úuidc                 S   s   g | ]
}|j ‘qS r'   )rI   )Ú.0Útracer'   r'   r(   Ú
<listcomp>Ë  s     z?BaseFigureWidget._handler_js2py_traceDeltas.<locals>.<listcomp>)Zremove_traceÚremove_propsF)rF   r    r   Úindexr   Ú_transform_dataZ_prop_defaultsÚ_remove_overlapping_propsZ_propsÚ_py2js_removeTracePropsZ _dispatch_trace_change_callbacksr!   r   r   Úpop)r"   Úchanger.   rH   r1   ÚdeltaZ	trace_uidZ
trace_uidsÚtrace_indexZ	uid_traceÚdelta_transformrM   Zremove_trace_props_msgr'   r'   r(   Ú_handler_js2py_traceDeltas°  s@    


 ÿ ÿþz+BaseFigureWidget._handler_js2py_traceDeltasÚ_js2py_layoutDeltac                 C   sÐ   |d }|sd| _ dS |d }|d }|| jkrÆt | j|¡}|  | j| j¡}|rfd|i}|| _d| _|D ]0}|d }	| j 	|	¡}
|
rj|	| jkrji | j|	< qj|  
|¡ d| _| jsÆ| jrÆ| j ¡ ƒ  q²d| _ dS )z@
        Process layout delta message from the frontend
        rG   NÚlayout_deltar*   rM   r   F)rX   r   r   rO   Z_layout_defaultsrP   Ú_layoutÚ_py2js_removeLayoutPropsr#   Z_subplot_re_matchZ!_dispatch_layout_change_callbacksr   r!   r   rR   )r"   rS   r.   rY   r*   rV   Zremoved_propsZremove_props_msgZproppathÚpropÚmatchr'   r'   r(   Ú_handler_js2py_layoutDeltañ  s<    
 ÿ ÿ
z+BaseFigureWidget._handler_js2py_layoutDeltaÚ_js2py_restylec                 C   sH   |d }|sd| _ dS |d }|d }|d }| j|||d� d| _ dS )zB
        Process Plotly.restyle message from the frontend
        rG   Nr;   r<   r+   )r0   r4   r+   )r_   Zplotly_restyle)r"   rS   r5   r;   r<   r+   r'   r'   r(   Ú_handler_js2py_restyle1  s    ýz'BaseFigureWidget._handler_js2py_restyleÚ_js2py_updatec                 C   sR   |d }|sd| _ dS |d }|d }|d }|d }| j||||d� d| _ dS )zA
        Process Plotly.update message from the frontend
        rG   Nr;   r<   r-   r+   )r0   r)   r4   r+   )ra   Zplotly_update)r"   rS   r>   Ústyler4   r#   r+   r'   r'   r(   Ú_handler_js2py_updateM  s    üz&BaseFigureWidget._handler_js2py_updateÚ_js2py_relayoutc                 C   sP   |d }|sd| _ dS |d }|d }d|kr8| d¡ | j||d� d| _ dS )zC
        Process Plotly.relayout message from the frontend
        rG   Nr)   r+   ZlastInputTime)r)   r+   )rd   rR   Zplotly_relayout)r"   rS   Zrelayout_msgr)   r+   r'   r'   r(   Ú_handler_js2py_relayoutk  s    
z(BaseFigureWidget._handler_js2py_relayoutÚ_js2py_pointsCallbackc                    sÈ  |d }|sdˆ _ dS |d }| dd¡rx|d }|d }|d }|dkrVtf |Ž}q||dkrjtf |Ž}q|td	| ƒ‚nd}| d
d¡rœ|d
 }tf |Ž}	nd}	|d }
‡ fdd„ttˆ jƒƒD ƒ}t	|
d |
d |
d |
d ƒD ]>\}}}}|| }|d  
|¡ |d  
|¡ |d  
|¡ qâ| ¡ D ]’\}}tf |Ž}ˆ j| }|dk�r^| ||	¡ n\|dk�rv| ||	¡ nD|dk�rŽ| ||	¡ n,|dk�r¦| ||¡ n|dk�r*| |¡ �q*dˆ _ dS )zC
        Process points callback message from the frontend
        rG   NÚ
event_typeÚselectorÚtypeÚselector_stateÚboxÚlassozUnsupported selector type: %sZdevice_stateÚpointsc              	      s&   i | ]}|g g g ˆ j | j|d œ“qS ))Ú
point_indsÚxsÚysZ
trace_namerU   )Ú
_data_objsÚname)rJ   Ú	trace_ind©r"   r'   r(   Ú
<dictcomp>´  s   ù
ûzBBaseFigureWidget._handler_js2py_pointsCallback.<locals>.<dictcomp>ro   rp   Zpoint_indexesr4   rn   Zplotly_clickZplotly_hoverZplotly_unhoverZplotly_selectedZplotly_deselect)rf   Úgetr   r   Ú
ValueErrorr   ÚrangeÚlenrq   ÚzipÚappendÚitemsr   r   Z_dispatch_on_clickZ_dispatch_on_hoverZ_dispatch_on_unhoverZ_dispatch_on_selectionZ_dispatch_on_deselect)r"   rS   Zcallback_datarg   Zselector_dataZselector_typerj   rh   Zdevice_state_dataÚstateZpoints_dataZtrace_pointsÚxÚyZ	point_indrs   Z
trace_dictZtrace_points_datarm   rK   r'   rt   r(   Ú_handler_js2py_pointsCallbackˆ  s^    
øü






z.BaseFigureWidget._handler_js2py_pointsCallbackc                 C   s   t ‚dS )z/
        Customize html representation
        N©ÚNotImplementedErrorrt   r'   r'   r(   Ú_repr_html_à  s    zBaseFigureWidget._repr_html_c                 K   s   ddd| j dœiS )zF
        Return mimebundle corresponding to default renderer.
        z(application/vnd.jupyter.widget-view+jsoné   r   )Zversion_majorZversion_minorZmodel_id)Z	_model_id)r"   ÚincludeÚexcludeÚvalidater$   r'   r'   r(   Ú_repr_mimebundle_æ  s    ýÿz"BaseFigureWidget._repr_mimebundle_c                 C   s   t ‚dS )zD
        Handle rich display of figures in ipython contexts
        Nr�   rt   r'   r'   r(   Ú_ipython_display_ò  s    z"BaseFigureWidget._ipython_display_c                 C   s$   | j s| jr| j |¡ n|ƒ  dS )a†  
        Register a function to be called after all pending trace and layout
        edit operations have completed

        If there are no pending edit operations then function is called
        immediately

        Parameters
        ----------
        fn : callable
            Function of zero arguments to be called when all pending edit
            operations have completed
        N)r   r!   r   r{   )r"   Úfnr'   r'   r(   Úon_edits_completedú  s    z#BaseFigureWidget.on_edits_completedc                 C   s   | j S ©N)r   rt   r'   r'   r(   r     s    zBaseFigureWidget.framesc                 C   s   |rt  ¡  d S rŒ   )r   r   )r"   Z
new_framesr'   r'   r(   r     s    c                  C   s   d} t | ƒ‚dS )z®
        Display an informative error when user attempts to set frames on a
        FigureWidget

        Raises
        ------
        ValueError
            always
        z‡
Frames are not supported by the plotly.graph_objs.FigureWidget class.
Note: Frames are supported by the plotly.graph_objs.Figure classN)rw   )Úmsgr'   r'   r(   r     s    z&BaseFigureWidget._display_frames_errorr'   c           
      C   sF  g }t | tƒrºt |tƒst‚| ¡ D ]’\}}t |tƒs@t |¡rŒ|| kr¶| | }||f }t |||¡}| |¡ |s¶|  	|¡ | 
|¡ q$|| kr$|dkr$|  	|¡ | 
||f ¡ q$nˆt | tƒ�rBt |tƒsÔt‚t|ƒD ]d\}	}|	t| ƒkrö �qB| |	 }|dk	�rt |tƒ�st |¡rÜ||	f }t |||¡}| |¡ qÜ|S )a§  
        Remove properties in input_data that are also in delta_data, and do so
        recursively.

        Exception: Never remove 'uid' from input_data, this property is used
        to align traces

        Parameters
        ----------
        input_data : dict|list
        delta_data : dict|list

        Returns
        -------
        list[tuple[str|int]]
            List of removed property path tuples
        rI   N)Ú
isinstanceÚdictÚAssertionErrorr|   r   Ú_is_dict_listr   rP   ÚextendrR   r{   ÚlistÚ	enumeratery   )
Z
input_dataZ
delta_dataZ	prop_pathZremovedÚpZ	delta_valÚ	input_valZrecur_prop_pathZrecur_removedÚir'   r'   r(   rP   -  sP    

  ÿ


ÿþý
  ÿz*BaseFigureWidget._remove_overlapping_propsc              	   C   sÄ  i }t | tƒrøt |tƒs*tdj| |d�ƒ‚| ¡ D ]”\}}t |tƒsNt |¡r”|| krlt |tƒrdi ng | |< | | }| tj	|||||f d�¡ q2|| ks¬t
 | | |¡s2|| |< ||f }|||< q2|röt|  ¡ ƒ t| ¡ ƒ¡D ]}	|  |	¡ qænÈt | tƒ�rÀt |tƒ�s"tdj| |d�ƒ‚t|ƒD ]”\}
}|
t| ƒk�rJ|  d¡ | |
 }|dk	�r”t |tƒ�stt |¡�r”| tj	|||||
f d�¡ n(t
 | |
 |¡�s*|| |
< ||||
f < �q*|S )aB  
        Transform to_data into from_data and return relayout-style
        description of the transformation

        Parameters
        ----------
        to_data : dict|list
        from_data : dict|list

        Returns
        -------
        dict
            relayout-style description of the transformation
        z,Mismatched data types: {to_dict} {from_data})Úto_dictÚ	from_data)Úshould_removeÚrelayout_pathz5Mismatched data types: to_data: {to_data} {from_data})Úto_datar™   N)rŽ   r�   rw   Úformatr|   r   r‘   Úupdater   rO   r   Z_vals_equalÚsetÚkeysÚ
differencerR   r“   r”   ry   r{   )rœ   r™   rš   r›   r)   Z	from_propZfrom_valr–   Zrelayout_path_propZremove_propr—   r'   r'   r(   rO   |  s~    

 ÿÿüÿ ÿ


ÿ ÿÿ

ÿÿüÿz BaseFigureWidget._transform_data)NNNF)N)NN)NN)NNT)r'   )Tr'   )BÚ__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚtagZ
_view_nameZ_view_moduler   Z_view_module_versionZ_model_nameZ_model_moduleZ_model_module_versionr   r   rZ   r   Ú_dataÚ_configr7   r3   r,   r=   rA   rD   r9   r[   rQ   rF   rX   r_   rd   ra   rf   r	   r   r    Z_set_trace_uidZ_allow_disable_validationr   r/   r6   r8   r:   r?   rB   rE   r   rW   r^   r`   rc   re   r€   rƒ   rˆ   r‰   r‹   Úpropertyr   ÚsetterÚstaticmethodr   rP   rO   Ú__classcell__r'   r'   r%   r(   r      s’   	
ÿÿ       ÿ9
 
.!   ÿ
22#
@
?



W



Nr   ) ÚuuidÚ	importlibr   ÚosÚnumbersÚurllibr   ÚImportErrorr   Ú
ipywidgetsZwidgetsZ	traitletsr   r   r   r   r	   Zbasedatatypesr   r   Ú	callbacksr   r   r   r   Zserializersr   Úversionr   ÚregisterZ	DOMWidgetr   r'   r'   r'   r(   Ú<module>   s   