def test_notebook_runner_flat2db3(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") notebook = os.path.split(__file__)[-1].replace(".ipynb", "").replace(".py", "")[5:] temp = get_temp_folder(__file__, "temp_" + notebook) nbfile = os.path.join(temp, "..", "..", "..", "_doc", "notebooks", "%s.ipynb" % notebook) if not os.path.exists(nbfile): raise FileNotFoundError(nbfile) addpath = [ os.path.normpath(os.path.join(temp, "..", "..", "..", "src")), os.path.normpath(os.path.join(temp, "..", "..", "..", "..", "pyquickhelper", "src")), os.path.normpath(os.path.join(temp, "..", "..", "..", "..", "pymyinstall", "src")), ] outfile = os.path.join(temp, "out_notebook.ipynb") assert not os.path.exists(outfile) valid = lambda code: 'run_cmd("SQLiteSpy.exe velib_vanves.db3")' not in code if "travis" in sys.executable: return kernel_name = None if "travis" in sys.executable else install_python_kernel_for_unittest("pyensae") out = run_notebook( nbfile, working_dir=temp, outfilename=outfile, additional_path=addpath, valid=valid, fLOG=fLOG, kernel_name=kernel_name, ) fLOG(out) assert os.path.exists(outfile)
def test_notebook_runner(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") temp = get_temp_folder(__file__, "temp_notebook1") nbfile = os.path.join( temp, "..", "..", "..", "_doc", "notebooks", "td2a", "td2a_correction_session_1.ipynb") nbfile = os.path.normpath(nbfile) assert os.path.exists(nbfile) addpath = [ os.path.dirname( pyquickhelper.__file__), os.path.dirname( pyensae.__file__)] addpath = [os.path.normpath(os.path.join(_, "..")) for _ in addpath] outfile = os.path.join(temp, "out_" + os.path.split(nbfile)[-1]) assert not os.path.exists(outfile) stat, out = run_notebook(nbfile, working_dir=temp, outfilename=outfile, additional_path=addpath, valid=lambda code: '%system' not in code) fLOG(out) assert os.path.exists(outfile)
def execute_notebooks(folder, notebooks, filter, clean_function=None, fLOG=noLOG, deepfLOG=noLOG): """ execute a list of notebooks @param folder folder @param notebooks list of notebooks @param filter function which validates the notebooks to test (True means will be tested) @param clean_function cleaning function to apply to the code before running it @param fLOG logging function @param deepfLOG logging function used to run the notebook @return dictionary { notebook_file: (isSuccess, outout) } The signature of function ``filter`` is:: def filter( i, filename) : return True or False """ def valid_cell(cell): if "%system" in cell: return False if "df.plot(...)" in cell: return False if 'df["difference"] = ...' in cell: return False return True addpath = get_additional_paths() kernel_name = None if "travis" in sys.executable else install_python_kernel_for_unittest( "ensae_teaching_cs") results = {} for i, note in enumerate(notebooks): if filter(i, note): fLOG("******", i, os.path.split(note)[-1]) outfile = os.path.join(folder, "out_" + os.path.split(note)[-1]) try: stat, out = run_notebook(note, working_dir=folder, outfilename=outfile, additional_path=addpath, valid=valid_cell, clean_function=clean_function, fLOG=deepfLOG, kernel_name=kernel_name ) if not os.path.exists(outfile): raise FileNotFoundError(outfile) results[note] = (True, stat, out) except Exception as e: results[note] = (False, None, e) return results
def test_notebook_runner_magic_command(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") notebook = os.path.split( __file__)[-1].replace(".ipynb", "").replace(".py", "")[5:] temp = get_temp_folder(__file__, "temp_" + notebook) nbfile = os.path.join( temp, "..", "..", "..", "_doc", "notebooks", "%s.ipynb" % notebook) if not os.path.exists(nbfile): raise FileNotFoundError(nbfile) addpath = [os.path.normpath(os.path.join(temp, "..", "..", "..", "src")), os.path.normpath( os.path.join( temp, "..", "..", "..", "..", "pyquickhelper", "src")), ] kernel_name = None if "travis" in sys.executable else install_python_kernel_for_unittest( "pyensae") outfile = os.path.join(temp, "out_notebook.ipynb") assert not os.path.exists(outfile) out = run_notebook( nbfile, working_dir=temp, outfilename=outfile, additional_path=addpath, kernel_name=kernel_name) fLOG(out) assert os.path.exists(outfile)