Ejemplo n.º 1
0
def notebook_to_md(notebook):
    """Convert a notebook to its Markdown representation, using Pandoc"""
    tmp_file = tempfile.NamedTemporaryFile(delete=False)
    tmp_file.write(ipynb_writes(notebook).encode('utf-8'))
    tmp_file.close()

    pandoc(
        u'--from ipynb --to markdown -s --atx-headers --wrap=preserve --preserve-tabs',
        tmp_file.name, tmp_file.name)

    with open(tmp_file.name, encoding='utf-8') as opened_file:
        text = opened_file.read()

    os.unlink(tmp_file.name)
    return '\n'.join(text.splitlines())
Ejemplo n.º 2
0
def notebook_to_md(notebook):
    """Convert a notebook to its Markdown representation, using Pandoc"""
    raise_if_pandoc_is_not_available()
    tmp_file = tempfile.NamedTemporaryFile(delete=False)
    tmp_file.write(ipynb_writes(notebook).encode("utf-8"))
    tmp_file.close()

    pandoc(
        u"--from ipynb --to markdown -s --atx-headers --wrap=preserve --preserve-tabs",
        tmp_file.name,
        tmp_file.name,
    )

    with open(tmp_file.name, encoding="utf-8") as opened_file:
        text = opened_file.read()

    os.unlink(tmp_file.name)
    return "\n".join(text.splitlines())
Ejemplo n.º 3
0
def convert(nb_files, in_place=True, combine=True):
    """
    Export python or R notebooks, or Jupyter notebooks, to the opposite format
    :param nb_files: one or more notebooks
    :param in_place: should result of conversion be stored in file
    with opposite extension?
    :param combine: should the current outputs of .ipynb file be preserved,
    when a cell with corresponding input is found in .py or .R file?
    :return:
    """
    for nb_file in nb_files:
        file, ext = os.path.splitext(nb_file)
        if ext not in ['.ipynb', '.py', '.R']:
            raise TypeError(
                'File {} is neither a Jupyter (.ipynb) nor a '
                'python script (.py), nor a R script (.R)'.format(nb_file))

        nb = readf(nb_file)
        main_language = get_default_language(nb)
        ext_dest = '.R' if main_language == 'R' else '.py'

        if in_place:
            if ext == '.ipynb':
                nb_dest = file + ext_dest
                print('Jupyter notebook {} being converted to '
                      'source {}'.format(nb_file, nb_dest))
            else:
                msg = ''
                nb_dest = file + '.ipynb'
                if combine and os.path.isfile(nb_dest):
                    try:
                        nb_outputs = readf(nb_dest)
                        combine_inputs_with_outputs(nb, nb_outputs)
                        msg = '(outputs were preserved)'
                    except (IOError, NotJSONError) as error:
                        msg = '(outputs were not preserved: {})'.format(error)
                print('R Markdown {} being converted to '
                      'Jupyter notebook {} {}'.format(nb_file, nb_dest, msg))
            writef(nb, nb_dest)
        else:
            if ext == '.ipynb':
                print(writes(nb, ext_dest))
            else:
                print(ipynb_writes(nb))