def create_notebook(self, cells, filename, start_notebook): nb = nbf.new_notebook() nb.cells = cells with open(filename, 'w') as f: f.write(nbf.writes(nb)) call(['ipython', 'trust', filename]) if start_notebook: call(['ipython', 'notebook', filename])
def show(self): nb = nbf.new_notebook() nb.cells = self.get_cells() with open('dataset_description.ipynb', 'w') as f: f.write(nbf.writes(nb)) call(['ipython', 'notebook', 'dataset_description.ipynb'])
def _create_notebook(self, cells, start_notebook): nb = nbf.new_notebook() nb.cells = cells with open('dataset_description.ipynb', 'w') as f: f.write(nbf.writes(nb)) if start_notebook:call(['ipython', 'notebook', 'dataset_description.ipynb'])
def _create_notebook(self, cells, start_notebook): nb = nbf.new_notebook() nb.cells = cells with open('dataset_description.ipynb', 'w') as f: f.write(nbf.writes(nb)) if start_notebook: call(['ipython', 'notebook', 'dataset_description.ipynb'])
def py2ipynb_default(input, output): with open(input) as f: code = f.read() code += """ # <markdowncell> # If you can read this, reads_py() is no longer broken! """ nbook = v3.reads_py(code) nbook = v4.upgrade(nbook) # Upgrade v3 to v4 jsonform = v4.writes(nbook) + "\n" with open(output, "w") as f: f.write(jsonform)
def py_to_ipynb(filname): with open(filname+".py",encoding="utf8", errors='ignore') as fpin: text = fpin.read() text += """ <markdowncell> If you can read this, reads_py() is no longer broken! """ nbook = v3.reads_py(text) nbook = v4.upgrade(nbook) # Upgrade v3 to v4 jsonform = v4.writes(nbook) + "\n" with open(filname+".ipynb", "w") as fpout: fpout.write(jsonform)
#Convert feature engg example python file to jupyter .ipynb #https://stackoverflow.com/questions/23292242/converting-to-not-from-ipython-notebook-format from IPython.nbformat import v3, v4 with open("feature_engg_example.py") as fpin: text = fpin.read() nbook = v3.reads_py(text) nbook = v4.upgrade(nbook) # Upgrade v3 to v4 jsonform = v4.writes(nbook) + "\n" with open("03_feature_engg_examples.ipynb", "w") as fpout: fpout.write(jsonform)
import base64, os, re, sys import IPython.nbformat.v4 as nbf filename = os.path.splitext(sys.argv[1])[0] try: title, description = open("{}.txt".format(filename), encoding="utf-8").read().split('\n\n', 1) except IOError: title, description = filename, "" description = description.replace("...", "").replace("'''", "**").replace("''", "*") bendpattern = re.compile("^!+", re.MULTILINE) bendcode = '<img src="http://pyx.sourceforge.net/bend.png" align="left">' description = re.sub(bendpattern, lambda m: bendcode*(m.end()-m.start()), description) code = open("{}.py".format(filename), encoding="utf-8").read() code = re.sub('\.writeEPSfile\(("[a-z]+")?\)\n.*writePDFfile\(("[a-z]+")?\)\n.*writeSVGfile\(("[a-z]+")?\)\n', "", code) nb = nbf.new_notebook() cells = [] cells.append(nbf.new_markdown_cell(source="# " + title)) cells.append(nbf.new_code_cell(source=code, execution_count=1, outputs=[nbf.new_output(output_type=u'execute_result', execution_count=1, data={'image/png': base64.encodebytes(open("{}.png".format(filename), "rb").read()).decode("ascii"), 'image/svg+xml': open("{}.svg".format(filename), "r", encoding="utf-8").read()})])) cells.append(nbf.new_markdown_cell(source=description)) nb = nbf.new_notebook(cells=cells, metadata={'language': 'python'}) open("{}.ipynb".format(filename), "w").write(nbf.writes(nb))
description) code = open("{}.py".format(filename), encoding="utf-8").read() code = re.sub( '\.writeEPSfile\(("[a-z]+")?\)\n.*writePDFfile\(("[a-z]+")?\)\n.*writeSVGfile\(("[a-z]+")?\)\n', "", code) nb = nbf.new_notebook() cells = [] cells.append(nbf.new_markdown_cell(source="# " + title)) cells.append( nbf.new_code_cell(source=code, execution_count=1, outputs=[ nbf.new_output( output_type=u'execute_result', execution_count=1, data={ 'image/png': base64.encodebytes( open("{}.png".format(filename), "rb").read()).decode("ascii"), 'image/svg+xml': open("{}.svg".format(filename), "r", encoding="utf-8").read() }) ])) cells.append(nbf.new_markdown_cell(source=description)) nb = nbf.new_notebook(cells=cells, metadata={'language': 'python'}) open("{}.ipynb".format(filename), "w").write(nbf.writes(nb))