3
Êb_àZ  ã               @   sÄ   d Z 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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 dd	lmZmZmZmZ dd
lmZ G dd„ deƒZG dd„ deƒZ dS )z,
Module for formatting output data in HTML.
é    )Údedent)
ÚIOÚAnyÚDictÚIterableÚListÚMappingÚOptionalÚTupleÚUnionÚcast)Ú
get_option)Úlib)ÚABCMultiIndex)Úoption_context)Úis_url)ÚDataFrameFormatterÚTableFormatterÚbuffer_put_linesÚget_level_lengths)Úpprint_thingc            
   @   s  e Zd ZdZdZd>eeeee	e e
edf f  ee ddœdd„Zeedœd	d
„ƒZeedœdd„ƒZeedœdd„ƒZedœdd„Zeedœdd„ƒZeedœdd„ƒZd?eeddœdd„Zd@eeeee ddœdd„ZdAeeee ddœdd„ZdBeeeee dd!œd"d#„ZdCeeeeee eeeef  edd$œd%d&„Ze	e dœd'd(„Zee dd)œd*d+„ZdDedd,œd-d.„Z edd,œd/d0„Z!edd,œd1d2„Z"edd,œd3d4„Z#eee	e f dœd5d6„Z$edd,œd7d8„Z%e&ee	e f edd9œd:d;„Z'e&ee	e f edd9œd<d=„Z(dS )EÚHTMLFormatterac  
    Internal class for formatting output data in html.
    This class is intended for shared functionality between
    DataFrame.to_html() and DataFrame._repr_html_().
    Any logic in common with other output formatting methods
    should ideally be inherited from classes in format.py
    and this class responsible for only producing html markup.
    é   N.)Ú	formatterÚclassesÚborderÚreturnc             C   sœ   || _ || _| j j| _| j jj| _g | _| j j| _| j j| _| j j| _|d kr\t	t
tdƒƒ}|| _| j j| _| j j| _t| j jt
ƒr˜dj| j jd�| j _d S )Nzdisplay.html.borderz{colspace}px)Úcolspace)Úfmtr   ÚframeÚtr_frameÚcolumnsÚelementsÚ	bold_rowsÚescapeZshow_dimensionsr   Úintr   r   Útable_idÚrender_linksÚ
isinstanceÚ	col_spaceÚformat)Úselfr   r   r   © r,   ú8/tmp/pip-build-jkwsxyiy/pandas/pandas/io/formats/html.pyÚ__init__&   s    





zHTMLFormatter.__init__)r   c             C   s   | j jS )N)r   Úshow_row_idx_names)r+   r,   r,   r-   r/   =   s    z HTMLFormatter.show_row_idx_namesc             C   s   | j jS )N)r   Úshow_col_idx_names)r+   r,   r,   r-   r0   A   s    z HTMLFormatter.show_col_idx_namesc             C   s    | j jr| jjjS | jrdS dS )Né   r   )r   Úindexr   Únlevelsr0   )r+   r,   r,   r-   Ú
row_levelsE   s
    
