o
    i                     @  s   d Z 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
 er6ddlmZ ddlmZ dd	lmZ dddZG dd dZG dd dZG dd deZdS )z*Module for formatting output data in HTML.    )annotationsN)dedent)TYPE_CHECKING)html)Iterable)TracebackType)	DataFramesstrreturnc                 C  s   t ddd | S )z9Replace consecutive spaces with HTML non-breaking spaces.z( {2,})c                 S  s   dt | d S )Nz&nbsp;r   )lengroup)match r   M/home/app/Keep/.python/lib/python3.10/site-packages/polars/dataframe/_html.py<lambda>   s    z,replace_consecutive_spaces.<locals>.<lambda>)resub)r	   r   r   r   replace_consecutive_spaces   s   r   c                   @  s2   e Zd ZdZ	ddddZdddZdddZdS )Tagz#Class for representing an HTML tag.Nelements	list[str]tagr
   
attributesdict[str, str] | Noner   Nonec                 C  s   || _ || _|| _d S N)r   r   r   )selfr   r   r   r   r   r   __init__   s   
zTag.__init__c                 C  sx   | j d ur/d| j d}| j  D ]\}}|| d| d7 }q|  d}| j| d S | jd| j d d S )N< z="z" >)r   r   itemsrstripr   append)r   r	   kvr   r   r   	__enter__%   s   
zTag.__enter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | Nonec                 C  s   | j d| j d d S )Nz</r!   )r   r$   r   )r   r(   r*   r,   r   r   r   __exit__/   s   zTag.__exit__r   )r   r   r   r
   r   r   r   r   r   r   )r(   r)   r*   r+   r,   r-   r   r   )__name__
__module____qualname____doc__r   r'   r.   r   r   r   r   r      s    


r   c                   @  sH   e Zd ZdddddddZdddZdddZdddZdddZdS ) HTMLFormatterK   (   F)max_colsmax_rowsfrom_seriesdfr   r7   intr8   r9   boolr   r   c                C  s   || _ g | _|| _|| _|| _|  |  ||jk r8t|d\}}g tt|| dtt|j| |j| _	nt|j| _	||j
k r_g tt|d dtt|j
|d  |j
| _d S t|j
| _d S )N   )r:   r   r7   r8   r9   heightdivmodlistrangerow_idxwidthcol_idx)r   r:   r7   r8   r9   Zhalfrestr   r   r   r   9   s4   


zHTMLFormatter.__init__c                 C  s  t | jd tttjdds^t | jd> | jj}| j	D ].}t | jd |dkr4| j
d n| j
t||  W d   n1 sIw   Y  q W d   n1 sYw   Y  tttjd	dst | jd= | jj }| j	D ]+}t | jd
 |dkr| j
d n| j
||  W d   n1 sw   Y  qyW d   n1 sw   Y  W d   dS W d   dS W d   dS 1 sw   Y  dS )z"Write the header of an HTML table.ZtheadZ"POLARS_FMT_TABLE_HIDE_COLUMN_NAMES0trthr>   &hellip;NZ'POLARS_FMT_TABLE_HIDE_COLUMN_DATA_TYPEStd)r   r   r<   r;   osenvirongetr:   columnsrE   r$   r   escapeZ_dfZdtype_strings)r   rO   cZdtypesr   r   r   write_header[   sD   


"zHTMLFormatter.write_headerc                 C  s  t tjjddd}t| jdi | jD ]\}t| jdL | jD ]@}t| jd0 |dks1|dkr8| jd n| j	d	d	|f }| jt
t|j|| W d	   n1 s[w   Y  q W d	   n1 skw   Y  qW d	   d	S 1 s|w   Y  d	S )
z Write the body of an HTML table.ZPOLARS_FMT_STR_LEN   )defaultZtbodyrH   rK   r>   rJ   N)r;   rL   rM   rN   r   r   rC   rE   r$   r:   r   r   rP   Z_sZget_fmt)r   Zstr_len_limitrrQ   Zseriesr   r   r   
write_bodys   s,   

"zHTMLFormatter.write_bodyinnerr
   c                 C  s   | j | dS )z&Append a raw string to the inner HTML.N)r   r$   )r   rW   r   r   r   write   s   zHTMLFormatter.writer   c                 C  s   t ttjdds4| jj}| jrd|d ddnd|d dd|d dd	}| j	d
| d t
| jdddd |   |   W d   | jS 1 sSw   Y  | jS )/Return the lines needed to render a HTML table.Z1POLARS_FMT_TABLE_HIDE_DATAFRAME_SHAPE_INFORMATIONrG   (r   _z,)z,    )z<small>shape: z</small>table1Z	dataframe)borderclassN)r<   r;   rL   rM   rN   r:   shaper9   r   r$   r   rR   rV   )r   r	   rb   r   r   r   render   s&   6

		zHTMLFormatter.renderN)
r:   r   r7   r;   r8   r;   r9   r<   r   r   r/   )rW   r
   r   r   r   r   )r0   r1   r2   r   rR   rV   rX   rc   r   r   r   r   r4   8   s    
"

r4   c                      s,   e Zd ZdZd	ddZd
 fddZ  ZS )NotebookFormatterz
    Class for formatting output data in HTML for display in Jupyter Notebooks.

    This class is intended for functionality specific to DataFrame._repr_html_().
    r   r   c                 C  s   d}|  t| d S )Nz            <style>
            .dataframe > thead > tr,
            .dataframe > tbody > tr {
              text-align: right;
              white-space: pre-wrap;
            }
            </style>
        )rX   r   )r   styler   r   r   write_style   s   	zNotebookFormatter.write_styler   c                   sH   t | jd |   t   W d   | jS 1 sw   Y  | jS )rY   divN)r   r   rg   superrc   )r   	__class__r   r   rc      s   
zNotebookFormatter.renderr/   rd   )r0   r1   r2   r3   rg   rc   __classcell__r   r   rj   r   re      s    
re   )r	   r
   r   r
   )r3   
__future__r   rL   r   textwrapr   typingr   Zpolars._dependenciesr   collections.abcr   typesr   Zpolarsr   r   r   r4   re   r   r   r   r   <module>   s    
 j