o
    Mhs                     @   s   d dl mZ d dlZd dlmZmZmZmZ d dl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ZG d	d
 d
eejZdS )    )deepcopyN)ListDictobserveInteger) display_jupyter_version_warnings   )
BaseFigureBasePlotlyType)BoxSelectorLassoSelectorInputDeviceStatePoints)custom_serializersc                       s4  e Zd ZdZeejd d Ze	 j
d<ddieZe j
d<ddieZe	 j
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZe	ddj
d<ddieZedj
ddZ edj
ddZ!dZ"d	Z#		dA fdd	Z$dd Z%dBddZ&dCddZ'dd Z(dd Z)	
dCddZ*dd Z+dd Z,e-ddd Z.e-d d!d" Z/e-d#d$d% Z0e-d&d'd( Z1e-d)d*d+ Z2e-d,d-d. Z3d/d0 Z4dDd1d2Z5d3d4 Z6d5d6 Z7e8d7d8 Z9e9j:d9d8 Z9e;d:d; Z<e;dEd=d>Z=e;dFd?d@Z>  Z?S )GBaseFigureWidgetza
    Base class for FigureWidget. The FigureWidget class is code-generated as a
    subclass
    package_datazwidgetbundle.jssyncT)
allow_noner   )r   FNc                    sp   t t| jd||||d| | jrt  d| _d| _g | _d| _d| _	d| _
t| jj| _t| j| _d S )N)data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_process_view_countr   _layout_obj_props_widget_layout_data_widget_data)selfr   layoutr   r   kwargs	__class__r   R/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/plotly/basewidget.pyr   Z   s$   zBaseFigureWidget.__init__c                 O   s   | S Nr   )r(   argsr*   r   r   r-   show   s   zBaseFigureWidget.showc                 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_datar3   r2   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_datarestyle_tracestrace_edit_idr2   r3   N)_normalize_trace_indexesr   r   r    r!   _py2js_restyle)r(   r8   trace_indexesr3   r2   r:   restyle_msgr   r   r-   _send_restyle_msg   s   




z"BaseFigureWidget._send_restyle_msgc                 C   H   | 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)
trace_datar:   r2   N)r   r   r    r!   _py2js_addTraces)r(   new_traces_datar2   r:   add_traces_msgr   r   r-   _send_addTraces_msg      


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
        )current_trace_indsnew_trace_indsN)_py2js_moveTraces)r(   current_indsnew_inds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_datar5   style_tracesr:   r2   r3   N)r;   r    r!   r   r   _py2js_update)r(   r8   r1   r=   r3   r:   r2   
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)rN   r5   rO   animation_optsr:   r2   r3   )r;   r    r!   r   r   _py2js_animate)r(   styles_datar1   r=   rS   r:   r2   animate_msgr   r   r-   _send_animate_msgP  s"   



z"BaseFigureWidget._send_animate_msgc                 C   r@   )z
        Send Plotly.deleteTraces message to the frontend

        Parameters
        ----------
        delete_inds : list[int]
            List of trace indexes of traces to delete
        r   T)delete_indsr2   r:   N)r    r!   r   r   _py2js_deleteTraces)r(   rX   r:   r2   
delete_msgr   r   r-   _send_deleteTraces_msg  rF   z'BaseFigureWidget._send_deleteTraces_msg_js2py_traceDeltasc                 C   s   |d }|sd| _ dS |d }|d }|| jkrh|D ];}|d }dd | jD }||}| j| }	t|	j|}
| |	j|	j}|rN||d}|| _	d| _	| 
|
|g qd	| _| jsh| jrh| j   | js_d| _ dS )
z@
        Process trace deltas message from the frontend
        newNtrace_deltasr:   uidc                 S   s   g | ]}|j qS r   )r_   ).0tracer   r   r-   
<listcomp>  s    z?BaseFigureWidget._handler_js2py_traceDeltas.<locals>.<listcomp>)remove_traceremove_propsF)r\   r    r   indexr   _transform_data_prop_defaults_remove_overlapping_propsr$   _py2js_removeTraceProps _dispatch_trace_change_callbacksr!   r   r   pop)r(   changer6   r^   r:   delta	trace_uid
trace_uidstrace_index	uid_tracedelta_transformrd   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ret| j|}| | j| j}|r3d|i}|| _d| _|D ]}|d }	| j	|	}
|
rM|	| jvrMi | j|	< q5| 
| d| _| jse| jre| j   | js\d| _ dS )z@
        Process layout delta message from the frontend
        r]   Nlayout_deltar2   rd   r   F)ru   r   r   rf   _layout_defaultsrh   r%   _py2js_removeLayoutPropsr)   _subplot_re_match!_dispatch_layout_change_callbacksr   r!   r   rk   )r(   rl   r6   rv   r2   rr   removed_propsremove_props_msg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
        r]   NrN   rO   r3   )r8   r=   r3   )r   plotly_restyle)r(   rl   r>   rN   rO   r3   r   r   r-   _handler_js2py_restyle#  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
        r]   NrN   rO   r5   r3   )r8   r1   r=   r3   )r   plotly_update)r(   rl   rQ   styler=   r)   r3   r   r   r-   _handler_js2py_update?  s   
