o
    g                     @  sb  d dl mZ d dlmZ d dlmZ d dl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mZ d dl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 d dl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z( d dl)m*  m+Z, edddZ-d dl.m/Z0 ee1e	f Z2ee2eeee2 f f Z3ee1ee1e4e5f f Z6ee6 Z7ee1e7f Z8G dd deZ9ee9 Z:ee;ee"f Z<G dd dZ=dndd Z>dod"d#Z?	dpdqd+d,Z@drd-d.ZAdsd1d2ZBdtdud7d8ZCdvd=d>ZDd?d@ ZE				A		dwdxdFdGZFdydJdKZGdzdOdPZHG dQdR dRZId{dUdVZJd|dXdYZK	3dtd}d]d^ZL	3dtd~dddeZMddhdiZNddjdkZOdldm ZPdS )    )annotations)defaultdict)partialN)	AnyCallableDefaultDictDictListOptionalSequenceTupleUnion)uuid4)
get_option)lib)FrameOrSeriesUnion	TypedDict)import_optional_dependency)	ABCSeries)	DataFrameIndex
IndexSlice
MultiIndexSeriesisna)is_list_likejinja2z DataFrame.style requires jinja2.)extraescapec                   @  s   e Zd ZU ded< ded< dS )CSSDictstrselectorCSSPropertiespropsN)__name__
__module____qualname____annotations__ r)   r)   \/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pandas/io/formats/style_render.pyr    5   s   
 r    c                   @  s   e Zd ZdZeddZejeddZe	dZ
e	dZe	dZe	d	Z	
		
	
	
	dLdMddZdNdd ZdNd!d"Zd#d$ ZdOdPd(d)ZdQd1d2ZdRd8d9ZdSd=d>Z	
	
	
	
	?	
	
dTdUdJdKZd
S )VStylerRendererzT
    Base class to process rendering a Styler with a specified jinja2 template.
    pandaszio/formats/templatesT)loadertrim_blockszhtml.tplzhtml_table.tplzhtml_style.tplz	latex.tplN   datar   uuid
str | Noneuuid_leninttable_stylesCSSStyles | Nonetable_attributescaptionstr | tuple | Nonecell_idsboolc                   s   t |tr	| }t |tstd|| _|j| _|j| _t |tr&|dks*tdt	d|| _
|p:t jd | j
 d | _|| _|| _|| _|| _d| _d| _g | _g | _tt| _tt| _g | _d | _td t fdd	| _d S )
Nz&``data`` must be a Series or DataFramer   z1``uuid_len`` must be an integer in range [0, 32].    _Fdisplay.precisionc                     s   t t dS )N)	precision)r   _default_formatterr)   def_precisionr)   r*   <lambda>r   s    z)StylerRenderer.__init__.<locals>.<lambda>)
isinstancer   to_framer   	TypeErrorr0   indexcolumnsr4   minr3   r   hexr1   r5   r7   r8   r:   hide_index_hide_columns_hidden_rowshidden_columnsr   listctxr!   cell_context_todotooltipsr   _display_funcs)selfr0   r1   r3   r5   r7   r8   r:   r)   rA   r*   __init__J   s4   



zStylerRenderer.__init__sparse_indexsparse_columnsreturnr!   c                 K  s>   |    | ||}|| | jjdi || j| jdS )z
        Renders the ``Styler`` including all applied styles to HTML.
        Generates a dict with necessary kwargs passed to jinja2 template.
        )html_table_tplhtml_style_tplNr)   )_compute
_translateupdatetemplate_htmlrendertemplate_html_tabletemplate_html_stylerU   rW   rX   kwargsdr)   r)   r*   _render_htmlt   s   


zStylerRenderer._render_htmlc                 K  sn   |    | j||dd}| | t| jjd< t| jjd< t| jjd< t| jjd< |	| | jj
di |S )	z1
        Render a Styler in latex format
         )blank