zHTMLFormatter.row_levelsc             C   s   | j S )N)r!   )r+   r,   r,   r-   Ú_get_columns_formatted_valuesT   s    z+HTMLFormatter._get_columns_formatted_valuesc             C   s   | j jS )N)r   Úis_truncated)r+   r,   r,   r-   r6   X   s    zHTMLFormatter.is_truncatedc             C   s   t | jjjƒS )N)Úlenr   r    r!   )r+   r,   r,   r-   Úncols\   s    zHTMLFormatter.ncolsr   )ÚsÚindentr   c             C   s    t |ƒ}| jjd| | ƒ d S )Nú )r   r"   Úappend)r+   r9   r:   Úrsr,   r,   r-   Úwrite`   s    zHTMLFormatter.writeF)r9   Úheaderr:   Útagsr   c             C   sB   |r,| j jdk	r,|pd}|dj| j jd�7 }| j|d||d� dS )a‹  
        Method for writting a formatted <th> cell.

        If col_space is set on the formatter then that is used for
        the value of min-width.

        Parameters
        ----------
        s : object
            The data to be written inside the cell.
        header : bool, default False
            Set to True if the <th> is for use inside <thead>.  This will
            cause min-width to be set if there is one.
        indent : int, default 0
            The indentation level of the cell.
        tags : str, default None
            Tags to include in the cell.

        Returns
        -------
        A written <th> cell.
        NÚ zstyle="min-width: {colspace};")r   Úth)Úkindr:   r@   )r   r)   r*   Ú_write_cell)r+   r9   r?   r:   r@   r,   r,   r-   Úwrite_thd   s    zHTMLFormatter.write_th)r9   r:   r@   r   c             C   s   | j |d||d� d S )NÚtd)rC   r:   r@   )rD   )r+   r9   r:   r@   r,   r,   r-   Úwrite_tdƒ   s    zHTMLFormatter.write_tdrF   )r9   rC   r:   r@   r   c       
      C   s¢   |d k	rdj ||d�}ndj |d�}| jr8ddddœ}ni }t||d	�jƒ }| jr€t|ƒr€t|i d	�jƒ }|d
