U
    h                     @   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 }|r6z| d }W n t k
r4   Y nX |r|dkr|rd|d  k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   Q/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/document_loaders/notebook.pyconcatenate_cells   s0    "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   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>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
   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 5 Q R X dd |d D } jrRttt|}d	tt fdd|}d	t
|i}t||d
gS )zLoad documents.utf8)encodingc                 S   s   g | ]}d d |  D qS )c                 S   s   i | ]\}}|d k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