o
    Zh                  
   @   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 deded	ed
edef
ddZdedef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 }|rz| d }W n	 t y   Y nw |r|dkr|rd|d  v r_|d d }|d d }|rP|d d }d	| d
| d| d| d| dS d	| d
| d| d| d	S |d d dkr|d d }t|t|}	d	| d
| d|d|	  dS dS d	| d
| 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   d/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/notebook.pyconcatenate_cells   sB   
r   xc                 C   sN   t | tr| ddS t | trdd | D S t | tr%dd |  D S | 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   s    z#remove_newlines.<locals>.<listcomp>c                 S   s   i | ]	\}}|t |qS r   r    r"   kvr   r   r   
<dictcomp>H   s    z#remove_newlines.<locals>.<dictcomp>)
isinstancestrreplacelistdictitemsr   r   r   r   r!   @   s   


r!   c                   @   sR   e Zd ZdZ				ddeeef dedededef
d	d
Z	de
e f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
   r3   r   )selfr2   r	   r
   r3   r   r   r   r   __init__P   s
   
zNotebookLoader.__init__r   c                    s   t  j}t|dd}t|}W d   n1 sw   Y  dd |d D } jr3t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   s    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   r/   r5   r   r   <lambda>   s    z%NotebookLoader.load.<locals>.<lambda>r   )Zpage_contentmetadata)r   r4   openjsonloadr3   r,   mapr!   joinr*   r   )r5   pfdZfiltered_datar   r<   r   r:   r   r?   k   s&   

zNotebookLoader.loadN)Fr1   FF)__name__
__module____qualname____doc__r   r*   r   boolintr6   r   r   r?   r   r   r   r   r0   M   s(    

r0   )rH   r>   pathlibr   typingr   r   r   Zlangchain_core.documentsr   Z)langchain_community.document_loaders.baser   r-   rI   rJ   r*   r   r!   r0   r   r   r   r   <module>   s&    
4