コード例 #1
0
ファイル: ipython.py プロジェクト: rock417/nengo
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
ファイル: repo_loader.py プロジェクト: RiccardoNanni/bigbang
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
ファイル: ipython.py プロジェクト: pblouw/blouw-etal-2016
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
ファイル: ipython.py プロジェクト: JolyZhang/nengo
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
ファイル: ipython.py プロジェクト: ustczjr86/nengo
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
ファイル: ipython.py プロジェクト: jepetolee/nengo
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
ファイル: ipython.py プロジェクト: nengo/nengo
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
ファイル: ipython.py プロジェクト: sagarkurandwad/nengo
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
ファイル: repo_loader.py プロジェクト: 00babyseal00/bigbang
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
ファイル: ipybo2py.py プロジェクト: monadyn/experiments
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)