
    q-Ph                        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rddlmZ ddlmZ dd	lmZ ddZ G d d          Z G d d          Z G 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                0    t          j        dd |           S )z9Replace consecutive spaces with HTML non-breaking spaces.z( {2,})c                L    dt          |                     d                    z  S )Nz&nbsp;r   )lengroup)matchs    V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/polars/dataframe/_html.py<lambda>z,replace_consecutive_spaces.<locals>.<lambda>   s    Hs5;;q>>7J7J,J     )resub)r
   s    r   replace_consecutive_spacesr      s    6*JJANNNr   c                  .    e Zd ZdZ	 dddZddZddZdS )Tagz#Class for representing an HTML tag.Nelements	list[str]tagr   
attributesdict[str, str] | Noner   Nonec                0    || _         || _        || _        d S N)r   r   r   )selfr   r   r   s       r   __init__zTag.__init__   s      $r   c                *   | j         hd| j         d}| j                                         D ]\  }}|| d| dz  }|                                 d}| j                            |           d S | j                            d| j         d           d S )N< z="z" >)r   r   itemsrstripr   append)r"   r
   kvs       r   	__enter__zTag.__enter__%   s    ?&DHA--// # #1]]Q]]]"88::   AM  #####M  TX11111r   exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbTracebackType | Nonec                L    | j                             d| j         d           d S )Nz</r'   )r   r*   r   )r"   r.   r0   r2   s       r   __exit__zTag.__exit__/   s.     	-$(---.....r   r!   )r   r   r   r   r   r   r   r   r   r   )r.   r/   r0   r1   r2   r3   r   r   )__name__
__module____qualname____doc__r#   r-   r5    r   r   r   r      s`        -- -1	% % % % %2 2 2 2/ / / / / /r   r   c                  @    e Zd ZddddddZddZddZddZddZdS )HTMLFormatterK   (   F)max_colsmax_rowsfrom_seriesdfr	   r@   intrA   rB   boolr   r   c               ^   || _         g | _        || _        || _        || _        |  |  ||j        k     rft          |d          \  }}g t          t          ||z                       dt          t          |j        |z
  |j                            | _	        nt          |j                  | _	        ||j
        k     rWg t          t          |dz                      dt          t          |j
        |dz  z
  |j
                            | _        d S t          |j
                  | _        d S )N   )rC   r   r@   rA   rB   heightdivmodlistrangerow_idxwidthcol_idx)r"   rC   r@   rA   rB   halfrests          r   r#   zHTMLFormatter.__init__9   s7    #%  &##bi!,,JD$eD4K(()) eBI,bi8899DLL !++DLbheHM**++ eBHx1}4bh??@@DLLL !??DLLLr   c           
     Z   t          | j        d          5  t          t          t          j                            dd                              st          | j        d          5  | j        j        }| j	        D ]}t          | j        d          5  |dk    r| j        
                    d           n2| j        
                    t          j        ||                              ddd           n# 1 swxY w Y   	 ddd           n# 1 swxY w Y   t          t          t          j                            d	d                              st          | j        d          5  | j        j                                        }| j	        D ]p}t          | j        d
          5  |dk    r| j        
                    d           n | j        
                    ||                    ddd           n# 1 swxY w Y   q	 ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )z"Write the header of an HTML table.thead"POLARS_FMT_TABLE_HIDE_COLUMN_NAMES0trthrH   &hellip;N'POLARS_FMT_TABLE_HIDE_COLUMN_DATA_TYPEStd)r   r   rE   rD   osenvirongetrC   columnsrO   r*   r   escape_dfdtype_strings)r"   r^   cdtypess       r   write_headerzHTMLFormatter.write_header[   s   (( 	@ 	@BJNN+OQTUUVVWW N-- N N"goG!\ N N 55 N N Bww $ 4 4Z @ @ @ @ $ 4 4T[5L5L M M M	N N N N N N N N N N N N N N NNN N N N N N N N N N N N N N N BJNN#LcRRSS  
