def notebook_to_md(nbfile, output_dir): import nbformat, nbconvert, os from collections import OrderedDict nb = nbformat.read(nbfile, as_version=nbformat.NO_CONVERT) dir_name, nb_name = os.path.split(nbfile) base_nb_name = os.path.splitext(nb_name)[0] exporter = nbconvert.get_exporter("markdown") resources = { 'output_files_dir': base_nb_name + "_files", "build_dir": output_dir } mdtext, resources = nbconvert.export(exporter, nb=nb, resources=resources) # Handle metadata specific to docs.microsoft.com meta = nb["metadata"].get("ms_docs_meta", {}) docs_meta = OrderedDict() for key in [ 'title', 'description', 'services', 'author', 'manager', 'ms.service', 'ms.technology', 'ms.topic', 'ms.date', 'ms.author' ]: if key in meta: docs_meta[key] = meta[key] if len(docs_meta) > 0: docs_meta_md = '\n'.join(['---'] + [': '.join(kv) for kv in docs_meta.items()] + ['---']) mdtext = docs_meta_md + '\n' + mdtext writer = nbconvert.writers.FilesWriter() writer.write(mdtext, resources, base_nb_name)
def export(cls, format, source, **kwargs): kwargs["preprocessors"] = kwargs.get("preprocessors", []) kwargs["preprocessors"].append( cls(target=kwargs.pop("target", cls.target.default_value))) str, dict = nbconvert.get_exporter(format)( **kwargs).from_filename(source) return str
async def render_execution_context(exporter_type: str, config: dict): # For LaTeX exporters, we want to use the bibliography template, which needs to be written to a file if issubclass(nbconvert.get_exporter(exporter_type), nbconvert.LatexExporter): with TEMPLATE_PATH_FACTORY() as path: config['Exporter']['template_file'] = str(path) yield else: yield
def create_exporter(exporter_type: str, config: dict = None) -> nbconvert.Exporter: if config is None: config = {} # Configure exporter to strip bibliography c = Config(**config) # Create exporter return nbconvert.get_exporter(exporter_type)(config=c)
def convert_to_files(names, to_format): target_dir = os.path.join("static-files", to_format) for folder in folders: if not os.path.exists(os.path.join(target_dir, folder)): os.makedirs(os.path.join(target_dir, folder)) for file_name in names: p = nbconvert.export(nbconvert.get_exporter(to_format), file_name) with open(os.path.join(target_dir, file_name[:-6] + p[1]["output_extension"]), 'w') as f: f.write(p[0].encode("utf-8")) print file_name
def _from_ipynb(path_to_nb, extension, nbconvert_exporter_name): if nbconvert_exporter_name is not None: exporter = nbconvert.get_exporter(nbconvert_exporter_name) else: try: exporter = nbconvert.get_exporter(extension.replace('.', '')) except ValueError: raise TaskBuildError('Could not determine nbconvert exporter ' 'either specify in the path extension ' 'or pass a valid exporter name in ' 'the NotebookRunner constructor, ' 'valid expoers are: {}'.format( nbconvert.get_export_names())) path = Path(path_to_nb) nb = nbformat.v4.reads(path.read_text()) content, _ = nbconvert.export(exporter, nb, exclude_input=True) path.write_text(content) return content
def convert_to_files(names, to_format): target_dir = os.path.join("static-files", to_format) for folder in folders: if not os.path.exists(os.path.join(target_dir, folder)): os.makedirs(os.path.join(target_dir, folder)) for file_name in names: p = nbconvert.export(nbconvert.get_exporter(to_format), file_name) with open( os.path.join(target_dir, file_name[:-6] + p[1]["output_extension"]), 'w') as f: f.write(p[0].encode("utf-8")) print file_name
def _get_exporter(exporter_name, path_to_output): """ Get function to convert notebook to another format using nbconvert, first. In some cases, the exporter name matches the file extension (e.g html) but other times it doesn't (e.g. slides), use `nbconvert.get_export_names()` to get available exporter_names Returns None if passed exported name is 'ipynb', raises ValueError if an exporter can't be located """ extension2exporter_name = {'md': 'markdown'} # sometimes extension does not match with the exporter name, fix # if needed if exporter_name in extension2exporter_name: exporter_name = extension2exporter_name[exporter_name] if exporter_name == 'ipynb': exporter = None else: try: exporter = nbconvert.get_exporter(exporter_name) # nbconvert 5.6.1 raises ValueError, beginning in version 6, # it raises ExporterNameError. However the exception is defined # since 5.6.1 so we can safely import it except (ValueError, ExporterNameError): example_dict = { 'source': 'script.ipynb', 'product': { 'nb': 'output.ipynb', 'other': path_to_output } } raise ValueError( 'Could not find nbconvert exporter ' 'with name "{}". ' 'Change the extension ' 'or pass a valid "nbconvert_exporter_name" ' 'value. Valid exporters are: {}.\n\nIf "{}" ' 'is not intended to be the output noteboook, ' 'register multiple products and identify the ' 'output notebooks with "nb". Example: {}'.format( exporter_name, nbconvert.get_export_names(), path_to_output, example_dict, )) return exporter
def notebook_to_md(nbfile, output_dir): import nbformat, nbconvert, os, shutil from collections import OrderedDict from base64 import b64encode nb = nbformat.read(nbfile, as_version=nbformat.NO_CONVERT) dir_name, nb_name = os.path.split(nbfile) base_nb_name = os.path.splitext(nb_name)[0] exporter = nbconvert.get_exporter("markdown") resources = { 'output_files_dir': base_nb_name + "_files", "build_dir": output_dir } mdtext, resources = nbconvert.export(exporter, nb=nb, resources=resources) # Handle metadata specific to docs.microsoft.com meta = nb["metadata"].get("ms_docs_meta", {}) docs_meta = OrderedDict() for key in [ 'title', 'description', 'services', 'author', 'manager', 'ms.service', 'ms.technology', 'ms.topic', 'ms.date', 'ms.author' ]: if key in meta: docs_meta[key] = meta[key] if len(docs_meta) > 0: docs_meta_md = '\n'.join(['---'] + [': '.join(kv) for kv in docs_meta.items()] + ['---']) mdtext = docs_meta_md + '\n' + mdtext # Replace images containing paths to file with base64-encoded inline images for k, v in resources["outputs"].items(): ext = os.path.splitext(k)[-1][1:] if ext in ("png", "jpg", "svg"): b64 = b64encode(v) v2 = "data:{0};base64,{1}".format(ext, b64.decode("ascii")) mdtext = mdtext.replace(k, v2) writer = nbconvert.writers.FilesWriter() writer.write(mdtext, resources, base_nb_name) shutil.rmtree(resources["output_files_dir"], ignore_errors=True) return mdtext, resources
def _get_exporter(exporter_name, path_to_output): """ Get function to convert notebook to another format using nbconvert, first. In some cases, the exporter name matches the file extension (e.g html) but other times it doesn't (e.g. slides), use `nbconvert.get_export_names()` to get available exporter_names Returns None if passed exported name is 'ipynb', raises ValueError if an exporter can't be located """ extension2exporter_name = {'md': 'markdown'} # sometimes extension does not match with the exporter name, fix # if needed if exporter_name in extension2exporter_name: exporter_name = extension2exporter_name[exporter_name] if exporter_name == 'ipynb': exporter = None else: try: exporter = nbconvert.get_exporter(exporter_name) # nbconvert 5.6.1 raises ValueError, beginning in version 6, # it raises ExporterNameError. However the exception is defined # since 5.6.1 so we can safely import it except (ValueError, ExporterNameError): raise ValueError('Could not find nbconvert exporter ' 'with name "{}". ' 'Either change the extension ' 'or pass a valid "nbconvert_exporter_name" ' 'value. Valid exporters are: {}. If "{}" ' 'is not intended to be the output noteboook, ' 'pass a dictionary with "nb" as the key ' 'for the output notebook, and other ' 'keys for the rest of the outputs'.format( exporter_name, nbconvert.get_export_names(), path_to_output, )) return exporter
def run( self, path: str = None, parameters: dict = None, output_format: str = None, exporter_kwargs: dict = None, ) -> str: """ Run a Jupyter notebook and output as HTML, notebook, or other formats. Args: - path (string, optional): path to fetch the notebook from; can also be a cloud storage path - parameters (dict, optional): dictionary of parameters to use for the notebook - output_format (str, optional): Notebook output format, should be a valid nbconvert Exporter name. 'json' is treated as 'notebook'. Valid exporter names: asciidoc, custom, html, latex, markdown, notebook, pdf, python, rst, script, slides, webpdf. (default: notebook) - exporter_kwargs (dict, optional): The arguments used for initializing the exporter. """ nb: nbformat.NotebookNode = pm.execute_notebook( path, "-", parameters=parameters, kernel_name=self.kernel_name, log_output=self.log_output, ) if output_format == "json": output_format = "notebook" if exporter_kwargs is None: exporter_kwargs = {} exporter = nbconvert.get_exporter(output_format) body, resources = nbconvert.export(exporter, nb, **exporter_kwargs) return body
def __getattr__(self, name): """Dynamically generate ``.to_...()`` converter methods with ``nbconvert.export_...()`` delegation. """ if not name.startswith('to_'): raise AttributeError("%s instance has no attribute %s" % (type(self), repr(name))) check_requirements() # import on demand to be not required for module import import nbconvert try: exportercls = nbconvert.get_exporter(name.split('_', 1)[1]) except ValueError as e: raise AttributeError( "%s instance has no converter method %s (%s: %s)" % (type(self), repr(name), type(e).__name__, e)) def method(): result = exportercls().from_filename(self) return result[0] # only the actual text method.__name__ = name return method
def run(bytes_in): myzipfile = zipfile.ZipFile(bytes_in) with TemporaryDirectory() as temp_dir: """Automatically deletes temp_dir if an error occurs""" print('\nCreate temporary folder: ', temp_dir) out_formats = ['latex', 'html', 'slides', 'rst', 'markdown'] out_formats_ext = ['tex', 'html', 'slides.html', 'rst', 'md'] # find nam files: all_nam_files = [] for f in myzipfile.infolist(): if f.is_dir(): # folder continue elif f.filename[0] == '_': # private continue elif os.path.basename(f.filename)[0] == '.': # private continue elif f.filename.lower()[-4:] == '.nam': all_nam_files.append(f) else: pass assert len( all_nam_files ) != 0, "The input zip does not contain a .nam nor a .NAM file." assert len( all_nam_files) == 1, "The zip can only contain a single name file." + \ "The nam files in the zip are: {0}".format(all_nam_files) # directory name nam file nam_dir = os.path.join(os.path.dirname(all_nam_files[0].filename), '') for f in myzipfile.infolist(): if f.is_dir(): # folder continue elif f.filename[0] == '_': # private continue elif os.path.basename(f.filename)[0] == '.': # private continue elif f.filename[:len(nam_dir)] == nam_dir: old_name = f.filename f.filename = f.filename[len(nam_dir):] new_name = f.filename s = 'Copying {0:s} to {1:s}'.format(f.filename, temp_dir) print(s) myzipfile.extract(f, temp_dir) # location to nam file. Always on top of directory nam_fp = os.path.join(temp_dir, os.path.basename(all_nam_files[0].filename)) assert os.path.isfile(nam_fp), f'{nam_fp} does not exist' mp = Model(nam_fp) nb = mp.script_model2nb(use_yapf=False) buff = io.BytesIO() zip_out = zipfile.ZipFile(buff, 'w') ipynb_buff = io.StringIO(nbformat.writes(nb)) zip_out.writestr('test.ipynb', ipynb_buff.getvalue()) for out_formats_item, out_formats_ext_item in zip( out_formats, out_formats_ext): ipynb_buff.seek(0) zip_out.writestr( 'test.' + out_formats_ext_item, nbconvert.export(nbconvert.get_exporter(out_formats_item), ipynb_buff)[0]) ipynb_buff.close() zip_out.close() print('\nContent of the zipfile:') zip_out.printdir() return buff
try: from flopymetascript.model import Model import io import nbformat import zipfile import nbconvert import flopy p_list = list(flopy.seawat.Seawat().mfnam_packages.keys()) mp = Model(add_pack=p_list) # with description nb = mp.script_model2nb(use_yapf=False) ipynb_buff = io.StringIO(nbformat.writes(nb)) s = nbconvert.export(nbconvert.get_exporter('markdown'), ipynb_buff)[0] s = s.replace('\n\n```', '\n\n```python') p_list_order = list(mp.packages.keys()) toc = [ '* [' + i + '](#' + i.replace('.', '') + ')\n' for i in p_list_order ] toc = ''.join(toc) with open('wiki_default_parameters.md', 'w') as f: f.write(toc) f.write(s) ipynb_buff.close()
import setuptools, datetime, nbconvert from pathlib import Path name = "futurewarningtv" __version__ = None here = Path(__file__).parent setup_args = dict( name=name, version=datetime.datetime.now().isoformat().rpartition(':')[0].replace( '-', '.').replace('T', '.').replace(':', '.'), author="tonyfast", url="https://github.com/futurewarningtv/gists", long_description=nbconvert.get_exporter('markdown')().from_filename( Path('readme.ipynb'))[0], long_description_content_type='text/markdown', python_requires=">=3.7", license="BSD-3-Clause", setup_requires=['pytest-runner'], tests_require=['pytest', "hypothesis", 'nbval'], install_requires=list(x for x in set( filter( bool, map( str.strip, '\n'.join(x.read_text() for x in Path().rglob( '**/requirements.txt')).splitlines()))) if x not in "tonyfast".split()), include_package_data=True, packages=setuptools.find_packages(), entry_points={
notebook, pdf, python, rst, script, slides, webpdf. (default: notebook) - exporter_kwargs (dict, optional): The arguments used for initializing the exporter. ======= - output_format (str, optional): Notebook output format. Currently supported: json, html (default: json) >>>>>>> prefect clone """ nb: nbformat.NotebookNode = pm.execute_notebook( path, "-", parameters=parameters, kernel_name=self.kernel_name ) if output_format == "json": <<<<<<< HEAD output_format = "notebook" if exporter_kwargs is None: exporter_kwargs = {} exporter = nbconvert.get_exporter(output_format) body, resources = nbconvert.export(exporter, nb, **exporter_kwargs) return body ======= return nbformat.writes(nb) if output_format == "html": html_exporter = nbconvert.HTMLExporter() (body, resources) = html_exporter.from_notebook_node(nb) return body raise NotImplementedError("Notebook output %s not supported", output_format) >>>>>>> prefect clone