U
    cL                     @   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                       sb  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'd=ddZ(d>ddZ)dd Z*dd Z+d?d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/ Z6d0d1 Z7e8d2d3 Z9e9j:d4d3 Z9e;d5d6 Z<e;d@d8d9Z=e;dAd:d;Z>  Z?S )B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__ 5/tmp/pip-unpacked-wheel-fki0jwev/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   qd| _ 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rt| j|}| | j| j}|rfd|i}|| _d| _|D ]0}|d }	| j	|	}
|
rj|	| jkrji | j|	< qj| 
| d| _| js| jr| j   qd| _ 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_stateZboxZ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>rm   rn   Zpoint_indexesr4   rl   Zplotly_clickZplotly_hoverZplotly_unhoverZplotly_selectedZplotly_deselect)rf   getr   r   
ValueErrorr   rangelenro   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_indrq   Z
trace_dictZtrace_points_datark   rK   r'   rr   r(   _handler_js2py_pointsCallback  s^    







z.BaseFigureWidget._handler_js2py_pointsCallbackc                 C   s   t j|  dS )zD
        Handle rich display of figures in ipython contexts
        N)widgets	DOMWidget_ipython_display_rr   r'   r'   r(   r     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   ry   )r"   fnr'   r'   r(   on_edits_completed  s    z#BaseFigureWidget.on_edits_completedc                 C   s   | j S N)r   rr   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)ru   )msgr'   r'   r(   r   
  s    z&BaseFigureWidget._display_frames_errorr'   c           
      C   sF  g }t | trt |tst| D ]\}}t |ts@t|r|| kr| | }||f }t|||}|| |s| 	| |
| q$|| kr$|dkr$| 	| |
||f  q$nt | trBt |tst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AssertionErrorrz   r   _is_dict_listr   rP   extendrR   ry   list	enumeraterw   )
Z
input_dataZ
delta_dataZ	prop_path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rt |ts*tdj| |d| D ]\}}t |tsNt|r|| krlt |trdi ng | |< | | }|tj	|||||f d q2|| kst
| | |s2|| |< ||f }|||< q2|rt|  t| D ]}	| |	 qnt | trt |ts"tdj| |dt|D ]\}
}|
t| krJ| d | |
 }|dk	rt |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})Zto_dict	from_data)should_removerelayout_pathz5Mismatched data types: to_data: {to_data} {from_data})to_datar   N)r   r   ru   formatrz   r   r   updater   rO   r   Z_vals_equalsetkeys
differencerR   r   r   rw   ry   )r   r   r   r   r)   Z	from_propZfrom_valr   Zrelayout_path_propZremove_propr   r'   r'   r(   rO   k  s~    

  


 

z BaseFigureWidget._transform_data)NNNF)N)NN)NN)r'   )Tr'   )@__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   propertyr   setterstaticmethodr   rP   rO   __classcell__r'   r'   r%   r(   r      s   	
       9
 
.!   
22#
@
?



W	


Nr   ) uuid	importlibr   osZnumbersurllibr   ImportErrorr   Z
ipywidgetsr   Z	traitletsr   r   r   r   r	   Zbasedatatypesr   r   	callbacksr   r   r   r   Zserializersr   versionr   registerr   r   r'   r'   r'   r(   <module>   s   