U
    ARd6                  
   @   s  d dl mZ d dlmZmZmZmZmZmZm	Z	m
Z
mZ d dl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 erd d
lmZ e
eeejeeeeee f df ZG dd dZ d*eeee ddddZ!eeeddddZ"eeeddddZ#eeddddZ$eeeeef ddddZ%e	deeef dZ&ee& ee& dddZ'd+eeeef eeed!d"d#Z(eeeeef dd$d%d&Z)eeeef ed'd(d)Z*dS ),    )Iterable)	TYPE_CHECKINGAnyDictListMappingOptionalTypeVarUnioncastN)ndarray)	DataFrame)Styler)	type_util)Arrow)gather_metrics)DeltaGeneratorc                	   @   sf   e Zd Zeddddeee ee eddddZed	dedd
ddZ	e
ddddZdS )
ArrowMixin_arrow_dataframeNF)use_container_widthr   )datawidthheightr   returnc                C   sZ   | j  }tt|}t }||_|r,||_|r6||_tjj	|_
t||| | j d|S )ag  Display a dataframe as an interactive table.

        Parameters
        ----------
        data : pandas.DataFrame, pandas.Styler, pyarrow.Table, numpy.ndarray, pyspark.sql.DataFrame, snowflake.snowpark.DataFrame, Iterable, dict, or None
            The data to display.

            If 'data' is a pandas.Styler, it will be used to style its
            underlying DataFrame.

        width : int or None
            Desired width of the dataframe element expressed in pixels. If None, the
            width will be automatically determined.

        height : int or None
            Desired height of the dataframe element expressed in pixels. If None, the
            height will be automatically determined.

        use_container_width : bool
            If True, set the dataframe width to the width of the parent container.
            This takes precedence over the width argument.
            This argument can only be supplied by keyword.

        Examples
        --------
        >>> import streamlit as st
        >>> import pandas as pd
        >>> import numpy as np
        >>>
        >>> df = pd.DataFrame(
        ...    np.random.randn(50, 20),
        ...    columns=('col %d' % i for i in range(20)))
        ...
        >>> st._arrow_dataframe(df)

        >>> st._arrow_dataframe(df, 200, 100)

        You can also pass a Pandas Styler object to change the style of
        the rendered DataFrame:

        >>> df = pd.DataFrame(
        ...    np.random.randn(10, 20),
        ...    columns=('col %d' % i for i in range(20)))
        ...
        >>> st._arrow_dataframe(df.style.highlight_max(axis=0))

        Zarrow_data_frame)dg_get_delta_path_strstrhash
ArrowProtor   r   r   ZEditingModeZ	READ_ONLYZediting_modemarshall_enqueue)selfr   r   r   r   
delta_pathdefault_uuidproto r%   </tmp/pip-unpacked-wheel-b9et7o5g/streamlit/elements/arrow.pyr   ,   s    <

zArrowMixin._arrow_dataframe_arrow_table)r   r   c                 C   s\   t |st |t jr&t j|dd}| j }tt|}t	 }t
||| | jd|S )a  Display a static table.

        This differs from `st._arrow_dataframe` in that the table in this case is
        static: its entire contents are laid out directly on the page.

        Parameters
        ----------
        data : pandas.DataFrame, pandas.Styler, pyarrow.Table, numpy.ndarray, pyspark.sql.DataFrame, snowflake.snowpark.DataFrame, Iterable, dict, or None
            The table data.

        Example
        -------
        >>> import streamlit as st
        >>> import pandas as pd
        >>> import numpy as np
        >>>
        >>> df = pd.DataFrame(
        ...    np.random.randn(10, 5),
        ...    columns=("col %d" % i for i in range(5)))
        ...
        >>> st._arrow_table(df)

        d   )Zmax_unevaluated_rowsZarrow_table)r   Zis_snowpark_data_objectZis_typeZ_PYSPARK_DF_TYPE_STRconvert_anything_to_dfr   r   r   r   r   r   r    )r!   r   r"   r#   r$   r%   r%   r&   r'   w   s     
zArrowMixin._arrow_table)r   c                 C   s
   t d| S )zGet our DeltaGenerator.r   )r   )r!   r%   r%   r&   r      s    zArrowMixin.dg)NNN)N)__name__
__module____qualname__r   Datar   intboolr   r'   propertyr   r%   r%   r%   r&   r   +   s"      J*r   )r$   r   r#   r   c                 C   s\   t |r(t|tstdt| || t|tjrBt || _	nt 
|}t || _	dS )a[  Marshall pandas.DataFrame into an Arrow proto.

    Parameters
    ----------
    proto : proto.Arrow
        Output. The protobuf for Streamlit Arrow proto.

    data : pandas.DataFrame, pandas.Styler, pyarrow.Table, numpy.ndarray, pyspark.sql.DataFrame, snowflake.snowpark.DataFrame, Iterable, dict, or None
        Something that is or can be converted to a dataframe.

    default_uuid : Optional[str]
        If pandas.Styler UUID is not provided, this value will be used.
        This attribute is optional and only used for pandas.Styler, other elements
        (e.g. charts) can ignore it.

    z.Default UUID must be a string for Styler data.N)r   Zis_pandas_styler
isinstancer   AssertionErrormarshall_stylerpaTableZpyarrow_table_to_bytesr   r)   data_frame_to_bytes)r$   r   r#   dfr%   r%   r&   r      s    
 
