U
    ARd                  	   @   s   U d 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
  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 erdd
lmZ ddlmZ eeZeed< G dd dZde eed ee! e!eddddZ"G dd deZ#dS )z/Streamlit support for Matplotlib PyPlot charts.    N)TYPE_CHECKINGAnyOptionalcast)Final)config)StreamlitDeprecationWarning)
get_logger)	ImageList)gather_metrics)Figure)DeltaGeneratorLOGGERc                   @   sF   e Zd Zedded ee eeddddZedd	d
dZ	dS )PyplotMixinpyplotNTr   r   )figclear_figureuse_container_widthkwargsreturnc                 K   sJ   |st dr| jt  t }t| j ||||f| | jd|S )a  Display a matplotlib.pyplot figure.

        Parameters
        ----------
        fig : Matplotlib Figure
            The figure to plot. When this argument isn't specified, this
            function will render the global figure (but this is deprecated,
            as described below)

        clear_figure : bool
            If True, the figure will be cleared after being rendered.
            If False, the figure will not be cleared after being rendered.
            If left unspecified, we pick a default based on the value of `fig`.

            * If `fig` is set, defaults to `False`.

            * If `fig` is not set, defaults to `True`. This simulates Jupyter's
              approach to matplotlib rendering.

        use_container_width : bool
            If True, set the chart width to the column width. Defaults to `True`.

        **kwargs : any
            Arguments to pass to Matplotlib's savefig function.

        Example
        -------
        >>> import streamlit as st
        >>> import matplotlib.pyplot as plt
        >>> import numpy as np
        >>>
        >>> arr = np.random.normal(1, 1, size=100)
        >>> fig, ax = plt.subplots()
        >>> ax.hist(arr, bins=20)
        >>>
        >>> st.pyplot(fig)

        .. output::
           https://doc-pyplot.streamlitapp.com/
           height: 630px

        Notes
        -----
        .. note::
           Deprecation warning. After December 1st, 2020, we will remove the ability
           to specify no arguments in `st.pyplot()`, as that requires the use of
           Matplotlib's global figure object, which is not thread-safe. So
           please always pass a figure object as shown in the example section
           above.

        Matplotlib supports several types of "backends". If you're getting an
        error using Matplotlib with Streamlit, try setting your backend to "TkAgg"::

            echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc

        For more information, see https://matplotlib.org/faq/usage_faq.html.

        deprecation.showPyplotGlobalUseZimgs)	r   
get_optiondg	exceptionPyplotGlobalUseWarningImageListProtomarshallZ_get_delta_path_strZ_enqueue)selfr   r   r   r   image_list_proto r   =/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/elements/pyplot.pyr   &   s    CzPyplotMixin.pyplotr   c                 C   s
   t d| S )zGet our DeltaGenerator.r   )r   r   r   r   r    r   w   s    zPyplotMixin.dg)NNT)
__name__
__module____qualname__r   r   boolr   r   propertyr   r   r   r   r    r   %   s      Pr   Tr   )coordinatesr   r   r   r   r   r   c              
      s   z dd l }dd lm} |  W n tk
r<   tdY nX |sR|d krNd}|}dddd} fdd	| D } | t }	|j	|	f  |rt
jjnt
jj}
t
j| |	d |
|d
ddd |r|  d S )Nr   z$pyplot() command requires matplotlibTZtight   Zpng)Zbbox_inchesZdpiformatc                    s   i | ]\}}|  ||qS r   )get).0abr   r   r    
<dictcomp>   s      zmarshall.<locals>.<dictcomp>FZRGBZPNG)r(   imagecaptionwidthZ
proto_imgsclampZchannelsZoutput_format)
matplotlibZmatplotlib.pyplotr   ZioffImportErroritemsupdateioBytesIOZsavefigimage_utilsZWidthBehaviourZCOLUMNZORIGINALZmarshall_imagesZclf)r(   r   r   r   r   r   r5   Zpltoptionsr1   Zimage_widthr   r/   r    r   }   s>    

r   c                       s0   e Zd Zdd fddZedddZ  ZS )r   Nr!   c                    s   t t| j|  dd d S )Nr   )msgZconfig_option)superr   __init___get_messager"   	__class__r   r    r?      s    
 zPyplotGlobalUseWarning.__init__c                 C   s   dS )Na  
You are calling `st.pyplot()` without any arguments. After December 1st, 2020,
we will remove the ability to do this as it requires the use of Matplotlib's global
figure object, which is not thread-safe.

To future-proof this code, you should pass in a figure as shown below:

```python
>>> fig, ax = plt.subplots()
>>> ax.scatter([1, 2, 3], [1, 2, 3])
>>>    ... other plotting actions ...
>>> st.pyplot(fig)
```
r   r"   r   r   r    r@      s    z#PyplotGlobalUseWarning._get_message)r#   r$   r%   r?   strr@   __classcell__r   r   rA   r    r      s   r   )NTT)$__doc__r9   typingr   r   r   r   Ztyping_extensionsr   Zstreamlit.elements.imageelementsr1   r;   Z	streamlitr   Zstreamlit.errorsr   Zstreamlit.loggerr	   Zstreamlit.proto.Image_pb2r
   r   Zstreamlit.runtime.metrics_utilr   Zmatplotlib.figurer   Zstreamlit.delta_generatorr   r#   r   __annotations__r   rC   r&   r   r   r   r   r   r    <module>   s6   [   ;