o
    Yha=                     @  s  U d dl mZ d dlmZ d dlmZ d dlmZ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mZ d dl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$ erod dl%Z&d dl'm(Z( e
dde)e*e+df Z,de-d< e
e)e*e+df Z.de-d< e	d Z/de-d< eddG dd dZ0G dd dZ1d3d!d"Z2d4d%d&Z3d5d(d)Z4d6d+d,Z5d7d.d/Z6d8d1d2Z7dS )9    )annotations)	dataclass)dedent)TYPE_CHECKINGAnyLiteralUnioncast)	TypeAlias)OptionSequenceconvert_anything_to_list)HeightLayoutConfigWidthvalidate_heightvalidate_width)maybe_raise_label_warnings)LabelVisibility get_label_visibility_proto_value)StreamlitAPIException)Metric)gather_metrics)
clean_textN)DeltaGeneratorznp.integer[Any]znp.floating[Any]r
   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(   Z/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/elements/metric.pyr!   4   s   
 r!   c                
   @  sD   e Zd Zed		d&dddddddd	d'd"d#Zed(d$d%ZdS ))MetricMixinmetricNr   visibleFstretchcontentline)helplabel_visibilityborderwidthheight
chart_data
chart_typelabelstrvaluer   deltar   delta_colorr   r0   
str | Noner1   r   r2   boolr3   r   r4   r   r5   OptionSequence[Any] | Noner6   Literal['line', 'bar', 'area']returnr   c                C  s.  t || t }t||_t||_t||_||_|dur#t	||_
ttdt||}|j|_|j|_t||j_|
durwg }t|
D ]%}z	|t| W qE tyj } ztd| dt| d|d}~ww t|dkrw|j| t||_t|	dd t|dd t ||	d	}| j!j"d
||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 GitHub-flavored Markdown of the following types: Bold, Italics,
            Strikethroughs, Inline Code, Links, and Images. Images display like
            icons, with a max height equal to the font height.

            Unsupported Markdown 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"``.

            See the ``body`` parameter of |st.markdown|_ for additional,
            supported Markdown directives.

            .. |st.markdown| replace:: ``st.markdown``
            .. _st.markdown: https://docs.streamlit.io/develop/api-reference/text/st.markdown

        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 : "normal", "inverse", or "off"
             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 or None
            A tooltip that gets displayed next to the metric label. Streamlit
            only displays the tooltip when ``label_visibility="visible"``. If
            this is ``None`` (default), no tooltip is displayed.

            The tooltip can optionally contain GitHub-flavored Markdown,
            including the Markdown directives described in the ``body``
            parameter of ``st.markdown``.

        label_visibility : "visible", "hidden", or "collapsed"
            The visibility of the label. The default is ``"visible"``. If this
            is ``"hidden"``, Streamlit displays an empty spacer instead of the
            label, which can help keep the widget aligned with other widgets.
            If this is ``"collapsed"``, Streamlit displays no label or spacer.

        border : bool
            Whether to show a border around the metric container. If this is
            ``False`` (default), no border is shown. If this is ``True``, a
            border is shown.

        height : "content", "stretch", or int
            The height of the metric element. This can be one of the following:

            - ``"content"`` (default): The height of the element matches the
              height of its content.
            - ``"stretch"``: The height of the element matches the height of
              its content or the height of the parent container, whichever is
              larger. If the element is not in a parent container, the height
              of the element matches the height of its content.
            - An integer specifying the height in pixels: The element has a
              fixed height. If the content is larger than the specified
              height, scrolling is enabled.

        width : "stretch", "content", or int
            The width of the metric element. This can be one of the following:

            - ``"stretch"`` (default): The width of the element matches the
              width of the parent container.
            - ``"content"``: The width of the element matches the width of its
              content, but doesn't exceed the width of the parent container.
            - An integer specifying the width in pixels: The element has a
              fixed width. If the specified width is greater than the width of
              the parent container, the width of the element matches the width
              of the parent container.

        chart_data : Iterable or None
            A sequence of numeric values to display as a sparkline chart. If
            this is ``None`` (default), no chart is displayed. The sequence can
            be anything supported by ``st.dataframe``, including a ``list`` or
            ``set``. If the sequence is dataframe-like, the first column will
            be used. Each value will be cast to ``float`` internally by
            default.

        chart_type : "line", "bar", or "area"
            The type of sparkline chart to display. This can be one of the
            following:

            - ``"line"`` (default): A simple sparkline.
            - ``"area"``: A sparkline with area shading.
            - ``"bar"``: A bar chart.

        Examples
        --------
        **Example 1: Show a metric**

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

        .. output::
            https://doc-metric-example1.streamlit.app/
            height: 210px

        **Example 2: Create a row of metrics**

        ``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.streamlit.app/
            height: 210px

        **Example 3: Modify the delta indicator**

        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.streamlit.app/
            height: 320px

        **Example 4: Create a grid of metric cards**

        Add borders to your metrics to create a dashboard look.

        >>> import streamlit as st
        >>>
        >>> a, b = st.columns(2)
        >>> c, d = st.columns(2)
        >>>
        >>> a.metric("Temperature", "30°F", "-9°F", border=True)
        >>> b.metric("Wind", "4 mph", "2 mph", border=True)
        >>>
        >>> c.metric("Humidity", "77%", "5%", border=True)
        >>> d.metric("Pressure", "30.34 inHg", "-2 inHg", border=True)

        .. output::
            https://doc-metric-example4.streamlit.app/
            height: 350px

        **Example 5: Show sparklines**

        To show trends over time, add sparklines.

        >>> import streamlit as st
        >>> from numpy.random import default_rng as rng
        >>>
        >>> changes = list(rng(4).standard_normal(20))
        >>> data = [sum(changes[:i]) for i in range(20)]
        >>> delta = round(data[-1], 2)
        >>>
        >>> row = st.container(horizontal=True)
        >>> with row:
        >>>     st.metric(
        ...         "Line", 10, delta, chart_data=data, chart_type="line", border=True
        ...     )
        >>>     st.metric(
        ...         "Area", 10, delta, chart_data=data, chart_type="area", border=True
        ...     )
        >>>     st.metric(
        ...         "Bar", 10, delta, chart_data=data, chart_type="bar", border=True
        ...     )

        .. output::
            https://doc-metric-example5.streamlit.app/
            height: 300px

        Nr   zFOnly numeric values are supported for chart data sequence. The value '' is of type z" and cannot be converted to float.r   T)allow_content)r3   r4   r+   )layout_config)#r   MetricProto_parse_valuebody_parse_labelr7   _parse_deltar:   show_borderr   r0   $_determine_delta_color_and_directionr	   r   r"   r#   r   r1   r9   r   appendfloat	Exceptionr   typelenr5   extend_parse_chart_typer6   r   r   r   dg_enqueue)selfr7   r9   r:   r;   r0   r1   r2   r3   r4   r5   r6   metric_protocolor_and_directionprepared_datavalexrC   r(   r(   r)   r+   ;   sR    
