[docs]defconcatenate_cells(cell:dict,include_outputs:bool,max_output_length:int,traceback:bool)->str:"""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=cell["cell_type"]source=cell["source"]ifinclude_outputs:try:output=cell["outputs"]exceptKeyError:passifinclude_outputsandcell_type=="code"andoutput:if"ename"inoutput[0].keys():error_name=output[0]["ename"]error_value=output[0]["evalue"]iftraceback:traceback=output[0]["traceback"]return(f"'{cell_type}' cell: '{source}'\n, gives error '{error_name}',"f" with description '{error_value}'\n"f"and traceback '{traceback}'\n\n")else:return(f"'{cell_type}' cell: '{source}'\n, gives error '{error_name}',"f"with description '{error_value}'\n\n")elifoutput[0]["output_type"]=="stream":output=output[0]["text"]min_output=min(max_output_length,len(output))return(f"'{cell_type}' cell: '{source}'\n with "f"output: '{output[:min_output]}'\n\n")else:returnf"'{cell_type}' cell: '{source}'\n\n"return""
[docs]defremove_newlines(x:Any)->Any:"""Recursively remove newlines, no matter the data structure they are stored in."""ifisinstance(x,str):returnx.replace("\n","")elifisinstance(x,list):return[remove_newlines(elem)foreleminx]elifisinstance(x,dict):return{k:remove_newlines(v)for(k,v)inx.items()}else:returnx
[docs]def__init__(self,path:Union[str,Path],include_outputs:bool=False,max_output_length:int=10,remove_newline:bool=False,traceback:bool=False,):"""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. """self.file_path=pathself.include_outputs=include_outputsself.max_output_length=max_output_lengthself.remove_newline=remove_newlineself.traceback=traceback