parse_wrapparse_table
parse_cellparse_headerNr)   )r\   r]   _translate_latex_parse_latex_table_wrappingtemplate_latexglobals_parse_latex_table_styles_parse_latex_cell_styles_parse_latex_header_spanr^   r`   rc   r)   r)   r*   _render_latex   s   

zStylerRenderer._render_latexc                 C  s6   | j   | }| jD ]\}}}|| |i |}q
|S )a  
        Execute the style functions built up in `self._todo`.

        Relies on the conventions that all style functions go through
        .apply or .applymap. The append styles to apply as tuples of

        (application method, *args, **kwargs)
        )rP   clearrR   )rU   rfuncargsrd   r)   r)   r*   r\      s
   
	zStylerRenderer._compute&nbsp;sparse_colsrh   c              	   C  s:  d}d}d}d}d}d}	d}
|}| j t| jpg | jd}td	}tt| jjt| jj	|\}}| 
|
||||||}|d
|i tt| _| |	||||||}|d|i dd | j D }|d|i | j}td}|s|pwd}d|v r|dd}n|d7 }|d|i | jr| j| j| j |}|S )aV  
        Process Styler data and settings into a dict for template rendering.

        Convert data and settings from ``Styler`` attributes such as ``self.data``,
        ``self.tooltips`` including applying any methods in ``self._todo``.

        Parameters
        ----------
        sparse_index : bool
            Whether to sparsify the index or print all hierarchical index elements.
            Upstream defaults are typically to `pandas.options.styler.sparse.index`.
        sparse_cols : bool
            Whether to sparsify the columns or print all hierarchical column elements.
            Upstream defaults are typically to `pandas.options.styler.sparse.columns`.

        Returns
        -------
        d : dict
            The following structure: {uuid, table_styles, caption, head, body,
            cellstyle, table_attributes}
        row_headingcol_heading
index_namecol_trimrow_trimr0   rh   )r1   r5   r8   zstyler.render.max_elementsheadbodyc                 S  s   g | ]\}}t ||d qS ))r$   	selectors)rO   ).0r$   r   r)   r)   r*   
<listcomp>   s    z-StylerRenderer._translate.<locals>.<listcomp>	cellstylezdisplay.html.use_mathjaxrg   zclass="zclass="tex2jax_ignore z class="tex2jax_ignore"r7   )r1   _format_table_stylesr5   r8   r   _get_trimming_maximumslenr0   rG   rH   _translate_headerr^   r   rO   cellstyle_map_translate_bodyitemsr7   replacerS   r]   )rU   rW   rz   rh   ROW_HEADING_CLASSCOL_HEADING_CLASSINDEX_NAME_CLASSTRIMMED_COL_CLASSTRIMMED_ROW_CLASS
DATA_CLASSBLANK_CLASSBLANK_VALUEre   max_elementsmax_rowsmax_colsr   r   r   
table_attruse_mathjaxr)   r)   r*   r]      sl   		zStylerRenderer._translateblank_classblank_valueindex_name_classcol_heading_classsparsify_colsr   trimmed_col_classc              
     s  t j||jjj d| }jjjdkr!dd |D }tt| }g }	jst	jjjD ]kt
d j gjjjd  }
jjj }t
d|du rU n d |dura|nj g}|rfddt| D }tjj|kr|t
d d d| d	d
dd |	|
| |  q3jjjrtjjjj rjsjsfddtjjjD }tjj|krt|d }nt|d d } fddt	|D }|	||  |	S )a  
        Build each <tr> within table <head> as a list

        Using the structure:
             +----------------------------+---------------+---------------------------+
             |  index_blanks ...          | column_name_0 |  column_headers (level_0) |
          1) |       ..                   |       ..      |             ..            |
             |  index_blanks ...          | column_name_n |  column_headers (level_n) |
             +----------------------------+---------------+---------------------------+
          2) |  index_names (level_0 to level_n) ...      | column_blanks ...         |
             +----------------------------+---------------+---------------------------+

        Parameters
        ----------
        blank_class : str
            CSS class added to elements within blank sections of the structure.
        blank_value : str
            HTML display value given to elements within blank sections of the structure.
        index_name_class : str
            CSS class added to elements within the index_names section of the structure.
        col_heading_class : str
            CSS class added to elements within the column_names section of structure.
        sparsify_cols : bool
            Whether column_headers section will add colspan attributes (>1) to elements.
        max_cols : int
            Maximum number of columns to render. If exceeded will contain `...` filler.
        trimmed_col_class : str
            CSS class added to elements within a column including `...` trimmed vals.

        Returns
        -------
        head : list
            The associated HTML elements needed for template rendering.
        N   c                 S     g | ]}|gqS r)   r)   r   xr)   r)   r*   r   0      z4StylerRenderer._translate_header.<locals>.<listcomp>th levelc                   sf   g | ]/\}}t d   d d| |t||fddkr-d|fd dnddqS )	r   r    colr   r   	colspan=""rg   
attributes)_element_is_visiblegetr   cvalue)r   col_lengthsrv   r)   r*   r   F  s    
 ...Trg   r   c                   s4   g | ]\}}t d  d| |du r n|dqS )r   r   NT)r   )r   r   name)r   r   r)   r*   r   i  s    r   c                   s*   g | ]}t d   d| |jvqS )r   r   r   rN   r   r   )r   r   rU   r)   r*   r   x  s    )_get_level_lengthsrH   rN   r0   tolistnlevelsrO   ziprL   ranger   rK   rG   names	enumerater   appendcomany_not_none)rU   r   r   r   r   r   r   r   clabelsr   index_blanksr   column_namecolumn_headersindex_names	blank_lencolumn_blanksr)   )r   r   r   r   r   rv   rU   r*   r      sr   -	
	
	z StylerRenderer._translate_header
data_classrow_heading_classsparsify_indexr   trimmed_row_classc                   sX  t j||jjj d| }jjjdkr!dd |D }g }	tj D ]\}
|krufddtjjjD } fddt|D }t	jj
|krk|td  d d| d	d
dd |	||   |	S fddt| D }g }t|
dd D ]\}}||kr|td  d d| d	d
dd  nud}|fjv rdj|f  }td  d d| | ||jvoֈjvdj|f |d}js|fjv rd d| |d< |fjv rj|f rjtj|f  d d|  || q|	||  q*|	S )a  
        Build each <tr> within table <body> as a list

        Use the following structure:
          +--------------------------------------------+---------------------------+
          |  index_header_0    ...    index_header_n   |  data_by_column           |
          +--------------------------------------------+---------------------------+

        Also add elements to the cellstyle_map for more efficient grouped elements in
        <style></style> block

        Parameters
        ----------
        data_class : str
            CSS class added to elements within data_by_column sections of the structure.
        row_heading_class : str
            CSS class added to elements within the index_header section of structure.
        sparsify_index : bool
            Whether index_headers section will add rowspan attributes (>1) to elements.

        Returns
        -------
        body : list
            The associated HTML elements needed for template rendering.
        Nr   c                 S  r   r)   r)   r   r)   r)   r*   r     r   z2StylerRenderer._translate_body.<locals>.<listcomp>c              	     s2   g | ]}t d   d| d dj ddqS )r   r   r   r   rg   r   )r   rK   r   )r   rU   r   r)   r*   r     s    c              	     s4   g | ]}t d   d| d d|jvddqS )tdr   r   r   rg   r   r   r   )r   rU   r   r)   r*   r     s    r   r   r   Trg   r   c                   s|   g | ]:\}}t d  d| d |t| oj d| d  |fddkr8d |fd dnd	d
qS )r   r    rowlevel_rowr   r   	rowspan="r   rg   )idr   )r   r   rK   r   r   )idx_lengthsrv   r   rU   r)   r*   r     s    r   r   )r   display_valuerow_colr   )r   rG   rM   r0   r   r   r   
itertuplesr   r   rH   r   r   rQ   rN   rT   r:   rP   r   tuple)rU   r   r   r   r   r   r   r   rlabelsr   row_tupindex_headersr0   r   r   clsdata_elementr)   )r   r   rv   r   rU   r   r*   r     s   $
9
	
 zStylerRenderer._translate_bodyre   dictNonec                   s|   dd |d D |d< g }t |d D ]$\ }jrg }ndd |D } fddt |D }|||  q||d< dS )a  
        Post-process the default render dict for the LaTeX template format.

        Processing items included are:
          - Remove hidden columns from the non-headers part of the body.
          - Place cellstyles directly in td cells rather than use cellstyle_map.
          - Remove hidden indexes or reinsert missing th elements if part of multiindex
            or multirow sparsification (so that \multirow and \multicol work correctly).
        c                 S  s   g | ]	}d d |D qS )c                 S  s   g | ]}|d  r|qS )
is_visibler)   r   colr)   r)   r*   r     s    z>StylerRenderer._translate_latex.<locals>.<listcomp>.<listcomp>r)   )r   r   r)   r)   r*   r     s    z3StylerRenderer._translate_latex.<locals>.<listcomp>r   r   c                 S  s8   g | ]}|d  dkri |d|d r|d ndiqS )typer   r   r   rg   r)   r   r)   r)   r*   r   "  s    
c                   sH   g | ] \}}|d  r"|d dkri |dj  |jjj f iqS )r   r   r   r   )rP   r0   rG   r   )r   r   r   rv   rU   r)   r*   r   -  s    "N)r   rK   r   )rU   re   r   r   row_body_headersrow_body_cellsr)   r   r*   rm     s   
zStylerRenderer._translate_latex.	formatterExtFormatter | NonesubsetSubset | Nonena_repr?   
int | Nonedecimal	thousandsr   c              	     s   t  du |du |du |dk|du |du |du fr | j  | S |du r(tdn|}t|}| jj| }t tsC fdd|j	D  | j	
|j	}	| j
|j}
|	D ]}t | j	| |||||d}|
D ]	}|| j||f< qgqS| S )u  
        Format the text display value of cells.

        Parameters
        ----------
        formatter : str, callable, dict or None
            Object to define how values are displayed. See notes.
        subset : label, array-like, IndexSlice, optional
            A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
            or single key, to `DataFrame.loc[:, <subset>]` where the columns are
            prioritised, to limit ``data`` to *before* applying the function.
        na_rep : str, optional
            Representation for missing values.
            If ``na_rep`` is None, no special formatting is applied.

            .. versionadded:: 1.0.0

        precision : int, optional
            Floating point precision to use for display purposes, if not determined by
            the specified ``formatter``.

            .. versionadded:: 1.3.0

        decimal : str, default "."
            Character used as decimal separator for floats, complex and integers

            .. versionadded:: 1.3.0

        thousands : str, optional, default None
            Character used as thousands separator for floats, complex and integers

            .. versionadded:: 1.3.0

        escape : str, optional
            Use 'html' to replace the characters ``&``, ``<``, ``>``, ``'``, and ``"``
            in cell display string with HTML-safe sequences.
            Use 'latex' to replace the characters ``&``, ``%``, ``$``, ``#``, ``_``,
            ``{``, ``}``, ``~``, ``^``, and ``\`` in the cell display string with
            LaTeX-safe sequences.
            Escaping is done before ``formatter``.

            .. versionadded:: 1.3.0

        Returns
        -------
        self : Styler

        Notes
        -----
        This method assigns a formatting function, ``formatter``, to each cell in the
        DataFrame. If ``formatter`` is ``None``, then the default formatter is used.
        If a callable then that function should take a data value as input and return
        a displayable representation, such as a string. If ``formatter`` is
        given as a string this is assumed to be a valid Python format specification
        and is wrapped to a callable as ``string.format(x)``. If a ``dict`` is given,
        keys should correspond to column names, and values should be string or
        callable, as above.

        The default formatter currently expresses floats and complex numbers with the
        pandas display precision unless using the ``precision`` argument here. The
        default formatter does not adjust the representation of missing values unless
        the ``na_rep`` argument is used.

        The ``subset`` argument defines which region to apply the formatting function
        to. If the ``formatter`` argument is given in dict form but does not include
        all columns within the subset then these columns will have the default formatter
        applied. Any columns in the formatter dict excluded from the subset will
        be ignored.

        When using a ``formatter`` string the dtypes must be compatible, otherwise a
        `ValueError` will be raised.

        Examples
        --------
        Using ``na_rep`` and ``precision`` with the default ``formatter``

        >>> df = pd.DataFrame([[np.nan, 1.0, 'A'], [2.0, np.nan, 3.0]])
        >>> df.style.format(na_rep='MISS', precision=3)
                0       1       2
        0    MISS   1.000       A
        1   2.000    MISS   3.000

        Using a ``formatter`` specification on consistent column dtypes

        >>> df.style.format('{:.2f}', na_rep='MISS', subset=[0,1])
                0      1          2
        0    MISS   1.00          A
        1    2.00   MISS   3.000000

        Using the default ``formatter`` for unspecified columns

        >>> df.style.format({0: '{:.2f}', 1: '£ {:.1f}'}, na_rep='MISS', precision=1)
                 0      1     2
        0    MISS   £ 1.0     A
        1    2.00    MISS   3.0

        Multiple ``na_rep`` or ``precision`` specifications under the default
        ``formatter``.

        >>> df.style.format(na_rep='MISS', precision=1, subset=[0])
        ...     .format(na_rep='PASS', precision=2, subset=[1, 2])
                0      1      2
        0    MISS   1.00      A
        1     2.0   PASS   3.00

        Using a callable ``formatter`` function.

        >>> func = lambda s: 'STRING' if isinstance(s, str) else 'FLOAT'
        >>> df.style.format({0: '{:.1f}', 2: func}, precision=4, na_rep='MISS')
                0        1        2
        0    MISS   1.0000   STRING
        1     2.0     MISS    FLOAT

        Using a ``formatter`` with HTML ``escape`` and ``na_rep``.

        >>> df = pd.DataFrame([['<div></div>', '"A&B"', None]])
        >>> s = df.style.format(
        ...     '<a href="a.com/{0}">{0}</a>', escape="html", na_rep="NA"
        ...     )
        >>> s.render()
        ...
        <td .. ><a href="a.com/&lt;div&gt;&lt;/div&gt;">&lt;div&gt;&lt;/div&gt;</a></td>
        <td .. ><a href="a.com/&#34;A&amp;B&#34;">&#34;A&amp;B&#34;</a></td>
        <td .. >NA</td>
        ...

        Using a ``formatter`` with LaTeX ``escape``.

        >>> df = pd.DataFrame([["123"], ["~ ^"], ["$%#"]])
        >>> s = df.style.format("\\textbf{{{}}}", escape="latex").to_latex()
        \begin{tabular}{ll}
        {} & {0} \\
        0 & \textbf{123} \\
        1 & \textbf{\textasciitilde \space \textasciicircum } \\
        2 & \textbf{\$\%\#} \\
        \end{tabular}
        Nr   c                   s   i | ]}| qS r)   r)   r   r   r)   r*   