j |d�7 }d}	nd}	| jdj |||	|d�|ƒ d S )Nz<{kind} {tags}>)rC   r@   z<{kind}>)rC   z&amp;z&lt;z&gt;)ú&ú<ú>)Zescape_charsz <a href="{url}" target="_blank">)Úurlz</a>rA   z{start}{rs}{end_a}</{kind}>)Ústartr=   Úend_arC   )r*   r$   r   Ústripr'   r   r>   )
r+   r9   rC   r:   r@   Z	start_tagÚescr=   Zrs_unescapedrM   r,   r,   r-   rD   †   s     zHTMLFormatter._write_cell)Úliner:   Úindent_deltar?   Úalignr@   Únindex_levelsr   c             C   s®   |d kri }|d kr"| j d|ƒ n| j dj|d�|ƒ ||7 }xVt|ƒD ]J\}}	|j|d ƒ}
|sn| jr‚||k r‚| j|	|||
d� qH| j|	||
d� qHW ||8 }| j d|ƒ d S )Nz<tr>z!<tr style="text-align: {align};">)rR   )r:   r?   r@   )r@   z</tr>)r>   r*   Ú	enumerateÚgetr#   rE   rG   )r+   rP   r:   rQ   r?   rR   r@   rS   Úir9   Zval_tagr,   r,   r-   Úwrite_tr¤   s    
zHTMLFormatter.write_trc             C   s@   | j ƒ  | jr:tdƒ}| jdjt| jƒ|t| jjƒd�ƒ | jS )Né×   z&<p>{rows} rows {by} {cols} columns</p>)ZrowsÚbyÚcols)	Ú_write_tableZshould_show_dimensionsÚchrr>   r*   r7   r   r!   r"   )r+   rY   r,   r,   r-   ÚrenderÁ   s    zHTMLFormatter.render)Úbufr   c             C   s   t || jƒ ƒ d S )N)r   r]   )r+   r^   r,   r,   r-   Úwrite_resultÎ   s    zHTMLFormatter.write_result)r:   r   c             C   sì   dg}t dƒ}|s|jdƒ | jd k	rpt| jtƒr>| jjƒ | _t| jttfƒsdtdj	t
| jƒd�ƒ‚|j| jƒ | jd kr€d}ndj	| jd�}| jd	j	| jd
j|ƒ|d�|ƒ | jjs¼| jrÌ| j|| j ƒ | j|| j ƒ | jd|ƒ d S )NZ	dataframezdisplay.html.use_mathjaxZtex2jax_ignorez3classes must be a string, list, or tuple, not {typ})ÚtyprA   z id="{table_id}")r&   z3<table border="{border}" class="{cls}"{id_section}>r;   )r   ÚclsÚ
id_sectionz</table>)r   r<   r   r(   ÚstrÚsplitÚlistÚtupleÚ	TypeErrorr*   ÚtypeÚextendr&   r>   r   Újoinr   r?   r/   Ú_write_headerrQ   Ú_write_body)r+   r:   Z_classesZuse_mathjaxrb   r,   r,   r-   r[   Ñ   s.    


zHTMLFormatter._write_tablec             C   sj  | j j}t| jtƒ�rÎd}| j jr*tj}nd}| jj|ddd�}t	||ƒ}t
|ƒd }�xtt||ƒƒD �]^\}\}	}
|�rò| j j}| j j�rˆi }xÐt|	jƒ ƒD ]À\}}||kr¾|||d < nr|| |k�r(|d ||< ||k�r |
d |… d |
|d …  }
n&|
d |… |
|d  f |
|d …  }
n|||< || |kr d||< |
d |… d |
|d …  }
q W |}	t
|ƒd }||k�ròd|	|< nji }x:t|	jƒ ƒD ]*\}}||k�rº|||d < n|||< �qšW d||< |}	|
d |… dg |
|d …  }
dg| jd  }| j j�s| j�rJ| j j�r@| jj| }|jt|�p8dƒƒ n
|jdƒ i }t
|ƒ}xZt|
ƒD ]N\}}||	k�r`|	| dk�rš|j|	| d�||< n�q`|d7 }|j|ƒ �q`W | j||| j|dd	� qhW n˜dg| jd  }| j j�sð| j�r| j j�r|j| jj�p
dƒ n
|jdƒ |j| jƒ ƒ | j j}|�rP| j| j j }|j|dƒ | j||| jd|d
� d S )Nz colspan="{span:d}" halign="left"F)ÚsparsifyÚadjoinÚnamesr1   ú...rA   )ÚspanT)r@   r?   )r?   rR   )rp   )rp   )r   Ú
truncate_hr(   r!   r   rm   r   Ú
no_defaultr*   r   r7   rT   ÚzipÚ
tr_col_numre   Úitemsr4   r2   r0   Zshow_index_namesro   r<   r   rW   rQ   Únameri   r5   ZjustifyÚinsert)r+   r:   rr   ÚtemplateÚsentinelÚlevelsÚlevel_lengthsÚ	inner_lvlÚlnumÚrecordsÚvaluesZins_colZrecs_newÚtagrq   Úrowrw   r@   ÚjrV   ÚvrR   r,   r,   r-   Ú_write_col_headeró   s‚    
 

( 







zHTMLFormatter._write_col_headerc             C   sL   | j j}dd„ | jjjD ƒdg| j|r*dnd   }| j||| jdd� d S )Nc             S   s   g | ]}|d k	r|nd‘qS )NrA   r,   )Ú.0Úxr,   r,   r-   ú
<listcomp>j  s    z3HTMLFormatter._write_row_header.<locals>.<listcomp>rA   r1   r   T)r?   )r   rr   r   r2   ro   r8   rW   rQ   )r+   r:   rr   r‚   r,   r,   r-   Ú_write_row_headerh  s    zHTMLFormatter._write_row_headerc             C   sJ   | j d|ƒ | jjr$| j|| j ƒ | jr:| j|| j ƒ | j d|ƒ d S )Nz<thead>z</thead>)r>   r   r?   r…   rQ   r/   r‰   )r+   r:   r,   r,   r-   rk   o  s    zHTMLFormatter._write_headerc          
      s2   t dd ƒ� ‡ fdd„tˆ jƒD ƒ}W d Q R X |S )Nzdisplay.max_colwidthc                s   i | ]}ˆ j j|ƒ|“qS r,   )r   Ú_format_col)r†   rV   )r+   r,   r-   ú
<dictcomp>|  s    z7HTMLFormatter._get_formatted_values.<locals>.<dictcomp>)r   Úranger8   )r+   Ú
fmt_valuesr,   )r+   r-   Ú_get_formatted_valuesz  s    "z#HTMLFormatter._get_formatted_valuesc             C   s`   | j d|ƒ | jƒ }| jjr>t| jjtƒr>| j||| j ƒ n| j	||| j ƒ | j d|ƒ d S )Nz<tbody>z</tbody>)
r>   rŽ   r   r2   r(   r   r   Ú_write_hierarchical_rowsrQ   Ú_write_regular_rows)r+   r:   r�   r,   r,   r-   rl     s    zHTMLFormatter._write_body)r�   r:   r   c                s,  | j j}| j j}t| j jƒ}| j jrX| j jdƒ}|d k	rJ| j jjj|ƒ}n| j jjjƒ }g }xÊt	|ƒD ]¾‰|r ˆ| j j
kr dgt|ƒ }	| j|	|| jd | jd� g }| j jr¼|j|ˆ ƒ n| jrÌ|jdƒ |j‡ ‡fdd„t	| jƒD ƒƒ |�r| j j| j }
|j|
dƒ | j||| jd | jd� qfW d S )NÚ	__index__z...)r@   rS   rA   c             3   s   | ]}ˆ | ˆ V  qd S )Nr,   )r†   rƒ   )r�   rV   r,   r-   ú	<genexpr>°  s    z4HTMLFormatter._write_regular_rows.<locals>.<genexpr>)r   rr   Ú
truncate_vr7   r    r2   Ú_get_formatterÚmapr*   rŒ   Ú
tr_row_numrW   rQ   r4   r<   r0   ri   r8   ru   rx   )r+   r�   r:   rr   r“   Únrowsr   Zindex_valuesr‚   Ústr_sep_rowZ
dot_col_ixr,   )r�   rV   r-   r�   ‹  s:    

 z!HTMLFormatter._write_regular_rowsc                sÔ  d}| j j}| j j}| j j}t|ƒ}|jjdddd�}tt|Ž ƒ}| j j	�røt
