o
    zi                     @   s~   d dl Z d dlZd dlZd dlmZ d dlZd dlmZ	 d dl
Z
d dlmZ 		ddededejfddZd	d
 Zdd ZdS )    N)Path)HTML
audio_dictfirst_column	format_fnc                    s  ddl m  g }d} fdd}|du r|}|du rd}|  D ]\\}}t|ts-d|i}t| }	|du rQ|g|	 }|d| d	t	|	d
  }
||
 g }|dd D ]}||||| fi | qYd| d}|d|7 }|| q dd| }|S )a  Embeds an audio table into HTML, or as the output cell
    in a notebook.

    Parameters
    ----------
    audio_dict : dict
        Dictionary of data to embed.
    first_column : str, optional
        The label for the first column of the table, by default None
    format_fn : typing.Callable, optional
        How to format the data, by default None

    Returns
    -------
    str
        Table as a string

    Examples
    --------

    >>> audio_dict = {}
    >>> for i in range(signal_batch.batch_size):
    >>>     audio_dict[i] = {
    >>>         "input": signal_batch[i],
    >>>         "output": output_batch[i]
    >>>     }
    >>> audiotools.post.audio_zip(audio_dict)

    r   AudioSignalNc                    sF   t |r	| }|d u rdS t| r|jdddd|S t|S )N.FTdisplayreturn_html )torch	is_tensortolist
isinstanceembedstr)labelxkwargsr   r   C/home/ubuntu/.local/lib/python3.10/site-packages/audiotools/post.py_default_format_fn4   s   

z'audio_table.<locals>._default_format_fnr	   Audioz | z|---z|:-:   z| 
)

audiotoolsr   itemsr   dictlistkeysappendjoinlen)r   r   r   r   outputcolumnsr   kvv_keyslayoutformatted_audiocolrowr   r   r   audio_table   s2   #


r-   c                  C   sL   zddl m}  d|  jvrW dS W dS  ty   Y dS  ty%   Y dS w )zDetermines if code is running in a notebook.

    Returns
    -------
    bool
        Whether or not this is running in a notebook.
    r   get_ipythonIPKernelAppFT)IPythonr/   configImportErrorAttributeErrorr.   r   r   r   in_notebook]   s   r5   c                 K   s   ddl m} t }t| |r| jddd}|rt|S t| t| tr<t| fi |}|r8tt	j
|dgdS t| t| tjrHt  dS dS )	zDisplays an object, depending on if its in a notebook
    or not.

    Parameters
    ----------
    obj : typing.Any
        Any object to display.

    r   r   FTr
   tables)extrasN)r   r   r5   r   r   r   printr   r-   mdmarkdownpltFigureshow)objr   r   IN_NOTEBOOK
audio_elemtabler   r   r   dispq   s   


rB   )NN)tempfiletypingzipfilepathlibr   	markdown2r9   matplotlib.pyplotpyplotr;   r   IPython.displayr   r   r   Callabler-   r5   rB   r   r   r   r   <module>   s&    
Q