Пример #1
0
def export_py(nb, dst_path):
    export = PythonExporter()
    body, resources = export.from_notebook_node(nb)
    if sys.version_info[0] == 2:
        body = unicodedata.normalize('NFKD', body).encode('ascii', 'ignore')
    with open(dst_path, 'w') as f:
        f.write(body)
Пример #2
0
def get_files(filepath):
    """
    Returns a list of the Python files in a directory, and
    converts IPython notebooks into Python source code and
    includes them with the Python files.
    """
    os.chdir(filepath)
    files = []
    for root, dirnames, filenames in os.walk("."):
        for filename in fnmatch.filter(filenames, "*.py"):
            files.append(os.path.join(root, filename))

        for filename in fnmatch.filter(filenames, "*.ipynb"):
            try:
                with open(filename) as fh:
                    nb = nbformat.reads_json(fh.read())
                    export_path = filename.replace(".ipynb", ".py")
                    exporter = PythonExporter()
                    source, meta = exporter.from_notebook_node(nb)

                with open(export_path, "w+") as fh:
                    fh.writelines(source)
                files.append()
            except Exception as e:
                raise RepoLoaderWarning(
                    f"May have issues with JSON encoding. The error message is: {e}"
                )
    return files
Пример #3
0
def export_py(nb, dst_path):
    export = PythonExporter()
    body, resources = export.from_notebook_node(nb)
    if sys.version_info[0] == 2:
        body = unicodedata.normalize('NFKD', body).encode('ascii', 'ignore')
    # We'll remove %matplotlib inline magic, but leave the rest
    body = body.replace("get_ipython().magic(u'matplotlib inline')\n", "")
    body = body.replace("get_ipython().magic('matplotlib inline')\n", "")
    with open(dst_path, 'w') as f:
        f.write(body)
Пример #4
0
def export_py(nb, dest_path=None):
    """Convert notebook to Python script.

    Optionally saves script to dest_path.
    """
    exporter = PythonExporter()
    body, resources = exporter.from_notebook_node(nb)
    if sys.version_info[0] == 2:
        body = unicodedata.normalize('NFKD', body).encode('ascii', 'ignore')
    # We'll remove %matplotlib inline magic, but leave the rest
    body = body.replace("get_ipython().magic(u'matplotlib inline')\n", "")
    body = body.replace("get_ipython().magic('matplotlib inline')\n", "")
    if dest_path is not None:
        with open(dest_path, 'w') as f:
            f.write(body)
    return body
Пример #5
0
def export_py(nb, dest_path=None):
    """Convert notebook to Python script.

    Optionally saves script to dest_path.
    """
    exporter = PythonExporter()
    body, resources = exporter.from_notebook_node(nb)
    # We'll remove %matplotlib inline magic, but leave the rest
    body = body.replace(u"get_ipython().magic(u'matplotlib inline')\n", u"")
    body = body.replace(u"get_ipython().magic('matplotlib inline')\n", u"")
    # Also remove the IPython notebook extension
    body = body.replace(u"get_ipython().magic(u'load_ext nengo.ipynb')\n", u"")
    body = body.replace(u"get_ipython().magic('load_ext nengo.ipynb')\n", u"")
    if dest_path is not None:
        with io.open(dest_path, 'w', encoding='utf-8') as f:
            f.write(body)
    return body
Пример #6
0
def export_py(nb, dest_path=None):
    """Convert notebook to Python script.

    Optionally saves script to dest_path.
    """
    exporter = PythonExporter()
    body, resources = exporter.from_notebook_node(nb)
    # We'll remove %matplotlib inline magic, but leave the rest
    body = body.replace(u"get_ipython().magic(u'matplotlib inline')\n", u"")
    body = body.replace(u"get_ipython().magic('matplotlib inline')\n", u"")
    # Also remove the IPython notebook extension
    body = body.replace(u"get_ipython().magic(u'load_ext nengo.ipynb')\n", u"")
    body = body.replace(u"get_ipython().magic('load_ext nengo.ipynb')\n", u"")
    if dest_path is not None:
        with io.open(dest_path, 'w', encoding='utf-8') as f:
            f.write(body)
    return body
