U
    ARd&                     @   sf  U d dl mZ d dlmZ d dlmZmZmZm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
lmZmZ erd dlZd dlmZ eddeee df Z!ee"d< eeee df Z#ee"d< e
d Z$ee"d< eddG dd dZ%G dd dZ&e e dddZ'e!e dddZ(e#e dd d!Z)e$e#e%d"d#d$Z*e#e+dd%d&Z,dS )'    )	dataclass)dedent)TYPE_CHECKINGOptionalUnioncast)Literal	TypeAlias) get_label_visibility_proto_value)StreamlitAPIException)Metric)gather_metrics)
clean_text)LabelVisibilitymaybe_raise_label_warningsN)DeltaGeneratorz
np.integerznp.floatingValueDelta)normalinverseoff
DeltaColorT)frozenc                   @   s   e Zd ZU ded< ded< dS )MetricColorAndDirectionz!MetricProto.MetricColor.ValueTypecolorz%MetricProto.MetricDirection.ValueType	directionN)__name__
__module____qualname____annotations__ r    r    =/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/elements/metric.pyr   '   s   
r   c                
   @   sF   e Zd Zeddeeeeee e	ddddZ
edd	d
dZdS )MetricMixinmetricNr   visibler   )labelvaluedeltadelta_colorhelplabel_visibilityreturnc           	      C   s~   t || t }t||_t||_t||_|dk	r@t||_	t
ttt||}|j|_|j|_t||j_| jd|S )u  Display a metric in big bold font, with an optional indicator of how the metric changed.

        Tip: If you want to display a large number, it may be a good idea to
        shorten it using packages like `millify <https://github.com/azaitsev/millify>`_
        or `numerize <https://github.com/davidsa03/numerize>`_. E.g. ``1234`` can be
        displayed as ``1.2k`` using ``st.metric("Short number", millify(1234))``.

        Parameters
        ----------
        label : str
            The header or title for the metric. The label can optionally contain
            Markdown and supports the following elements: Bold, Italics,
            Strikethroughs, Inline Code, Emojis, and Links.

            This also supports:

            * Emoji shortcodes, such as ``:+1:``  and ``:sunglasses:``.
              For a list of all supported codes,
              see https://share.streamlit.io/streamlit/emoji-shortcodes.

            * LaTeX expressions, by wrapping them in "$" or "$$" (the "$$"
              must be on their own lines). Supported LaTeX functions are listed
              at https://katex.org/docs/supported.html.

            * Colored text, using the syntax ``:color[text to be colored]``,
              where ``color`` needs to be replaced with any of the following
              supported colors: blue, green, orange, red, violet.

            Unsupported elements are unwrapped so only their children (text contents) render.
            Display unsupported elements as literal characters by
            backslash-escaping them. E.g. ``1\. Not an ordered list``.
        value : int, float, str, or None
             Value of the metric. None is rendered as a long dash.
        delta : int, float, str, or None
            Indicator of how the metric changed, rendered with an arrow below
            the metric. If delta is negative (int/float) or starts with a minus
            sign (str), the arrow points down and the text is red; else the
            arrow points up and the text is green. If None (default), no delta
            indicator is shown.
        delta_color : str
             If "normal" (default), the delta indicator is shown as described
             above. If "inverse", it is red when positive and green when
             negative. This is useful when a negative change is considered
             good, e.g. if cost decreased. If "off", delta is  shown in gray
             regardless of its value.
        help : str
            An optional tooltip that gets displayed next to the metric label.
        label_visibility : "visible" or "hidden" or "collapsed"
            The visibility of the label. If "hidden", the label doesn't show but there
            is still empty space for it (equivalent to label="").
            If "collapsed", both the label and the space are removed. Default is
            "visible". This argument can only be supplied by keyword.

        Example
        -------
        >>> import streamlit as st
        >>>
        >>> st.metric(label="Temperature", value="70 °F", delta="1.2 °F")

        .. output::
            https://doc-metric-example1.streamlitapp.com/
            height: 210px

        ``st.metric`` looks especially nice in combination with ``st.columns``:

        >>> import streamlit as st
        >>>
        >>> col1, col2, col3 = st.columns(3)
        >>> col1.metric("Temperature", "70 °F", "1.2 °F")
        >>> col2.metric("Wind", "9 mph", "-8%")
        >>> col3.metric("Humidity", "86%", "4%")

        .. output::
            https://doc-metric-example2.streamlitapp.com/
            height: 210px

        The delta indicator color can also be inverted or turned off:

        >>> import streamlit as st
        >>>
        >>> st.metric(label="Gas price", value=4, delta=-0.5,
        ...     delta_color="inverse")
        >>>
        >>> st.metric(label="Active developers", value=123, delta=123,
        ...     delta_color="off")

        .. output::
            https://doc-metric-example3.streamlitapp.com/
            height: 320px

        Nr#   )r   MetricProto_parse_valuebody_parse_labelr%   _parse_deltar'   r   r)   $_determine_delta_color_and_directionr   r   r   r   r   r
   r*   r&   dgZ_enqueue)	selfr%   r&   r'   r(   r)   r*   Zmetric_protoZcolor_and_directionr    r    r!   r#   .   s"    e




 zMetricMixin.metric)r+   c                 C   s
   t d| S )Nr   )r   )r3   r    r    r!   r2      s    zMetricMixin.dg)Nr   Nr$   )r   r   r   r   strr   r   r   r   r   r#   propertyr2   r    r    r    r!   r"   -   s        xr"   )r%   r+   c                 C   s0   t | ts,tdt|  dtt|  d| S )N'' is of type zg, which is not an accepted type. label only accepts: str. Please convert the label to an accepted type.)