V




zMetricMixin.metricc                 C  s
   t d| S )Nr   )r	   )rT   r(   r(   r)   rR   9  s   
zMetricMixin.dg)Nr   )r7   r8   r9   r   r:   r   r;   r   r0   r<   r1   r   r2   r=   r3   r   r4   r   r5   r>   r6   r?   r@   r   )r@   r   )r$   r%   r&   r   r+   propertyrR   r(   r(   r(   r)   r*   :   s      ~r*   r6   r?   r@   MetricProto.ChartType.ValueTypec                 C  s(   | dkrt jjS | dkrt jjS t jjS )Nbararea)rD   	ChartTypeBARAREALINE)r6   r(   r(   r)   rQ   >  s
   rQ   r7   r8   c                 C  s(   t | tstd|  dt|  d| S )N'rA   zg, which is not an accepted type. label only accepts: str. Please convert the label to an accepted type.)
isinstancer8   	TypeErrorrN   )r7   r(   r(   r)   rG   I  s
   
rG   r9   c                 C  s   | d u rdS t | tttfrt| S t| dr3zt |  ttfr(t|  W S W n	 ty2   Y nw td|  dt|  d)Nu   —itemrb   rA   z|, which is not an accepted type. value only accepts: int, float, str, or None. Please convert the value to an accepted type.)	rc   intrL   r8   hasattrre   rM   rd   rN   )r9   r(   r(   r)   rE   R  s   
rE   r:   c                 C  sV   | d u s| dkr
dS t | trt| S t | ttfrt| S td|  dt|  d)N rb   rA   z|, which is not an accepted type. delta only accepts: int, float, str, or None. Please convert the value to an accepted type.)rc   r8   r   rf   rL   rd   rN   r:   r(   r(   r)   rH   h  s   
rH   r;   c                 C  s   | dvrt d|  d|d u s|dkrttjjtjjdS t|r=| dkr+tjj}n| dkr4tjj	}ntjj}tjj
}n| dkrFtjj	}n| dkrOtjj}ntjj}tjj}t||dS )N>   r   r   r   rb   zS' is not an accepted value. delta_color only accepts: 'normal', 'inverse', or 'off'rh   )r"   r#   r   r   )r   r!   rD   MetricColorGRAYMetricDirectionNONE_is_negative_deltaREDGREENDOWNUP)r;   r:   cd_colorcd_directionr(   r(   r)   rJ   v  s4   





rJ   r=   c                 C  s   t t| dS )N-)r   r8   
startswithri   r(   r(   r)   rn     s   rn   )r6   r?   r@   r[   )r7   r8   r@   r8   )r9   r   r@   r8   )r:   r   r@   r8   )r;   r   r:   r   r@   r!   )r:   r   r@   r=   )8
__future__r   dataclassesr   textwrapr   typingr   r   r   r   r	   typing_extensionsr
   streamlit.dataframe_utilr   r   #streamlit.elements.lib.layout_utilsr   r   r   r   r   streamlit.elements.lib.policiesr   streamlit.elements.lib.utilsr   r   streamlit.errorsr   streamlit.proto.Metric_pb2r   rD   streamlit.runtime.metrics_utilr   streamlit.string_utilr   numpynpstreamlit.delta_generatorr   rL   rf   r8   r   r'   r   r   r!   r*   rQ   rG   rE   rH   rJ   rn   r(   r(   r(   r)   <module>   s<     


	

'