<dictcomp>  r   z)StylerRenderer.format.<locals>.<dictcomp>)r   r?   r   r   r   )allrT   ru   slicenon_reducing_slicer0   locrD   r   rH   get_indexer_forrG   _maybe_wrap_formatterr   )rU   r   r   r   r?   r   r   r   r0   cisrisciformat_funcrir)   r   r*   format6  sB    

zStylerRenderer.format)Nr/   NNNT)r0   r   r1   r2   r3   r4   r5   r6   r7   r2   r8   r9   r:   r;   )rW   r;   rX   r;   rY   r!   )ry   )rW   r;   rz   r;   rh   r!   )r   r!   r   r!   r   r!   r   r!   r   r;   r   r4   r   r!   )r   r!   r   r!   r   r;   r   r4   r   r4   r   r!   r   r!   )re   r   rY   r   )NNNNr   NN)r   r   r   r   r   r2   r?   r   r   r!   r   r2   r   r2   rY   r+   )r%   r&   r'   __doc__r   PackageLoaderr-   Environmentenvget_templater_   ra   rb   ro   rV   rf   rt   r\   r]   r   r   rm   r   r)   r)   r)   r*   r+   >   s@    




*

Z 
	 
&r+   html_elementr!   
html_classr   r   r   r;   rY   r   c                 K  s"   d|vr||d< | |||d|S )z]
    Template to return container with information for a <td></td> or <th></th> element.
    r   )r   r   classr   r)   )r  r  r   r   rd   r)   r)   r*   r     s   