isinstancer4   	TypeErrortype)r%   r    r    r!   r/      s
    
r/   )r&   r+   c                 C   s   | d krdS t | ts*t | ts*t | tr2t| S t| drz.t |  tsZt |  trht|  W S W n tk
r~   Y nX tdt|  dtt|  dd S )Nu   —itemr6   r7   z|, which is not an accepted type. value only accepts: int, float, str, or None. Please convert the value to an accepted type.)	r8   intfloatr4   hasattrr;   	Exceptionr9   r:   )r&   r    r    r!   r-      s    
r-   )r'   r+   c                 C   sh   | d ks| dkrdS t | tr&t| S t | ts:t | trBt| S tdt|  dtt|  dd S )N r6   r7   z|, which is not an accepted type. delta only accepts: int, float, str, or None. Please convert the value to an accepted type.)r8   r4   r   r<   r=   r9   r:   r'   r    r    r!   r0      s    
r0   )r(   r'   r+   c                 C   s   | dkrt dt|  d|d ks,|dkr@ttjjtjjdS t|r~| dkrZtjj	}n| dkrltjj
}ntjj}tjj}n4| dkrtjj
}n| dkrtjj	}ntjj}tjj}t||dS )N>   r   r   r   r6   zS' is not an accepted value. delta_color only accepts: 'normal', 'inverse', or 'off'r@   )r   r   r   r   )r   r4   r   r,   ZMetricColorZGRAYZMetricDirectionNONE_is_negative_deltaREDGREENDOWNUP)r(   r'   Zcd_colorZcd_directionr    r    r!   r1      s4    




r1   c                 C   s   t t| dS )N-)r   r4   
startswithrA   r    r    r!   rC     s    rC   )-Zdataclassesr   textwrapr   typingr   r   r   r   Ztyping_extensionsr   r	   Zstreamlit.elements.utilsr
   Zstreamlit.errorsr   Zstreamlit.proto.Metric_pb2r   r,   Zstreamlit.runtime.metrics_utilr   Zstreamlit.string_utilr   Zstreamlit.type_utilr   r   ZnumpynpZstreamlit.delta_generatorr   r=   r<   r4   r   r   r   r   r   r"   r/   r-   r0   r1   boolrC   r    r    r    r!   <module>   s6    	'