a
    bg                     @   s~   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ eeeeeddd	Zeed
ddZG dd deZdS )zLoads .ipynb notebook files.    N)Path)AnyListUnion)Document)
BaseLoader)cellinclude_outputsmax_output_length	tracebackreturnc           
      C   s*  | d }| d }|r4z| d }W n t y2   Y n0 |r|dkr|rd|d  v r|d d }|d d }|r|d d }d	| d
| d| d| d| dS d	| d
| d| d| d	S nL|d d dkr&|d d }t|t|}	d	| d
| d|d|	  dS nd	| d
| dS dS )as  Combine cells information in a readable format ready to be used.

    Args:
        cell: A dictionary
        include_outputs: Whether to include the outputs of the cell.
        max_output_length: Maximum length of the output to be displayed.
        traceback: Whether to return a traceback of the error.

    Returns:
        A string with the cell information.

    	cell_typesourceoutputscodeZenamer   Zevaluer   'z	' cell: 'z'
, gives error 'z', with description 'z'
and traceback 'z'

z',with description 'output_typestreamtextz'
 with output: 'N )KeyErrorkeysminlen)
r   r	   r
   r   r   r   outputZ
error_nameZerror_valueZ
min_output r   {/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/notebook.pyconcatenate_cells   s@    
r   )xr   c                 C   sR   t | tr| ddS t | tr.dd | D S t | trJdd |  D S | S dS )zMRecursively remove newlines, no matter the data structure they are stored in.
r   c                 S   s   g | ]}t |qS r   remove_newlines).0elemr   r   r   
<listcomp>F       z#remove_newlines.<locals>.<listcomp>c                 S   s   i | ]\}}|t |qS r   r    r"   kvr   r   r   
<dictcomp>H   r%   z#remove_newlines.<locals>.<dictcomp>N)
isinstancestrreplacelistdictitemsr   r   r   r   r!   @   s    


r!   c                   @   sB   e Zd ZdZdeeef eeeedddZ	e
e ddd	Zd
S )NotebookLoaderz'Load `Jupyter notebook` (.ipynb) files.F
   )pathr	   r
   remove_newliner   c                 C   s"   || _ || _|| _|| _|| _dS )a  Initialize with a path.

        Args:
            path: The path to load the notebook from.
            include_outputs: Whether to include the outputs of the cell.
                Defaults to False.
            max_output_length: Maximum length of the output to be displayed.
                Defaults to 10.
            remove_newline: Whether to remove newlines from the notebook.
                Defaults to False.
            traceback: Whether to return a traceback of the error.
                Defaults to False.
        N)	file_pathr	   r
   r4   r   )selfr3   r	   r
   r4   r   r   r   r   __init__P   s
    zNotebookLoader.__init__)r   c                    s   t  j}t|dd}t|}W d   n1 s60    Y  dd |d D } jrfttt|}d	tt fdd	|}d
t
|i}t||dgS )zLoad documents.utf8)encodingNc                 S   s   g | ]}d d |  D qS )c                 S   s   i | ]\}}|d v r||qS ))r   r   r   r   r&   r   r   r   r)   u   r%   z2NotebookLoader.load.<locals>.<listcomp>.<dictcomp>)r/   )r"   r   r   r   r   r$   t   s   z'NotebookLoader.load.<locals>.<listcomp>cellsr   c                    s   t |  j j jS )N)r   r	   r
   r   r0   r6   r   r   <lambda>   s   z%NotebookLoader.load.<locals>.<lambda>r   )Zpage_contentmetadata)r   r5   openjsonloadr4   r-   mapr!   joinr+   r   )r6   pfdZfiltered_datar   r=   r   r;   r   r@   k   s$    
(
zNotebookLoader.loadN)Fr2   FF)__name__
__module____qualname____doc__r   r+   r   boolintr7   r   r   r@   r   r   r   r   r1   M   s       
r1   )rI   r?   pathlibr   typingr   r   r   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   r.   rJ   rK   r+   r   r!   r1   r   r   r   r   <module>   s   4