
    gE?              
         U d dl mZ d dlZd dlZ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 d dlmZ d dlmZmZ d d	lmZ d d
lmZ e	rd dlZd dlmZmZmZ d dlmZ  ed   Z!dZ"de#d<   dZ$ G d de%e      Z&e'e%e&f   Z(de#d<   e&jR                  e&jT                  ge&jV                  e&jX                  e&jZ                  e&jR                  e&j\                  e&jT                  ge&j^                  e&jR                  e&jV                  e&jT                  ge&jR                  e&j^                  e&jV                  e&jX                  e&jT                  ge&j`                  e&jb                  e&jT                  ge&jd                  e&jb                  e&jT                  ge&jb                  e&j`                  e&jd                  e&jT                  ge&jR                  e&jT                  gdZ3de#d<   d%dZ4d&dZ5	 	 	 	 d'dZ6	 	 	 	 d'dZ7	 d(	 	 	 	 	 d)dZ8	 	 	 	 	 	 d*dZ9e'ee!e%f   ef   Z:de#d<   eee!e%f   eede%f   f   Z;de#d<   	 d(	 	 	 d+d Z<	 	 	 	 	 	 	 	 d,d!Z=	 	 	 	 	 	 d-d"Z>d.d#Z?	 	 	 	 	 	 d/d$Z@y)0    )annotationsN)Mapping)Enum)TYPE_CHECKINGFinalLiteralUnion)	TypeAlias)
DataFormat)ColumnConfig
ColumnType)remove_none_values)StreamlitAPIException)	DataFrameIndexSeries)Arrow_indexIndexIdentifierTypeINDEX_IDENTIFIERz_pos:c                  P    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZy)ColumnDataKindintegerfloatdatetimedatetimebooleanstring	timedeltaperiodintervalbytesdecimalcomplexlistdictemptyunknownN)__name__
__module____qualname__INTEGERFLOATDATETIMEDATETIMEBOOLEANSTRING	TIMEDELTAPERIODINTERVALBYTESDECIMALCOMPLEXLISTDICTEMPTYUNKNOWN     _/var/www/openai/venv/lib/python3.12/site-packages/streamlit/elements/lib/column_config_utils.pyr   r   0   sW    GEDDHGFIFHEGGDDEGr?   r   r
   DataframeSchema)textnumbercheckbox	selectboxr   r   r   linkz-Final[dict[ColumnType, list[ColumnDataKind]]]_EDITING_COMPATIBILITY_MAPPINGc                *    | t         vry|t         |    v S )a.  Check if the column type is compatible with the underlying data kind.

    This check only applies to editable column types (e.g. number or text).
    Non-editable column types (e.g. bar_chart or image) can be configured for
    all data kinds (this might change in the future).

    Parameters
    ----------
    column_type : ColumnType
        The column type to check.

    data_kind : ColumnDataKind
        The data kind to check.

    Returns
    -------
    bool
        True if the column type is compatible with the data kind, False otherwise.
    T)rG   )column_type	data_kinds     r@   is_type_compatiblerK   n   s!    * 886{CCCr?   c                   ddl }| j                  }|j                  j                  |      rt        j
                  S |j                  j                  |      rt        j                  S |j                  j                  |      rt        j                  S |j                  j                  |      rt        j                  S |j                  j                  |      rt        j                  S |j                  j                  |      rt        j                  S |j                  j!                  |      rt        j"                  S |j                  j%                  |      rt        j&                  S |j                  j)                  |      rt        j*                  S |j                  j-                  |      rt        j.                  S |j                  j1                  |      rt        j2                  S |j                  j5                  |      rt        j6                  S |j                  j9                  |      rt        j:                  S t        j<                  S )am  Determine the data kind via the arrow type information.

    The column data kind refers to the shared data type of the values
    in the column (e.g. int, float, str, bool).

    Parameters
    ----------

    field : pa.Field
        The arrow field from the arrow table schema.

    Returns
    -------
    ColumnDataKind
        The data kind of the field.
    r   N)pyarrowtypetypes