r   皙?c                   s:    fdd}| | |kr|| |\} }| | |ks| |fS )aa  
    Recursively reduce the number of rows and columns to satisfy max elements.

    Parameters
    ----------
    rn, cn : int
        The number of input rows / columns
    max_elements : int
        The number of allowable elements

    Returns
    -------
    rn, cn : tuple
        New rn and cn values that satisfy the max_elements constraint
    c                   s(   || kr| t |  fS t |   |fS N)r4   )rncnscaling_factorr)   r*   
scale_down  s   z*_get_trimming_maximums.<locals>.scale_downr)   )r	  r
  r   r  r  r)   r  r*   r     s
   r   rG   r   sparsify	max_indexr4   hidden_elementsSequence[int] | Nonec                 C  s>  t | tr| jtjdd}n|  }|du rg }i }| jdkr4t|D ]\}}||vr1d|d|f< q#|S t|D ][\}}t|D ]R\}	}
|	|krJ nI|sSd|||	f< q@|
tjure|	|vre|	}d|||f< q@|
tjurs|	}d|||f< q@|	|vr|||f dkr|	}d|||f< q@|||f  d7  < q@q8dd | D }|S )aZ  
    Given an index, find the level length for each element.

    Parameters
    ----------
    index : Index
        Index or columns to determine lengths of each element
    sparsify : bool
        Whether to hide or show each distinct element in a MultiIndex
    max_index : int
        The maximum number of elements to analyse along the index due to trimming
    hidden_elements : sequence of int
        Index positions of elements hidden from display in the index affecting
        length

    Returns
    -------
    Dict :
        Result is a dictionary of (level, initial_position): span
    F)r  adjoinNr   r   c                 S  s   i | ]\}}|d kr||qS )r   r)   )r   elementlengthr)   r)   r*   r   b  s    z&_get_level_lengths.<locals>.<dictcomp>)rD   r   r   r   