@ -- @ @!W[6688F!\ @ @ 55 @ @ Bww $ 4 4Z @ @ @ @ $ 4 4VAY ? ? ?	@ @ @ @ @ @ @ @ @ @ @ @ @ @ @@@ @ @ @ @ @ @ @ @ @ @ @ @ @ @	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   AH &+D
AC1	%D
1C55D
8C59D
>H 
D	H D	AH $=H!AG/	#H/G33H6G37H<H H	H H	H  H$'H$c                   t          t          j                            dd                    }t	          | j        d          5  | j        D ]}t	          | j        d          5  | j        D ]}t	          | j        d          5  |dk    s|dk    r| j                            d           nc| j	        d	d	|f         }| j                            t          t          j        |j                            ||                                         d	d	d	           n# 1 swxY w Y   	 d	d	d	           n# 1 swxY w Y   	 d	d	d	           d	S # 1 swxY w Y   d	S )
z Write the body of an HTML table.POLARS_FMT_STR_LEN   )defaulttbodyrV   rZ   rH   rX   N)rD   r[   r\   r]   r   r   rM   rO   r*   rC   r   r   r_   _sget_fmt)r"   str_len_limitrrb   seriess        r   
write_bodyzHTMLFormatter.write_bodys   s   BJNN+?NLLMM(( 	" 	"\ " "-- " "!\ 
" 
" 55 	" 	" Bww!r'' $ 4 4Z @ @ @ @)-A $ 4 4$>(,FI4E4Ea4W4W(X(X%& %&!" !" !"	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"
"" " " " " " " " " " " " " " ""	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s[   E#D2BDD2DD2 D!D2&E2D66E9D6:EEEinnerr   c                :    | j                             |           dS )z&Append a raw string to the inner HTML.N)r   r*   )r"   rp   s     r   writezHTMLFormatter.write   s    U#####r   r   c                   t          t          t          j                            dd                              sU| 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          5  |                                  |                                  ddd           n# 1 swxY w Y   | j        S )/Return the lines needed to render a HTML table.1POLARS_FMT_TABLE_HIDE_DATAFRAME_SHAPE_INFORMATIONrU   (r   _z,)z,    )z<small>shape: z</small>table1	dataframe)borderclassN)rE   rD   r[   r\   r]   rC   shaperB   r   r*   r   rd   ro   )r"   r
   r   s      r   renderzHTMLFormatter.render   sM   
RTWXX 
 
 		C A&*&6SN!NNNNN<S!<S<S<S!A$<S<S<S<SEM  !A%!A!A!ABBB M[11
 
 	 	 OO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 }s   ))CC"%C"N)
rC   r	   r@   rD   rA   rD   rB   rE   r   r   r6   )rp   r   r   r   r   r   )r7   r8   r9   r#   rd   ro   rr   r   r;   r   r   r=   r=   8   s        
 ! +  +  +  +  +  +D@ @ @ @0" " " "$$ $ $ $     r   r=   c                  ,     e Zd ZdZddZd fdZ x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                N    d}|                      t          |                     d S )Nz            <style>
            .dataframe > thead > tr,
            .dataframe > tbody > tr {
              text-align: right;
              white-space: pre-wrap;
            }
            </style>
        )rr   r   )r"   styles     r   write_stylezNotebookFormatter.write_style   s)     	

6%==!!!!!r   r   c                    t          | j        d          5  |                                  t                                                       ddd           n# 1 swxY w Y   | j        S )rt   divN)r   r   r   superr   )r"   	__class__s    r   r   zNotebookFormatter.render   s    && 	 	GGNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 }s   5AAAr6   r   )r7   r8   r9   r:   r   r   __classcell__)r   s   @r   r   r      s[         
" 
" 
" 
"         r   r   )r
   r   r   r   )r:   
__future__r   r[   r   textwrapr   typingr   polars.dependenciesr   collections.abcr   typesr   polarsr	   r   r   r=   r   r;   r   r   <module>r      sI   0 0 " " " " " " 				 				                   $ $ $ $ $ $ !((((((######      O O O O
/ / / / / / / /@g g g g g g g gT         r   