is_integerr   r-   is_floatingr.   
is_booleanr2   	is_stringr3   is_dater/   is_timer0   is_timestampr1   is_durationr4   is_listr:   
is_decimalr8   is_nullr<   	is_binaryr7   	is_structr;   r=   )fieldpa
field_types      r@   _determine_data_kind_via_arrowr`      s   " J	xx:&%%%	xxJ'###	xx:&%%%	xx*%$$$	xx
#"""	xx
#"""	xxZ(&&&	xxJ''''	xx
#"""	xx:&%%%	xx
#### 
xx*%###	xx*%"""!!!r?   c                   ddl }| j                  }|j                  j                  j	                  |      rt
        j                  S |j                  j                  j                  |      rt
        j                  S |j                  j                  j                  |      rt
        j                  S |j                  j                  j                  |      rt
        j                  S |j                  j                  j                  |      rt
        j                  S t        ||j                         rt
        j"                  S t        ||j$                        rt
        j&                  S |j                  j                  j)                  |      rt
        j*                  S |j                  j                  j-                  |      du r5|j                  j                  j/                  |      rt
        j0                  S t
        j2                  S )a  Determine the data kind by using the pandas dtype.

    The column data kind refers to the shared data type of the values
    in the column (e.g. int, float, str, bool).

    Parameters
    ----------
    column : pd.Series, pd.Index
        The column for which the data kind should be determined.

    Returns
    -------
    ColumnDataKind
        The data kind of the column.
    r   NF)pandasdtypeapirO   is_bool_dtyper   r2   is_integer_dtyper-   is_float_dtyper.   is_datetime64_any_dtyper1   is_timedelta64_dtyper4   
isinstancePeriodDtyper5   IntervalDtyper6   is_complex_dtyper9   is_object_dtypeis_string_dtyper3   r=   )columnpdcolumn_dtypes      r@   %_determine_data_kind_via_pandas_dtypers      sa   $ <<L	vv||!!,/%%%	vv||$$\2%%%	vv||""<0###	vv||++L9&&&	vv||((6''',/$$$, 0 01&&&	vv||$$\2%%%	vv||##	66<<//=$$$!!!r?   c                   ddl m}  ||       }|dk(  rt        j                  S |dk(  rt        j                  S |dv rt        j
                  S |dk(  rt        j                  S |dk(  rt        j                  S |dk(  rt        j                  S |d	k(  rt        j                  S |d
v rt        j                  S |dk(  rt        j                  S |dv rt        j                  S |dk(  rt        j                  S |dk(  rt        j                  S |dk(  rt        j                  S |dk(  rt        j                   S t        j"                  S )a  Determine the data kind by inferring it from the underlying data.

    The column data kind refers to the shared data type of the values
    in the column (e.g. int, float, str, bool).

    Parameters
    ----------
    column : pd.Series, pd.Index
        The column to determine the data kind for.

    Returns
    -------
    ColumnDataKind
        The data kind of the column.
    r   )infer_dtyper   r#   )floatingzmixed-integer-floatr   r$   r%   r   )
datetime64r   r   )timedelta64r    r   r!   r"   r(   )pandas.api.typesru   r   r3   r7   r.   r-   r8   r9   r2   r1   r/   r4   r0   r5   r6   r<   r=   )rp   ru   inferred_types      r@   &_determine_data_kind_via_inferred_typer{     s:   $ -'M $$$###;;###	!%%%	!%%%	!%%%	!%%%22&&&"""44'''""" $$$
"&&&### !!!r?   c                *   ddl }t        | j                  |j                        rt	        | j                  j
                        S | t        |      }|t        j                  k7  r|S | j                  j                  dk(  rt	        |       S t        |       S )a  Determine the data kind of a column.

    The column data kind refers to the shared data type of the values
    in the column (e.g. int, float, str, bool).

    Parameters
    ----------
    column : pd.Series, pd.Index
        The column to determine the data kind for.
    field : pa.Field, optional
        The arrow field from the arrow table schema.

    Returns
    -------
    ColumnDataKind
        The data kind of the column.
    r   Nobject)rb   rj   rc   CategoricalDtyper{   
categoriesr`   r   r=   namers   )rp   r]   rq   rJ   s       r@   _determine_data_kindr   F  s~    ( &,, 3 34 6fll6M6MNN259	...||H$5f==088r?   c                    i }t        | j                        |t        <   t        | j	                               D ](  \  }}|\  }}t        ||j                  |            ||<   * |S )a  Determine the schema of a dataframe.

    Parameters
    ----------
    data_df : pd.DataFrame
        The dataframe to determine the schema of.
    arrow_schema : pa.Schema
        The Arrow schema of the dataframe.

    Returns
    -------

    DataframeSchema
        A mapping that contains the detected data type for the index and columns.
        The key is the column name in the underlying dataframe or ``_index`` for index columns.
    )r   indexr   	enumerateitemsr]   )data_dfarrow_schemadataframe_schemairp   column_namecolumn_datas          r@   determine_dataframe_schemar   l  sn    ( )+ *>gmm)L%& w}}/	6#) [(<++A.)
