B
    U[                 @   s   d Z ddlmZ ddlZddl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
lmZ dddZde_g e_dd Zdd ZeZdd Ze  dd Zdd Zdd ZdS )z<A matplotlib backend for publishing figures via display_data    )print_functionN)new_figure_managerFigureCanvasAgg)colors)Gcf)get_ipython)display   )InlineBackendc          	   C   sf   | dkrt  j} z.x(t D ]}t|jjt|jjd qW W dg t	_
| r`t r`tjd X dS )a  Show all figures as SVG/PNG payloads sent to the IPython clients.

    Parameters
    ----------
    close : bool, optional
      If true, a ``plt.close('all')`` call is automatically issued after
      sending all the figures. If this is set, the figures will entirely
      removed from the internal list of figures.
    block : Not used.
      The `block` parameter is a Matplotlib experimental parameter.
      We accept it in the function signature for compatibility with other
      backends.
    N)metadataall)r
   instanceclose_figuresr   get_all_fig_managersr   canvasfigure_fetch_figure_metadatashow_to_draw
matplotlibZpyplotclose)r   blockZfigure_manager r   =lib/python3.7/site-packages/ipykernel/pylab/backend_inline.pyr      s    
r   Fc                 s|   t  } | dkrdS | jj t ds4 fdd _t s@dS ytj	  W n t
k
rd   Y nX tj  dt_dS )z5
    Is called after every pylab drawing command
    Nr   c                 s   t  t dS )N)r   )r   r   )a)figr   r   <lambda>O   s    z%draw_if_interactive.<locals>.<lambda>T)r   Z
get_activer   r   hasattrr   r   Zis_interactiver   remove
ValueErrorappend_draw_called)Zmanagerr   )r   r   draw_if_interactive7   s    
r"   c                 s  t js
dS t jr`yt dS  tk
r^ }  z$t }|dkrB| n|  dS W dd} ~ X Y nX ztdd t	
 D  xt fddt jD D ]\}yt|t|d W q tk
r }  z$t }|dkr| n|  dS W dd} ~ X Y qX qW W dg t _dt _X dS )a  Send all figures that changed

    This is meant to be called automatically and will call show() if, during
    prior code execution, there had been any calls to draw_if_interactive.

    This function is meant to be used as a post_execute callback in IPython,
    so user-caused errors are handled with showtraceback() instead of being
    allowed to raise.  If this function is not called from within IPython,
    then these exceptions will raise.
    NTc             S   s   g | ]}|j jqS r   )r   r   ).0Zfmr   r   r   
<listcomp>   s    z!flush_figures.<locals>.<listcomp>c                s   g | ]}| kr|qS r   r   )r#   r   )activer   r   r$      s    )r   F)r   r!   r
   r   r   	Exceptionr   Zshowtracebacksetr   r   r   r   r   )eipr   r   )r%   r   flush_figuresd   s0    
r*   c           	      s   ddl m}  t |  rdt krddlmm  y   W n: ttfk
r    fddj	
d Y nX dS )	zYEnable extra IPython matplotlib integration when we are loaded as the matplotlib backend.r   )get_backendzmodule://%s)configure_inline_supportactivate_matplotlibc                 s$      j d d S )Npost_run_cell)eventsZ
unregister)args)r-   backendr,   configure_oncer)   r   r   r2      s    
z6_enable_matplotlib_integration.<locals>.configure_oncer.   N)r   r+   r   __name__ZIPython.core.pylabtoolsr,   r-   ImportErrorAttributeErrorr/   register)r+   r   )r-   r1   r,   r2   r)   r   _enable_matplotlib_integration   s    r7   c             C   sN   t |  rJtdd | jD }|jrJ||d k rJd|d rDdndiS dS )z3Get some metadata to help with displaying a figure.c             S   s4   g | ],}|j |jfD ]}| D ]}| q qqS r   )ZxaxisZyaxisZget_ticklabelsZ	get_color)r#   axesZaxisZlabelr   r   r   r$      s   z*_fetch_figure_metadata.<locals>.<listcomp>r   Zneeds_backgroundZdarkZlightN)_is_transparentZget_facecolor	_is_lightr8   sizer   )r   Z
ticksLightr   r   r   r      s    r   c             C   s(   t | }|ddddf ddkS )zDetermines if a color (or each of a sequence of colors) is light (as
    opposed to dark). Based on ITU BT.601 luminance formula (see
    https://stackoverflow.com/a/596241).N   )gA`"?gbX9?gv/?g      ?)r   Zto_rgba_arraydot)colorZrgbaArrr   r   r   r:      s    
r:   c             C   s   t | }|d dk S )z"Determine transparency from alpha.r<   g      ?)r   Zto_rgba)r>   Zrgbar   r   r   r9      s    
r9   )NN)__doc__Z
__future__r   r   Zmatplotlib.backends.backend_aggr   r   r   Zmatplotlib._pylab_helpersr   ZIPython.core.getipythonr   ZIPython.core.displayr   Zconfigr
   r   r!   r   r"   r*   ZFigureCanvasr7   r   r:   r9   r   r   r   r   <module>   s&   
-1