j}	|jj|	ddd�}
t|
|	ƒ}t|ƒd }|�r
| j j}tt|ƒ}d}�x4t|ƒD �]&\}}i }�xt|jƒ ƒD � ]ü\}}||krà|||d < n||| |k�rT|d ||< |�s.t||d  ƒ}d|d< |j|t|ƒƒ d}n$t|| ƒ}d||| < t|ƒ||< n|||< || |krÀd||< |dk�r–|j|tdgt|ƒ ƒƒ qÀ|rÀt|| ƒ}d||| < t|ƒ||< qÀW |||< q¢W d|| |< x&ttˆ ƒƒD ]}ˆ | j|dƒ �qèW |d7 }xêt|ƒD ]Þ‰g }i }d}d}xht||ˆ ƒD ]V\}}ˆ|k�rn|ˆ dk�rz|j|ˆ d�||< n|d7 }�q8|d7 }|j|ƒ �q8W |j‡ ‡fd	d
„t| jƒD ƒƒ |�rÒ|j| j| | j j dƒ | j||| j|t|
ƒ| d� �qW nØg }xÒtt|ƒƒD ]Â‰|�rHˆ| j jk�rHdgt|ƒ }| j||| jd | jd� tt|jjdddd�Ž ƒ}g }|j|ˆ ƒ |j‡ ‡fdd
„t| jƒD ƒƒ |�r°|j| j| j j dƒ | j||| jd |jjd� �q
W d S )Nzrowspan="{span}" valign="top"F)rm   rn   ro   r1   z...Tr   )rq   c             3   s   | ]}ˆ | ˆ V  qd S )Nr,   )r†   rƒ   )r�   rV   r,   r-   r’     s    z9HTMLFormatter._write_hierarchical_rows.<locals>.<genexpr>)r@   rS   c             3   s   | ]}ˆ | ˆ V  qd S )Nr,   )r†   rƒ   )r�   rV   r,   r-   r’   ,  s    éÿÿÿÿ)r   rr   r“   r    r7   r2   r*   re   rt   rm   r   rs   r   r–   r   r%   rT   rv   rx   rf   rŒ   r<   ri   r8   r4   ru   rW   rQ   r3   )r+   r�   r:   ry   rr   r“   r   r—   Z
idx_valuesrz   r{   r|   r}   Zins_rowZinsertedr~   r   Zrec_newr�   rq   Zdot_rowZix_colr‚   r@   Zsparse_offsetrƒ   r„   r˜   r,   )r�   rV   r-   r�   ¹  s¸    




 
 z&HTMLFormatter._write_hierarchical_rows)NN)r   )Fr   N)r   N)rF   r   N)r   r   FNNr   )r   ))Ú__name__Ú
