def convert_files(self, group): """ Converts all notebooks and python scripts into :epkg:`HTML` for a group. @param group group name @return list of new files """ names = list(self.enumerate_group_files(group)) files = [] for name in names: if "attachments" not in name: continue ext = os.path.splitext(name)[-1] if ext == ".ipynb": self.fLOG( "[ProjectsRepository.convert_files] convert '{0}'".format( name)) out = name + ".html" if os.path.exists(out): warnings.warn( "[convert_files] overwriting '{0}'".format(out)) try: upgrade_notebook(name) nb2html(name, out, exc=False) files.append(out) except Exception as e: warnings.warn( "unable to convert a notebook '{0}' because of {1}". format(name, e)) elif ext == ".py": self.fLOG( "[ProjectsRepository.convert_files] convert '{0}'".format( name)) out = name + ".html" if os.path.exists(out): warnings.warn( "[convert_files] overwriting '{0}'".format(out)) try: py_to_html_file(name, out, False, title=os.path.relpath( name, self.get_group_location(group))) files.append(out) except Exception: # the syntax of the python file might be wrong warnings.warn( "unable to convert File \"{0}\"".format(name)) return files
def test_convert_notebooks(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fold = os.path.abspath(os.path.dirname(__file__)) fold2 = os.path.normpath( os.path.join(fold, "..", "..", "_doc", "notebooks")) for nbf in explore_folder_iterfile(fold2, pattern=".*[.]ipynb"): t = upgrade_notebook(nbf) if t: fLOG("modified", nbf) fold2 = os.path.normpath(os.path.join(fold, "..", "..", "_unittests")) for nbf in explore_folder_iterfile(fold2, pattern=".*[.]ipynb"): t = upgrade_notebook(nbf) if t: fLOG("modified", nbf)
def test_convert_notebooks(self): fold = os.path.abspath(os.path.dirname(__file__)) fold2 = os.path.normpath( os.path.join(fold, "..", "..", "_doc", "notebooks")) for nbf in explore_folder_iterfile(fold2, pattern=".*[.]ipynb"): t = upgrade_notebook(nbf) if t: fLOG("modified", nbf) # remove numbers remove_execution_number(nbf, nbf) fold2 = os.path.normpath(os.path.join(fold, "..", "..", "_unittests")) for nbf in explore_folder_iterfile(fold2, pattern=".*[.]ipynb"): t = upgrade_notebook(nbf) if t: fLOG("modified", nbf)
def test_convert_notebooks(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fold = os.path.abspath(os.path.dirname(__file__)) fold2 = os.path.normpath( os.path.join(fold, "..", "..", "_doc", "notebooks")) for nbf in explore_folder_iterfile(fold2, pattern=".*[.]ipynb"): t = upgrade_notebook(nbf) if t: fLOG("modified", nbf) fold2 = os.path.normpath(os.path.join(fold, "..", "..", "_unittests")) for nbf in explore_folder_iterfile(fold2, pattern=".*[.]ipynb"): t = upgrade_notebook(nbf) if t: fLOG("modified", nbf)
def convert_files(self, group): """ Converts all notebooks and python scripts into :epkg:`HTML` for a group. @param group group name @return list of new files """ names = list(self.enumerate_group_files(group)) files = [] for name in names: if "attachments" not in name: continue ext = os.path.splitext(name)[-1] if ext == ".ipynb": self.fLOG( "[ProjectsRepository.convert_files] convert '{0}'".format(name)) out = name + ".html" if os.path.exists(out): warnings.warn( "[convert_files] overwriting '{0}'".format(out)) try: upgrade_notebook(name) nb2html(name, out, exc=False) files.append(out) except Exception as e: warnings.warn( "unable to convert a notebook '{0}' because of {1}".format(name, e)) elif ext == ".py": self.fLOG( "[ProjectsRepository.convert_files] convert '{0}'".format(name)) out = name + ".html" if os.path.exists(out): warnings.warn( "[convert_files] overwriting '{0}'".format(out)) try: py_to_html_file(name, out, False, title=os.path.relpath( name, self.get_group_location(group))) files.append(out) except Exception: # the syntax of the python file might be wrong warnings.warn( "unable to convert File \"{0}\"".format(name)) return files
def test_convert_notebooks(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fold = os.path.abspath(os.path.dirname(__file__)) fold2 = os.path.normpath( os.path.join(fold, "..", "..", "_doc", "notebooks")) for nbf in explore_folder_iterfile(fold2, pattern=".*[.]ipynb"): t = upgrade_notebook(nbf) if t: fLOG("modified", nbf) # remove numbers remove_execution_number(nbf, nbf) fold2 = os.path.normpath(os.path.join(fold, "..", "..", "_unittests")) for nbf in explore_folder_iterfile(fold2, pattern=".*[.]ipynb"): if "site-packages" in nbf: continue t = upgrade_notebook(nbf) if t: fLOG("modified", nbf)
def test_notebook_find(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") path = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.normpath(os.path.join(path, "notebooks_find")) nbs = [os.path.join(fold, _) for _ in os.listdir(fold) if ".ipynb" in _] formats = ["slides", "rst", "present", "ipynb", "html", "python", "pdf"] if sys.platform.startswith("win"): formats.append("docx") temp = get_temp_folder(__file__, "temp_nb_find") if is_travis_or_appveyor() in ('travis', 'appveyor'): return nbs2 = [] for r in nbs: r2 = os.path.join(temp, os.path.split(r)[-1]) shutil.copy(r, r2) nbs2.append(r2) r = upgrade_notebook(r2) fLOG("change", r) nblinks = {('reftag', 'ipynb'): 'http://something', ('reftag', 'python'): 'http://something'} res = process_notebooks( nbs2, temp, temp, formats=formats, exc=False, nblinks=nblinks) fLOG("*****", len(res)) for _ in res: fLOG(_) self.assertTrue(os.path.exists(_[0])) check = os.path.join(temp, "nb_find.tex") with open(check, "r", encoding="utf8") as f: content = f.read() self.assertTrue("\\end{document}" in content)
def test_notebook_find(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") path = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.normpath(os.path.join(path, "notebooks_find")) nbs = [os.path.join(fold, _) for _ in os.listdir(fold) if ".ipynb" in _] formats = ["slides", "rst", "ipynb", "html", "python", "pdf"] if sys.platform.startswith("win"): formats.append("docx") temp = get_temp_folder(__file__, "temp_nb_find") if is_travis_or_appveyor() in ('travis', 'appveyor'): return nbs2 = [] for r in nbs: r2 = os.path.join(temp, os.path.split(r)[-1]) shutil.copy(r, r2) nbs2.append(r2) r = upgrade_notebook(r2) fLOG("change", r) nblinks = {('reftag', 'ipynb'): 'http://something', ('reftag', 'python'): 'http://something'} res = process_notebooks( nbs2, temp, temp, formats=formats, exc=False, nblinks=nblinks) fLOG("*****", len(res)) for _ in res: fLOG(_) self.assertTrue(os.path.exists(_[0])) check = os.path.join(temp, "nb_find.tex") with open(check, "r", encoding="utf8") as f: content = f.read() self.assertTrue("\\end{document}" in content)
def ctest_notebook(self, name): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") path = os.path.abspath(os.path.split(__file__)[0]) fold = os.path.normpath(os.path.join(path, "notebooks2")) nbs = [ os.path.join(fold, _) for _ in os.listdir(fold) if ".ipynb" in _ and name in _ ] formats = ["ipynb", "html", "python", "rst", "pdf"] if sys.platform.startswith("win"): formats.append("docx") temp = get_temp_folder(__file__, "temp_nb_bug2_" + name) if is_travis_or_appveyor() in ('travis', 'appveyor'): return nbs2 = [] for r in nbs: r2 = os.path.join(temp, os.path.split(r)[-1]) shutil.copy(r, r2) nbs2.append(r2) r = upgrade_notebook(r2) fLOG("change", r) res = process_notebooks(nbs2, temp, temp, formats=formats, exc=False) fLOG("*****", len(res)) for _ in res: fLOG(_) self.assertExists(os.path.exists(_[0])) check = os.path.join(temp, name + ".tex") with open(check, "r", encoding="utf8") as f: content = f.read() self.assertIn("\\end{document}", content)