no_defaultr   r   r   )rG   r  r  r  levelslengthsir   lvljr   
last_labelnon_zero_lengthsr)   r)   r*   r   !  sD   


r   c                 C  s   || f|v S )z/
    Index -> {(idx_row, idx_col): bool}).
    r)   )idx_rowidx_colr  r)   r)   r*   r   i  s   r   styles	CSSStylesc                 C  s   dd | D S )z
    looks for multiple CSS selectors and separates them:
    [{'selector': 'td, th', 'props': 'a:v;'}]
        ---> [{'selector': 'td', 'props': 'a:v;'},
              {'selector': 'th', 'props': 'a:v;'}]
    c                 S  s.   g | ]}|d   dD ]	}||d dqqS )r"   ,r$   r"   r$   )split)r   css_dictr"   r)   r)   r*   r   w  s    z(_format_table_styles.<locals>.<listcomp>r)   )r  r)   r)   r*   r   p  s   r   Fr   r?   r   c                 C  sL   t | ttfr|r| d| dS | d| dS t | tr$|r$| dS | S )a  
    Format the display of a value

    Parameters
    ----------
    x : Any
        Input variable to be formatted
    precision : Int
        Floating point precision used if ``x`` is float or complex.
    thousands : bool, default False
        Whether to group digits with thousands separated with ",".

    Returns
    -------
    value : Any
        Matches input type, or string if input is float or complex or int with sep.
    z,.fr   z,.0f)rD   floatcomplexr4   )r   r?   r   r)   r)   r*   r@   ~  s   r@   r   r   r   r2   c                   s    fdd}|S )z
    Takes a string formatting function and wraps logic to deal with thousands and
    decimal parameters, in the case that they are non-standard and that the input
    is a (float, complex, int).
    c                   s   t | tttfrL dkr$d ur$dkr$| ddd dS  dkr8d u s0dkr8| d S  dkrLd urLdkrL| dS | S )Nr   r!  u   §_§-)rD   r&  r'  r4   r   r   r   r   r   r)   r*   wrapper  s   z(_wrap_decimal_thousands.<locals>.wrapperr)   )r   r   r   r*  r)   r)  r*   _wrap_decimal_thousands  s   	r+  c                 C  s<   t | tr|dkrt| S |dkrt| S td| | S )z/if escaping: only use on str, else return inputhtmllatexz2`escape` only permitted in {'html', 'latex'}, got )rD   r!   escape_html_escape_latex
ValueError)r   r   r)   r)   r*   _str_escape  s   
r1  r   BaseFormatter | Noner   r   r   c                   s   t trfddn(trn!du r+|du rtdn|}tt||dudn	tdt  dur@ fdd}n}|dksN|durV|d	krVt|||d
n|du r^S fddS )z
    Allows formatters to be expressed as str, callable or None, where None returns
    a default formatting function. wraps with na_rep, and precision where they are
    available.
    c                   s
     | S r  )r   r(  r   r)   r*   rC     s   
 z'_maybe_wrap_formatter.<locals>.<lambda>Nr>   )r?   r   z*'formatter' expected str or callable, got c                   s   t |  dS )Nr   )r1  r(  )r   func_0r)   r*   rC     s    r   r!  )r   r   c                   s   t | rS  | S r  )r   r(  )func_2r   r)   r*   rC     s    )	rD   r!   callabler   r   r@   rF   r   r+  )r   r   r?   r   r   r   func_1r)   )r   r   r3  r4  r   r*   r     s&   

r   slice_Subsetc                   s~   t tjtttf}t| |rtdd| f } ddd t| s2t| t	s+| gg} t
| S | g} t
| S  fdd| D } t
| S )	z
    Ensure that a slice doesn't reduce to a Series or Scalar.

    Any user-passed `subset` should have this called on it
    to make sure we're always working with DataFrames.
    NrY   r;   c                 S  s.   t | trtdd | D S t | tpt| S )z
        Returns
        -------
        bool
            True if slice does *not* reduce,
            False if `part` is a tuple.
        c                 s  s"    | ]}t |tpt|V  qd S r  )rD   r   r   )r   sr)   r)   r*   	<genexpr>  s     z3non_reducing_slice.<locals>.pred.<locals>.<genexpr>)rD   r   anyr   r   )partr)   r)   r*   pred  s   

