
    Ng              
           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ZdedefdZ G 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                    | d         }| d         }|r	 | d         }n# t           $ r Y nw xY w|r|dk    r|rd|d                                         v rM|d         d         }|d         d         }|r |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 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codeenamer   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   output
error_nameerror_value
min_outputs
             i/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/notebook.pyconcatenate_cellsr#      s    [!I(^F 	)_FF 	 	 	D	  59..6.fQinn&&&&7+J )H-K "1Ik2	7	 7 7F 7 7j 7 7*57 7&/7 7 7<	 < <F < <j < <)4< < < AY}%11AYv&F.F<<J7I 7 7 7 7";J;/7 7 7 2 5944v44442s    
**xc                    t          | t                    r|                     dd          S t          | t                    rd | D             S t          | t                    rd |                                 D             S | S )zMRecursively remove newlines, no matter the data structure they are stored in.
r   c                 ,    g | ]}t          |          S  remove_newlines).0elems     r"   
<listcomp>z#remove_newlines.<locals>.<listcomp>F   s     444$%%444    c                 4    i | ]\  }}|t          |          S r(   r)   r+   kvs      r"   
<dictcomp>z#remove_newlines.<locals>.<dictcomp>H   s&    >>>&1a?1%%>>>r.   )
isinstancestrreplacelistdictitems)r$   s    r"   r*   r*   @   s     !S yyr"""	At		 44!4444	At		 >>AGGII>>>>r.   c                   `    e Zd ZdZ	 	 	 	 ddeeef         dedededef
d	Z	d
e
e         fdZdS )NotebookLoaderz'Load `Jupyter notebook` (.ipynb) files.F
   pathr
   r   remove_newliner   c                 L    || _         || _        || _        || _        || _        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   r>   r   )selfr=   r
   r   r>   r   s         r"   __init__zNotebookLoader.__init__P   s.    * .!2,"r.   r   c                     t           j                  }t          |d          5 }t          j        |          }ddd           n# 1 swxY w Y   d |d         D             } j        r"t          t          t          |                    }d	                    t          t           fd|                              }dt          |          i}t          ||	          gS )
zLoad documents.utf8)encodingNc                 J    g | ] }d  |                                 D             !S )c                 "    i | ]\  }}|d v 	||S ))r   r   r   r(   r0   s      r"   r3   z2NotebookLoader.load.<locals>.<listcomp>.<dictcomp>u   s)    XXXfq!5W0W0WQ0W0W0Wr.   )r9   )r+   r	   s     r"   r-   z'NotebookLoader.load.<locals>.<listcomp>t   s?     
 
 
 YX

XXX
 
 
r.   cellsr   c                 F    t          | j        j        j                  S )N)r#   r
   r   r   )r$   rA   s    r"   <lambda>z%NotebookLoader.load.<locals>.<lambda>   s#    /4/1G  r.   r   )page_contentmetadata)r   r@   openjsonloadr>   r7   mapr*   joinr5   r   )rA   pfdfiltered_datar   rL   s   `      r"   rO   zNotebookLoader.loadk   s;      !f%%% 		!A	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 
'

 
 

  	F _m!D!DEEMww    "	  	
 	
 c!ff%dX>>>??s   AAAN)Fr<   FF)__name__
__module____qualname____doc__r   r5   r   boolintrB   r   r   rO   r(   r.   r"   r;   r;   M   s        11
 !&!#$# #CI# # 	#
 # # # # #6@	h@ @ @ @ @ @r.   r;   )rY   rN   pathlibr   typingr   r   r   langchain_core.documentsr   )langchain_community.document_loaders.baser   r8   rZ   r[   r5   r#   r*   r;   r(   r.   r"   <module>r`      s   " "        # # # # # # # # # # - - - - - - @ @ @ @ @ @1
1!%1:=1JN11 1 1 1h
s 
s 
 
 
 
<@ <@ <@ <@ <@Z <@ <@ <@ <@ <@r.   