__module__Ú__qualname__Ú__doc__rQ   r   r	   r   rc   r   r
   r%   r.   ÚpropertyÚboolr/   r0   r4   r   r5   r6   r8   r   r>   rE   rG   rD   r   rW   r]   r   r_   r[   r…   r‰   rk   rŽ   rl   r   r�   r�   r,   r,   r,   r-   r      sR    &      "u-r   c                   sd   e Zd ZdZeeee f dœdd„Zee dœdd„Z	ddœdd	„Z
ee dœ‡ fd
d„Z‡  ZS )ÚNotebookFormatterz×
    Internal class for formatting output data in html for display in Jupyter
    Notebooks. This class is intended for functionality specific to
    DataFrame._repr_html_() and DataFrame.to_html(notebook=True)
    )r   c                s   ‡ fdd„t ˆ jƒD ƒS )Nc                s   i | ]}ˆ j j|ƒ|“qS r,   )r   rŠ   )r†   rV   )r+   r,   r-   r‹   @  s    z;NotebookFormatter._get_formatted_values.<locals>.<dictcomp>)rŒ   r8   )r+   r,   )r+   r-   rŽ   ?  s    z'NotebookFormatter._get_formatted_valuesc             C   s
   | j jƒ S )N)r!   r*   )r+   r,   r,   r-   r5   B  s    z/NotebookFormatter._get_columns_formatted_valuesNc                s€   d}d}d‰ ddg}t | jtƒr<|jdƒ | jrF|jdƒ n
|jdƒ djt‡ fdd„|ƒƒ}tdj|||fƒƒ}| j|ƒ d S )Nz            <style scoped>z            </style>zM                .dataframe %s {
                    %s: %s;
                }útbody tr th:only-of-typeúvertical-alignÚmiddleútbody tr thÚtopúthead tr thú
text-alignÚleftúthead tr:last-of-type thÚrightúthead thz

c                s   ˆ |  S )Nr,   )Út)Útemplate_selectr,   r-   Ú<lambda>]  s    z/NotebookFormatter.write_style.<locals>.<lambda>Ú
)r¡   r¢   r£   )r¤   r¢   r¥   )r¦   r§   r¨   )r©   r§   rª   )r«   r§   rª   )	r(   r!   r   r<   r/   rj   r•   r   r>   )r+   Ztemplate_firstZtemplate_lastZelement_propsZtemplate_midry   r,   )r­   r-   Úwrite_styleE  s    

zNotebookFormatter.write_stylec                s,   | j dƒ | jƒ  tƒ jƒ  | j dƒ | jS )Nz<div>z</div>)r>   r°   Úsuperr]   r"   )r+   )Ú	__class__r,   r-   r]   a  s
    


zNotebookFormatter.render)rš   r›   rœ   r�   r   r%   r   rc   rŽ   r5   r°   r]   Ú__classcell__r,   r,   )r²   r-   r    8  s
   r    N)!r�   Útextwrapr   Útypingr   r   r   r   r   r   r	   r
   r   r   Zpandas._configr   Zpandas._libsr   Zpandas.core.dtypes.genericr   Zpandasr   Zpandas.io.commonr   Zpandas.io.formats.formatr   r   r   r   Zpandas.io.formats.printingr   r   r    r,   r,   r,   r-   Ú<module>   s   0    "