z non_reducing_slice.<locals>.predc                   s   g | ]} |r
|n|gqS r)   r)   )r   pr=  r)   r*   r     s    z&non_reducing_slice.<locals>.<listcomp>rY   r;   )r   npndarrayr   rO   r!   rD   r   r   r   r   )r7  kindsr)   r?  r*   r     s   	


r   styler#   CSSListc                 C  sH   t | tr"| d}zdd |D W S  ty!   td|  dw | S )z
    Convert css-string to sequence of tuples format if needed.
    'color:red; border:1px solid black;' -> [('color', 'red'),
                                             ('border','1px solid red')]
    ;c                 S  s<   g | ]}|  d kr|dd   |dd   fqS )rg   :r   r   )stripr#  r   r)   r)   r*   r   #  s
    "z/maybe_convert_css_to_tuples.<locals>.<listcomp>zSStyles supplied as string must follow CSS rule formats, for example 'attr: val;'. 'z' was given.)rD   r!   r#  
IndexErrorr0  )rD  r9  r)   r)   r*   maybe_convert_css_to_tuples  s   

rJ  c                   @  sH   e Zd ZdZg dde fdd
dZedd ZdddZdddZ	dS ) Tooltipsa  
    An extension to ``Styler`` that allows for and manipulates tooltips on hover
    of ``<td>`` cells in the HTML result.

    Parameters
    ----------
    css_name: str, default "pd-t"
        Name of the CSS class that controls visualisation of tooltips.
    css_props: list-like, default; see Notes
        List of (attr, value) tuples defining properties of the CSS class.
    tooltips: DataFrame, default empty
        DataFrame of strings aligned with underlying Styler data for tooltip
        display.

    Notes
    -----
    The default properties for the tooltip CSS class are:

        - visibility: hidden
        - position: absolute
        - z-index: 1
        - background-color: black
        - color: white
        - transform: translate(-20px, -20px)

    Hidden visibility is a key prerequisite to the hover functionality, and should
    always be included in any manual properties specification.
    ))
visibilityhidden)positionabsolute)zz-indexr   )background-colorblack)colorwhite)	transformztranslate(-20px, -20px)zpd-t	css_propsr#   css_namer!   rS   r   c                 C  s   || _ || _|| _g | _d S r  )
class_nameclass_propertiestt_datar5   )rU   rU  rV  rS   r)   r)   r*   rV   N  s   
zTooltips.__init__c                 C  s   d| j  t| jdgS )a  
        Combine the ``_Tooltips`` CSS class name and CSS properties to the format
        required to extend the underlying ``Styler`` `table_styles` to allow
        tooltips to render in HTML.

        Returns
        -------
        styles : List
        r   r"  )rW  rJ  rX  )rU   r)   r)   r*   _class_styles`  s   
zTooltips._class_stylesr1   r   r   r4   r   textc                 C  sZ   d| d t | d t | }|d|  dgd|d| d d	d
| d
fgdgS )a:  
        For every table data-cell that has a valid tooltip (not None, NaN or
        empty string) must create two pseudo CSS entries for the specific
        <td> element id which are added to overall table styles:
        an on hover visibility change and a content change
        dependent upon the user's chosen display string.

        For example:
            [{"selector": "T__row1_col1:hover .pd-t",
             "props": [("visibility", "visible")]},
            {"selector": "T__row1_col1 .pd-t::after",
             "props": [("content", "Some Valid Text String")]}]

        Parameters
        ----------
        uuid: str
            The uuid of the Styler instance
        name: str
            The css-name of the class used for styling tooltips
        row : int
            The row index of the specified tooltip string data
        col : int
            The col index of the specified tooltip string data
        text : str
            The textual content of the tooltip to be displayed in HTML.

        Returns
        -------
        pseudo_css : List
        z#T_r   r   z:hover .)rL  visibler"  z .z::aftercontentr   )r!   )rU   r1   r   r   r   r[  selector_idr)   r)   r*   _pseudo_cssr  s    zTooltips._pseudo_cssstyler_datar   re   r   c                   s   j |_ j jr|S jj  j dB  dd  fddttj jD D _	j	rg|d D ]}|D ]}|d dkrUt
|d d	j d
 |d< q>q:|d j |d j	 |S )a=  
        Mutate the render dictionary to allow for tooltips:

        - Add ``<span>`` HTML element to each data cells ``display_value``. Ignores
          headers.
        - Add table level CSS styles to control pseudo classes.

        Parameters
        ----------
        styler_data : DataFrame
            Underlying ``Styler`` DataFrame used for reindexing.
        uuid : str
            The underlying ``Styler`` uuid for CSS id.
        d : dict
            The dictionary prior to final render

        Returns
        -------
        render_dict : Dict
        rg   c                 S  s   g | ]	}|D ]}|qqS r)   r)   )r   sublistrD  r)   r)   r*   r     s    z'Tooltips._translate.<locals>.<listcomp>c                   sR   g | ]%}t tjjD ]} j||f s||tjj||f qqS r)   )r   r   rY  rH   ilocr_  r!   )r   r  r  maskr   rU   r1   r)   r*   r     s     r   r   r   r   z<span class="z	"></span>r5   )rY  reindex_likeemptyrW  r   eqr   r   rG   r5   r!   extendrZ  )rU   r`  r1   re   r   itemr)   rc  r*   r]     s.   
