From 4909a7d5f54b2b773479b7a9300fcd2be50ebeca Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20Wobst?= Date: Thu, 30 Apr 2015 20:37:01 +0000 Subject: [PATCH] update mkipynb to notebook version 4 git-svn-id: http://svn.code.sf.net/p/pyx/code/trunk/pyx@3645 a4f5e268-e194-4f32-bce1-d30804cbbcc5 --- www/mkipynb.py | 57 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 29 deletions(-) rewrite www/mkipynb.py (61%) diff --git a/www/mkipynb.py b/www/mkipynb.py dissimilarity index 61% index 0912ed59..3861b9a9 100644 --- a/www/mkipynb.py +++ b/www/mkipynb.py @@ -1,29 +1,28 @@ -import base64, os, re, sys -from IPython.nbformat.current import reads, write, new_output - -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 = "![bend](http://pyx.sourceforge.net/bend.png)" -description = re.sub(bendpattern, lambda m: "![bend](http://pyx.sourceforge.net/bend.png)"*(m.end()-m.start()), description) -code = open("{}.py".format(filename), encoding="utf-8").read() -code = re.sub('\.writeEPSfile\(("[a-z]+")?\)\n.*writePDFfile\(("[a-z]+")?\)', "", code) -input = """# -{title} -# -{code} -# -{description} -# -""".format(title=title, code=code, description=description) - -nb = reads(input, "py") -codecell, = [cell for ws in nb.worksheets for cell in ws.cells if cell.cell_type == "code"] -codecell.outputs.append(new_output(output_type="pyout", - output_png=base64.encodebytes(open("{}.png".format(filename), "rb").read()))) -write(nb, open("{}.ipynb".format(filename), "w"), "ipynb") +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 = '' +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) + +print(description) + +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)) -- 2.11.4.GIT