% 0
 r?   ColumnConfigMappingColumnConfigMappingInputc           	     ,   | i S i }| j                         D ]z  \  }}|t        d      ||<   t        |t              rt        |      ||<   8t        |t              rt        j                  |      ||<   at        d| dt        |       d       |S )a?  Transforms a user-provided column config mapping into a valid column config mapping
    that can be used by the frontend.

    Parameters
    ----------
    column_config: dict or None
        The user-provided column config mapping.

    Returns
    -------
    dict
        The transformed column config mapping.
    T)hidden)labelz"Invalid column config for column `z.`. Expected `None`, `str` or `dict`, but got `z`.)	r   r   rj   strr'   copydeepcopyr   rN   )column_configtransformed_column_configrp   configs       r@   process_config_mappingr     s      	57'--/>0<D0I%f-$0<60J%f-% 15f0E%f-'4VH =>>B6l^2O  0 %$r?   c                >    || vri | |<   | |   j                  |       y)aU  Updates the column config value for a single column within the mapping.

    Parameters
    ----------

    column_config_mapping : ColumnConfigMapping
        The column config mapping to update.

    column : str
        The column to update the config value for.

    column_config : ColumnConfig
        The column config to update.
    N)update)column_config_mappingrp   r   s      r@   update_column_configr     s+    $ **(*f%&!((7r?   c                   |t         j                  t         j                  t         j                  t         j                  t         j
                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  fv rt!        | t"        ddi       yy)a~  Apply data specific configurations to the provided dataframe.

    This will apply inplace changes to the dataframe and the column configurations
    depending on the data format.

    Parameters
    ----------
    columns_config : ColumnConfigMapping
        A mapping of column names/ids to column configurations.

    data_format : DataFormat
        The format of the data.
    r   TN)r   SET_OF_VALUESTUPLE_OF_VALUESLIST_OF_VALUES
NUMPY_LISTNUMPY_MATRIXLIST_OF_RECORDSLIST_OF_ROWSCOLUMN_VALUE_MAPPINGPANDAS_ARRAYPANDAS_INDEXPOLARS_DATAFRAMEPOLARS_SERIESPOLARS_LAZYFRAMEPYARROW_ARRAYRAY_DATASETr   r   )columns_configdata_formats     r@   apply_data_specific_configsr     s    *   ""!!""''##  ##  ! $ 	^-=$?OP%r?   c           
        	 t        j                  t        |       j                         D ci c]+  \  }}t	        |t
              rt         t        |       n||- c}}d      S c c}}w # t        $ r}t        d|       |d }~ww xY w)NF)	allow_nanz;The provided column config cannot be serialized into JSON: )
jsondumpsr   r   rj   int_NUMERICAL_POSITION_PREFIXr   
ValueErrorr   )r   kvexs       r@   _convert_column_config_to_jsonr     s    zz
 11FGMMO	 PFQ @J!S?Q123q6(;WXO	 
 	
  #I"N
	s(   +A- 0A'
	A- 'A- -	B
6BB
c                $    t        |      | _        y)zMarshall the column config into the Arrow proto.

    Parameters
    ----------
    proto : ArrowProto
        The proto to marshall into.

    column_config_mapping : ColumnConfigMapping
        The column config to marshall.
    N)r   columns)protor   s     r@   marshall_column_configr     s     33HIEMr?   )rI   r   rJ   r   returnbool)r]   zpa.Fieldr   r   )rp   Series | Indexr   r   )N)rp   r   r]   zpa.Field | Noner   r   )r   r   r   z	pa.Schemar   rA   )r   zColumnConfigMappingInput | Noner   r   )r   r   rp   r   r   r   r   None)r   r   r   r   r   r   )r   r   r   r   )r   
ArrowProtor   r   r   r   )A
__future__r   r   r   collections.abcr   enumr   typingr   r   r   r	   typing_extensionsr
   streamlit.dataframe_utilr   #streamlit.elements.lib.column_typesr   r    streamlit.elements.lib.dicttoolsr   streamlit.errorsr   rM   r^   rb   r   r   r   streamlit.proto.Arrow_pb2r   r   r   r   __annotations__r   r   r   r'   rA   r3   r<   r-   r.   r8   r4   r2   r/   r1   r0   rG   rK   r`   rs   r{   r   r   r   r   r   r   r   r   r   r>   r?   r@   <module>r      s   #   #  7 7 ' / H ? 2//= h' (0 % 0
 % S$ . "#~"56 6
 ""N$8$89   		 	   ."9"9>;O;OP  ."9"9>;O;OP	 ""N$8$89? Q  M  FD6?"D3"3"3"lB"B"B"L 6:#9#9#2#9#9L  &/  H "&e,?,D&E|&S!T Y T&-	
s
"#	,c
!"$' )  6:"%2"%"%J8.88;8LX8	80'Q''Q'Q 
'QT$JJ.AJ	Jr?   