zTooltips._translateN)rU  r#   rV  r!   rS   r   )
r1   r!   r   r!   r   r4   r   r4   r[  r!   )r`  r   r1   r!   re   r   )
r%   r&   r'   r   r   rV   propertyrZ  r_  r]   r)   r)   r)   r*   rK  0  s    

+rK  r5   r8   c                   s.   g d | duot  fdd| D p|duS )a8  
    Indicate whether LaTeX {tabular} should be wrapped with a {table} environment.

    Parses the `table_styles` and detects any selectors which must be included outside
    of {tabular}, i.e. indicating that wrapping must occur, and therefore return True,
    or if a caption exists and requires similar.
    )toprulemidrule
bottomrulecolumn_formatNc                 3  s    | ]	}|d   vV  qdS )r"   Nr)   )r   re   IGNORED_WRAPPERSr)   r*   r:    s    z._parse_latex_table_wrapping.<locals>.<genexpr>)r;  )r5   r8   r)   ro  r*   rn     s   rn   r"   c                 C  sD   | ddd D ]}|d |krt |d d d dd  S qdS )	ux  
    Return the first 'props' 'value' from ``tables_styles`` identified by ``selector``.

    Examples
    --------
    >>> table_styles = [{'selector': 'foo', 'props': [('attr','value')],
    ...                 {'selector': 'bar', 'props': [('attr', 'overwritten')]},
    ...                 {'selector': 'bar', 'props': [('a1', 'baz'), ('a2', 'ignore')]}]
    >>> _parse_latex_table_styles(table_styles, selector='bar')
    'baz'

    Notes
    -----
    The replacement of "§" with ":" is to avoid the CSS problem where ":" has structural
    significance and cannot be used in LaTeX labels, but is often required by them.
    Nr"   r$   r   r      §rG  )r!   r   )r5   r"   rD  r)   r)   r*   rq     s
    rq   latex_stylesr   convert_cssc              
   C  s   |rt | } | ddd D ]P\}}d| d| dd| d| d| d| d| d| dd| d	| dd
}d| | d| }dD ]}|t|v r\|| dt||d} nqFq|S )a  
    Mutate the ``display_value`` string including LaTeX commands from ``latex_styles``.

    This method builds a recursive latex chain of commands based on the
    CSSList input, nested around ``display_value``.

    If a CSS style is given as ('<command>', '<options>') this is translated to
    '\<command><options>{display_value}', and this value is treated as the
    display value for the next iteration.

    The most recent style forms the inner component, for example for styles:
    `[('c1', 'o1'), ('c2', 'o2')]` this returns: `\c1o1{\c2o2{display_value}}`

    Sometimes latex commands have to be wrapped with curly braces in different ways:
    We create some parsing flags to identify the different behaviours:

     - `--rwrap`        : `\<command><options>{<display_value>}`
     - `--wrap`         : `{\<command><options> <display_value>}`
     - `--nowrap`       : `\<command><options> <display_value>`
     - `--lwrap`        : `{\<command><options>} <display_value>`
     - `--dwrap`        : `{\<command><options>}{<display_value>}`

    For example for styles:
    `[('c1', 'o1--wrap'), ('c2', 'o2')]` this returns: `{\c1o1 \c2o2{display_value}}
    Nrq  z{\z--to_parse }\z--to_parse} z--to_parse{z--to_parse}{)--wrap--nowrap--lwrap--rwrap--dwrapr   )rx  rw  ry  rz  r{  z
--to_parser   arg)_parse_latex_css_conversionr!   r   _parse_latex_options_strip)rs  r   rt  commandoptionsr   r}  r)   r)   r*   rr     s&   rr   celldict[str, Any]multirow_alignmulticol_alignwrapc                 C  s   d| v rX| d }d|v r0|| dd d }t|d| d }d| d| d| d  d	S d
|v rX|| d
d d }t|d| d }d| d| d| d  d	S |rbd| d  d	S | d S )a  
    Refactor the cell `display_value` if a 'colspan' or 'rowspan' attribute is present.

    'rowspan' and 'colspan' do not occur simultaneouly. If they are detected then
    the `display_value` is altered to a LaTeX `multirow` or `multicol` command
    respectively, with the appropriate cell-span.

    ``wrap`` is used to enclose the `display_value` in braces which is needed for
    column headers using an siunitx package.

    Requires the package {multirow}, whereas multicol support is usually built in
    to the {tabular} environment.

    Examples
    --------
    >>> cell = {'display_vale':'text', 'attributes': 'colspan="3"'}
    >>> _parse_latex_header_span(cell, 't', 'c')
    '\multicol{3}{c}{text}'
    r   r   	   Nr   z\multicolumn{z}{r   ru  r   z
\multirow[z]{z}{*}{{)findr4   )r  r  r  r  attrscolspanrowspanr)   r)   r*   rs   +  s&   rs   str | int | floatr}  c                 C  s$   t | |ddddd S )z
    Strip a css_value which may have latex wrapping arguments, css comment identifiers,
    and whitespaces, to a valid string for latex options parsing.

    For example: 'red /* --wrap */  ' --> 'red'
    rg   z/*z*/)r!   r   rH  r|  r)   r)   r*   r  W  s   $r  c                 C  s   dd }dd }dd }|t |ddd	t |d