Пример #7
0
def export_py(nb, dest_path=None):
    """Convert notebook to Python script.

    Optionally saves script to dest_path.
    """
    exporter = PythonExporter()
    body, _ = exporter.from_notebook_node(nb)

    # Remove all lines with get_ipython
    while "get_ipython()" in body:
        ind0 = body.find("get_ipython()")
        ind1 = body.find("\n", ind0)
        body = body[:ind0] + body[(ind1 + 1) :]

    if dest_path is not None:
        with io.open(dest_path, "w", encoding="utf-8") as f:
            f.write(body)
    return body
Пример #8
0
def export_py(nb, dest_path=None):
    """Convert notebook to Python script.

    Optionally saves script to dest_path.
    """
    exporter = PythonExporter()
    body, resources = exporter.from_notebook_node(nb)

    # Remove all lines with get_ipython
    while "get_ipython()" in body:
        ind0 = body.find("get_ipython()")
        ind1 = body.find("\n", ind0)
        body = body[:ind0] + body[(ind1 + 1):]

    if "plt" in body:
        body += "\nplt.show()\n"

    if dest_path is not None:
        with io.open(dest_path, 'w', encoding='utf-8') as f:
            f.write(body)
    return body
Пример #9
0
def export_py(nb, dest_path=None):
    """Convert notebook to Python script.

    Optionally saves script to dest_path.
    """
    exporter = PythonExporter()
    body, resources = exporter.from_notebook_node(nb)

    # Remove all lines with get_ipython
    while u"get_ipython()" in body:
        ind0 = body.find(u"get_ipython()")
        ind1 = body.find(u"\n", ind0)
        body = body[:ind0] + body[(ind1 + 1):]

    if u"plt" in body:
        body += u"\nplt.show()\n"

    if dest_path is not None:
        with io.open(dest_path, 'w', encoding='utf-8') as f:
            f.write(body)
    return body
Пример #10
0
def get_files(filepath):
	os.chdir(filepath)
	files = []
	for root, dirnames, filenames in os.walk("."):
		for filename in fnmatch.filter(filenames, '*.py'):
			files.append(os.path.join(root, filename))

		for filename in fnmatch.filter(filenames, '*.ipynb'):
			try:
				with open(filename) as fh:
	    			nb = nbformat.reads_json(fh.read())

				export_path = filename.replace(".ipynb", ".py")
				exporter = PythonExporter()
				source, meta = exporter.from_notebook_node(nb)

				with open(export_path, 'w+') as fh:
					fh.writelines(source)
				files.append()
			except: #may have issues with JSON encoding
				pass
	return files
Пример #11
0
def get_files(filepath):
	os.chdir(filepath)
	files = []
	for root, dirnames, filenames in os.walk("."):
		for filename in fnmatch.filter(filenames, '*.py'):
			files.append(os.path.join(root, filename))

		for filename in fnmatch.filter(filenames, '*.ipynb'):
			try:
				with open(filename) as fh:
                                    nb = nbformat.reads_json(fh.read())

                                    export_path = filename.replace(".ipynb", ".py")
                                    exporter = PythonExporter()
                                    source, meta = exporter.from_notebook_node(nb)

				with open(export_path, 'w+') as fh:
					fh.writelines(source)
				files.append()
			except: #may have issues with JSON encoding
				pass
	return files
Пример #12
0
from IPython.nbformat import current as nbformat
from IPython.nbconvert import PythonExporter

filepath = 'E:\\Chrome-Downloads\\NewProject\\SignatureVerification\\Siamese_Network\\predict.ipynb'
export_path = '.'

with open(filepath) as fh:
    nb = nbformat.reads_json(fh.read())

exporter = PythonExporter()

# source is a tuple of python source code
# meta contains metadata
source, meta = exporter.from_notebook_node(nb)

with open(export_path, 'w+') as fh:
    fh.writelines(source)
Пример #13
0
from IPython.nbformat import current as nbformat
from IPython.nbconvert import PythonExporter

filepath = "Huasong.Shan.HW1.ipynb"  #'HW1_T2.ipynb'
export_path = "Huasong.Shan.HW1.ipynb.py"

with open(filepath) as fh:
    nb = nbformat.reads_json(fh.read())

exporter = PythonExporter()

# source is a tuple of python source code
# meta contains metadata
source, meta = exporter.from_notebook_node(nb)

with open(export_path, "w+") as fh:
    fh.writelines(source)