z&BaseFigureWidget._handler_js2py_update_js2py_relayoutc                 C   sP   |d }|sd| _ dS |d }|d }d|v r|d | j||d d| _ dS )zC
        Process Plotly.relayout message from the frontend
        r]   Nr1   r3   lastInputTime)r1   r3   )r   rk   plotly_relayout)r(   rl   relayout_msgr1   r3   r   r   r-   _handler_js2py_relayout]  s   

z(BaseFigureWidget._handler_js2py_relayout_js2py_pointsCallbackc                    s  |d }|sd _ dS |d }|ddr?|d }|d }|d }|dkr-tdi |}n|dkr9tdi |}ntd	| d}|d
drS|d
 }tdi |}	nd}	|d }
 fddtt jD }t	|
d |
d |
d |
d D ]\}}}}|| }|d 
| |d 
| |d 
| qv| D ]E\}}tdi |} j| }|dkr|||	 q|dkr|||	 q|dkr|||	 q|dkr||| q|dkr|| qd _ dS )zC
        Process points callback message from the frontend
        r]   N
event_typeselectortypeselector_stateboxlassozUnsupported selector type: %sdevice_statepointsc              	      s&   i | ]}|g g g  j | j|d qS ))
point_indsxsys
trace_namerp   )
_data_objsname)r`   	trace_indr(   r   r-   
<dictcomp>  s    
zBBaseFigureWidget._handler_js2py_pointsCallback.<locals>.<dictcomp>r   r   point_indexesr=   r   plotly_clickplotly_hoverplotly_unhoverplotly_selectedplotly_deselectr   )r   getr   r   
ValueErrorr   rangelenr   zipappenditemsr   r   _dispatch_on_click_dispatch_on_hover_dispatch_on_unhover_dispatch_on_selection_dispatch_on_deselect)r(   rl   callback_datar   selector_dataselector_typer   r   device_state_datastatepoints_datatrace_pointsxy	point_indr   
trace_dicttrace_points_datar   ra   r   r   r-   _handler_js2py_pointsCallbackz  s`   



z.BaseFigureWidget._handler_js2py_pointsCallbackc                 C      t )z/
        Customize html representation
        NotImplementedErrorr   r   r   r-   _repr_html_     zBaseFigureWidget._repr_html_c                 K   s2   t   t| jj| _t| j| _ddd| jdiS )zF
        Return mimebundle corresponding to default renderer.
        z(application/vnd.jupyter.widget-view+json   r   )version_majorversion_minormodel_id)r   r   r#   r$   r%   r&   r'   	_model_id)r(   includeexcludevalidater*   r   r   r-   _repr_mimebundle_  s   z"BaseFigureWidget._repr_mimebundle_c                 C   r   )zD
        Handle rich display of figures in ipython contexts
        r   r   r   r   r-   _ipython_display_  r   z"BaseFigureWidget._ipython_display_c                 C   s&   | j s| jr| j| dS |  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 r.   )r   r   r   r   r-   r     s   zBaseFigureWidget.framesc                 C   s   |rt   d S d S r.   )r   r   )r(   
new_framesr   r   r-   r     s   c                  C   s   d} t | )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 class)r   )msgr   r   r-   r     s   z&BaseFigureWidget._display_frames_errorr   c           
      C   sB  g }t | tr^t |tsJ | D ]I\}}t |ts t|rF|| v rE| | }||f }t|||}|| |sE| | |	| q|| v r[|dkr[| | |	||f  q|S t | t
rt |t
sjJ t|D ]0\}	}|	t| kr{ |S | |	 }|durt |tst|r||	f }t|||}|| qn|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
        r_   N)
isinstancedictr   r	   _is_dict_listr   rh   extendrk   r   list	enumerater   )

input_data
delta_data	prop_pathremovedp	delta_val	input_valrecur_prop_pathrecur_removedir   r   r-   rh   %  sP   








z*BaseFigureWidget._remove_overlapping_propsc              	   C   s  i }t | tr}t |tstdj| |d| D ]J\}}t |ts't|rJ|| vr6t |tr2i ng | |< | | }|tj	|||||f d q|| vsVt
| | |sc|| |< ||f }|||< q|r{t|  t| D ]}	| |	 qs|S t | trt |tstdj| |dt|D ]D\}
}|
t| kr| d | |
 }|durt |tst|r|tj	|||||
f d qt
| |
 |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   r   formatr   r	   r   updater   rf   r
   _vals_equalsetkeys
differencerk   r   r   r   r   )r   r   r   r   r1   	from_propfrom_valr   relayout_path_propremove_propr   r   r   r-   rf   s  s~   



(


z BaseFigureWidget._transform_data)NNNFr.   )NN)NNT)r   )Tr   )@__name__
__module____qualname____doc__pathlibPath__file__parent_esmr   tagr   r%   r   r'   _configrB   r<   r4   rP   rT   rY   rI   rx   ri   r\   ru   r   r   r   r   r   r   r    _set_trace_uid_allow_disable_validationr   r0   r7   r?   rE   rM   rR   rW   r[   r   rt   r   r   r   r   r   r   r   r   r   propertyr   setterstaticmethodr   rh   rf   __classcell__r   r   r+   r-   r      s    
;

 .!
22#
>
>



V



Mr   )copyr   r   	traitletsr   r   r   r   plotly.io._renderersr   basedatatypesr	   r
   	callbacksr   r   r   r   serializersr   	anywidget	AnyWidgetr   r   r   r   r-   <module>   s    