dd	|d}g }| D ]F\}}t|tr9d|v r9|||ddf || v rgd}dD ]}	|	t|v rU|	t||	}} nqC|| ||}
|
durg||
g q!|S )z
    Convert CSS (attribute,value) pairs to equivalent LaTeX (command,options) pairs.

    Ignore conversion if tagged with `--latex` option, skipped if no conversion found.
    c                 S  s   | dks| dkrd| fS d S )Nboldbolderbfseriesr)   r|  r)   r)   r*   font_weighth  s   
z0_parse_latex_css_conversion.<locals>.font_weightc                 S  s(   | dkr	d| fS | dkrd| fS d S )Nitalicitshapeobliqueslshaper)   r|  r)   r)   r*   
font_stylem  s
   

z/_parse_latex_css_conversion.<locals>.font_stylec           	   	   S  s  |dkr|n|}| d dkr$t | dkr$|d| dd   d| fS | d dkrTt | d	krT| d  d
  | d
  d
  | d  d
  }|d| d| fS | dd dkrtd| d  }d|v rtt|dd d nt|d }td| d  }d|v rt|dd d nt|d }| d dkrtd| d  }n
td| d  }d|v rt|dd d nt|d }|d|dd|dd|dd| fS |d|  d| fS )a  
        CSS colors have 5 formats to process:

         - 6 digit hex code: "#ff23ee"     --> [HTML]{FF23EE}
         - 3 digit hex code: "#f0e"        --> [HTML]{FF00EE}
         - rgba: rgba(128, 255, 0, 0.5)    --> [rgb]{0.502, 1.000, 0.000}
         - rgb: rgb(128, 255, 0,)          --> [rbg]{0.502, 1.000, 0.000}
         - string: red                     --> {red}

        Additionally rgb or rgba can be expressed in % which is also parsed.
        rg   r   #   z[HTML]{r   Nru           rgbz(?<=\()[0-9\s%]+(?=,)%rq  d      z(?<=,)[0-9\s%]+(?=,)az(?<=,)[0-9\s%]+(?=\))z[rgb]{z.3fz, r  )r   upperrefindallrH  r&  r4   )	r   user_argr  comm_argr}  valrv   gbr)   r)   r*   rR  t  s"    4(((&z*_parse_latex_css_conversion.<locals>.color	cellcolorry  )r  r  rR  rg   )zfont-weightrP  rR  z
font-stylez--latex)rw  rx  ry  r{  rz  N)r   rD   r!   r   r   keysr  rh  )r  r  r  rR  CONVERTED_ATTRIBUTESrs  	attributer   r}  r   latex_styler)   r)   r*   r~  a  s0   "r~  c                 C  st   |  dd dd dd dd d	d
 dd dd dd dd dd dd dd dd ddS )al  
    Replace the characters ``&``, ``%``, ``$``, ``#``, ``_``, ``{``, ``}``,
    ``~``, ``^``, and ``\`` in the string with LaTeX-safe sequences.

    Use this if you need to display text that might contain such characters in LaTeX.

    Parameters
    ----------
    s : str
        Input to be escaped

    Return
    ------
    str :
        Escaped string
    rv  u   ab2§=§8yzu   ab2§=§8yz u   ab2§=§8yz\space &z\&r  z\%$z\$r  z\#r=   z\_r  z\{ru  z\}z~ z~\space ~z\textasciitilde z^ z^\space ^z\textasciicircum z\textbackslash )r   )r9  r)   r)   r*   r/    s   
r/  )
r  r!   r  r!   r   r   r   r;   rY   r   )r  r  )rG   r   r  r;   r  r4   r  r  r@  )r  r   rY   r   )F)r   r   r?   r4   r   r;   rY   r   )r   r   r   r!   r   r2   rY   r   )NNNr   NN)r   r2  r   r2   r?   r   r   r!   r   r2   r   r2   rY   r   )r7  r8  )rD  r#   rY   rE  )r5   r   r8   r2   rY   r;   )r5   r   r"   r!   rY   r2   )rs  rE  r   r!   rt  r;   rY   r!   )
r  r  r  r!   r  r!   r  r;   rY   r!   )r   r  r}  r!   rY   r!   )r  rE  rY   rE  )Q
__future__r   collectionsr   	functoolsr   r  typingr   r   r   r   r	   r
   r   r   r   r1   r   numpyrA  pandas._configr   pandas._libsr   pandas._typingr   r   pandas.compat._optionalr   pandas.core.dtypes.genericr   r,   r   r   r   r   r   r   pandas.api.typesr   pandas.core.commoncorecommonr   r   
markupsafer   r.  r!   BaseFormatterExtFormatterr4   r&  CSSPairrE  r#   r    r   r   r8  r+   r   r   r   r   r   r@   r+  r1  r   r   rJ  rK  rn   rq   rr   rs   r  r~  r/  r)   r)   r)   r*   <module>   sx    ,      
6
!
H


-
+ 
%
1
,

L