U
    Zf                     @   s>   d dl mZ d dlmZ d dlmZ d dlmZ d
dd	ZdS )    )reducegroupby)add)htmlNTFc                    s  ddl }	ddl}
|dk	r*jdd|f dk	r`dgjD ]}| fdd|< q@ dk	rdgjD ]}|  fdd|< qv|r |dk	rjd|id	|rt|t	t
|	j|
jfrz
|_W n tk
r   td
Y nX nt|trj|d	jj}fddt|D dd D dd D fddtd|d D dd D tfddt|D g}ng }|tfddttD  | |f|S )a  
    Generate a Table component from a dataframe.

    Parameters
    ----------
    df : pandas.DataFrame
        DataFrame to render as a table.
    float_format : str, optional
        Format to use for floating point numbers.
    columns : sequence, optional
        Columns to render.
    header : boolean or list(str) or dict(str: str), optional
        Write out the column names. If a list of strings is given it is assumed
        to be aliases for the columns names (and hence must be the same length
        as df.columns). A dict can be passed to rename some columns, the format
        is {'<current_name>': '<new_name>'}. The dictionary need not have an
        entry for every column.
    index : boolean, optional
        Render the row names (index).
    index_label : str, optional
        Column label for index column if desired. If None is passed, but both
        header and index are True, then the index name is used.
    date_format : str, optional
        Format string for datetime objects.
    **table_kwargs : Keyword arguments
        Additional arguments to pass to the table component. See
        dash_bootstrap_components.Table for details.
    r   Nfloatc                    s   d  | S )Nz{1:{0}})formatx)float_format D/tmp/pip-unpacked-wheel-gvynaulb/dash_bootstrap_components/_table.py<lambda>7       z)_generate_table_from_df.<locals>.<lambda>datetimec                    s
   |   S N)strftimer	   )date_formatr   r   r   ;   r   index)columnsziIf specifying column names with a sequence, the number of names must exactly match the number of columns.c                    s   g | ]}t  j|qS r   )listr   Zget_level_values.0leveldfr   r   
<listcomp>P   s   z+_generate_table_from_df.<locals>.<listcomp>c                 S   s   g | ]}d d t |D qS )c                 S   s   g | ]\}}t t|qS r   )lenr   )r   _groupr   r   r   r   W   s     6_generate_table_from_df.<locals>.<listcomp>.<listcomp>r   )r   Zlevel_valuesr   r   r   r   V   s   c                    s,   g | ]$  fd dt dt d D qS )c                    s   g | ]}t  d | qS r   )sumr   iZlevel_spansr   r   r   ]   s     r       )ranger   r   r   r$   r   r   \   s   c              	      s.   g | ]&}t ttt d | dhqS )Nr   )sortedsetr   r   unionr   )header_breaksr   r   r   b   s   r%   c                    s2   g | ]* t  fd dtt d ddD qS )c                    s    g | ]} |  |d    qS )r%   r   r"   Zlevel_breaksr   r   r   j   s   r    r%   r   )reversedr&   r   r'   r   r,   r   r   h   s   
c                    s6   g | ]. t j fd dt    D dqS )c                    s&   g | ]\}}t j  | |d qS ))ZcolSpan)r   ZTh)r   posspan)header_valuesr   r   r   r   v   s
   
r    )children)r   Trzipr'   )r+   header_spansr1   )r   r   r   t   s    c                    s0   g | ]( t  fd dttjD qS )c                    s    g | ]}t  j|f qS r   )r   ZTdZiloc)r   j)r   r#   r   r   r      s     r    )r   r3   r&   r   r   r'   r   )r#   r   r      s   )ZnumpyZpandaslocZselect_dtypesr   mapZreset_indexrename
isinstancetupler   ZndarrayZIndex
ValueErrordictZnlevelsr&   r   ZTheadappendZTbodyr   )clsr   r   r   headerr   Zindex_labelr   Ztable_kwargsnppdcZn_levelstabler   )r   r   r   r+   r5   r1   r   _generate_table_from_df   sn    '





rE   )NNTFNN)		functoolsr   	itertoolsr   operatorr   Zdashr   rE   r   r   r   r   <module>   s         