r   )r$   stylerr#   r   c                 C   s\   t | || |  tdr(| }n|dd}t| | t| || t| |j| dS )ao  Marshall pandas.Styler into an Arrow proto.

    Parameters
    ----------
    proto : proto.Arrow
        Output. The protobuf for Streamlit Arrow proto.

    styler : pandas.Styler
        Helps style a DataFrame or Series according to the data with HTML and CSS.

    default_uuid : str
        If pandas.Styler uuid is not provided, this value will be used.

    z1.3.0FN)	_marshall_uuidZ_computer   is_pandas_version_less_thanZ
_translate_marshall_caption_marshall_styles_marshall_display_valuesr   )r$   r8   r#   Zpandas_stylesr%   r%   r&   r3      s    


r3   c                 C   s&   |j dkr|| t|j | j_ dS )at  Marshall pandas.Styler uuid into an Arrow proto.

    Parameters
    ----------
    proto : proto.Arrow
        Output. The protobuf for Streamlit Arrow proto.

    styler : pandas.Styler
        Helps style a DataFrame or Series according to the data with HTML and CSS.

    default_uuid : str
        If pandas.Styler uuid is not provided, this value will be used.

    N)uuidZset_uuidr   r8   )r$   r8   r#   r%   r%   r&   r9      s    

r9   )r$   r8   r   c                 C   s   |j dk	r|j | j_ dS )a  Marshall pandas.Styler caption into an Arrow proto.

    Parameters
    ----------
    proto : proto.Arrow
        Output. The protobuf for Streamlit Arrow proto.

    styler : pandas.Styler
        Helps style a DataFrame or Series according to the data with HTML and CSS.

    N)captionr8   )r$   r8   r%   r%   r&   r;      s    
r;   )r$   r8   stylesr   c                 C   s   g }d|krB|d }t |}|D ] }td||jdd}|| q d|kr||d }t |}|D ]}td||j}|| q^t|dkrd|| j_dS )	aR  Marshall pandas.Styler styles into an Arrow proto.

    Parameters
    ----------
    proto : proto.Arrow
        Output. The protobuf for Streamlit Arrow proto.

    styler : pandas.Styler
        Helps style a DataFrame or Series according to the data with HTML and CSS.

    styles : dict
        pandas.Styler translated styles.

    table_styles )	separator	cellstyle
cell_styler   
N)_trim_pandas_styles_pandas_style_to_cssr>   appendlenjoinr8   r@   )r$   r8   r@   Z	css_rulesrA   styleZrulerD   r%   r%   r&   r<     s(       r<   M)bound)r@   r   c                 C   s   dd | D S )zFilter out empty styles.

    Every cell will have a class, but the list of props
    may just be [['', '']].

    Parameters
    ----------
    styles : list
        pandas.Styler translated styles.

    c                 S   s&   g | ]}t d d |d D r|qS )c                 s   s   | ]}t |V  qd S )Nany).0yr%   r%   r&   	<genexpr>H  s     z1_trim_pandas_styles.<locals>.<listcomp>.<genexpr>propsrO   )rQ   xr%   r%   r&   
<listcomp>H  s      z'_trim_pandas_styles.<locals>.<listcomp>r%   )r@   r%   r%   r&   rG   <  s    rG    )
style_typerL   r>   rC   r   c                 C   s   g }|d D ]&\}}|  d |   }|| qd| }| dksX| dkrdtdrd|d g}	n|d }	g }
|	D ]}|
|| |  qtd	|
}d
|}|d | d }|S )aT  Convert pandas.Styler translated style to CSS.

    Parameters
    ----------
    style_type : str
        Either "table_styles" or "cell_style".

    style : dict
        pandas.Styler translated style.

    uuid : str
        pandas.Styler uuid.

    separator : str
        A string separator used between table and cell selectors.

    rT   z: z#T_rA   rE   z1.1.0selector	selectorsz, z; z { z })striprI   r   r:   rK   )rX   rL   r>   rC   ZdeclarationsZcss_propertyZ	css_valueZdeclarationZtable_selectorZcell_selectorsrZ   Zcell_selectorrY   Zdeclaration_blockZrule_setr%   r%   r&   rH   K  s&    


rH   )r$   r7   r@   r   c                 C   s   t ||}t|| j_dS )a0  Marshall pandas.Styler display values into an Arrow proto.

    Parameters
    ----------
    proto : proto.Arrow
        Output. The protobuf for Streamlit Arrow proto.

    df : pandas.DataFrame
        A dataframe with original values.

    styles : dict
        pandas.Styler translated styles.

    N)_use_display_valuesr   r6   r8   Zdisplay_values)r$   r7   r@   new_dfr%   r%   r&   r=     s    
r=   )r7   r@   r   c                 C   s~   ddl }| t}|d}d|krz|d }|D ]H}|D ]>}||d }|r8tt| \}	}
t|d |j|	|
f< q8q0|S )zCreate a new pandas.DataFrame where display values are used instead of original ones.

    Parameters
    ----------
    df : pandas.DataFrame
        A dataframe with original values.

    styles : dict
        pandas.Styler translated styles.

    r   Nzrow(\d+)_col(\d+)bodyidZdisplay_value)	reZastyper   compilematchmapr.   groupsZiat)r7   r@   r`   r]   Zcell_selector_regexZrowsrowcellrb   rcr%   r%   r&   r\     s    

r\   )N)rW   )+collections.abcr   typingr   r   r   r   r   r   r	   r
   r   Zpyarrowr4   Znumpyr   Zpandasr   Zpandas.io.formats.styler   Z	streamlitr   Zstreamlit.proto.Arrow_pb2r   r   Zstreamlit.runtime.metrics_utilr   Zstreamlit.delta_generatorr   r5   r   r-   r   r   r3   r9   r;   r<   rM   rG   rH   r=   r\   r%   r%   r%   r&   <module>   sJ   ,$} #  
) 
D  
