##// END OF EJS Templates
Merge pull request #1480 from minrk/npmagic...
Merge pull request #1480 from minrk/npmagic Fix %notebook magic, etc. nbformat unicode tests and fixes. * json.writes always gives unicode, so that `current.writes` can be trusted to give the same interface * setup base TestCase for nbformat tests, to consolidate code, and better test both file formats * add tests for reading/writing to files * allow `name` as kwarg to new_notebook to avoid unnecessary breakage of previous API. * remove fallback to xml, which would hide corrupt notebook files behind a nonsensical 'xml unsupported' message. Closes #1545, #1487.

File last commit:

r6318:0895a9b6
r6479:c4cf9403 merge
Show More
nbexamples.py
147 lines | 2.8 KiB | text/x-python | PythonLexer
MinRK
unicode-related fixes in rwbase, nbformat tests
r6210 # -*- coding: utf-8 -*-
MinRK
fix base64 code in nbformat.v2...
r5175 import os
from base64 import encodestring
Brian E. Granger
Full versioning added to nbformat.
r4406 from ..nbbase import (
Brian E. Granger
Initial draft of more formal notebook format....
r4401 NotebookNode,
Brian E. Granger
Implemented metadata for notebook format.
r4637 new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output,
Brian Granger
Fixing minor issues with nbformat....
r6048 new_metadata, new_author, new_heading_cell, nbformat
Brian E. Granger
Initial draft of more formal notebook format....
r4401 )
MinRK
fix base64 code in nbformat.v2...
r5175 # some random base64-encoded *bytes*
png = encodestring(os.urandom(5))
jpeg = encodestring(os.urandom(6))
Brian E. Granger
Initial draft of more formal notebook format....
r4401
ws = new_worksheet(name='worksheet1')
Brian E. Granger
Markdown cells are now saved and restored in notebooks.
r4511 ws.cells.append(new_text_cell(
u'html',
source='Some NumPy Examples',
rendered='Some NumPy Examples'
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ))
ws.cells.append(new_code_cell(
Brian E. Granger
Updates to basic notebook format....
r4402 input='import numpy',
Brian E. Granger
Added collapsed field to the code cell.
r4533 prompt_number=1,
collapsed=False
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ))
Brian E. Granger
Markdown cells are now saved and restored in notebooks.
r4511 ws.cells.append(new_text_cell(
u'markdown',
Brian E. Granger
New .py notebook format implemented.
r4536 source='A random array',
rendered='A random array'
Brian E. Granger
Markdown cells are now saved and restored in notebooks.
r4511 ))
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 ws.cells.append(new_text_cell(
Brian Granger
Changing rst->plaintext in nbformat.
r6025 u'plaintext',
Brian Granger
Adding rst and heading cells to the notebook format.
r6016 source='A random array',
))
ws.cells.append(new_heading_cell(
u'My Heading',
level=2
))
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ws.cells.append(new_code_cell(
Brian E. Granger
Updates to basic notebook format....
r4402 input='a = numpy.random.rand(100)',
Brian E. Granger
Added collapsed field to the code cell.
r4533 prompt_number=2,
collapsed=True
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ))
MinRK
preserve trailing newlines in ipynb...
r6318 ws.cells.append(new_code_cell(
input='a = 10\nb = 5\n',
prompt_number=3,
))
ws.cells.append(new_code_cell(
input='a = 10\nb = 5',
prompt_number=4,
))
Brian E. Granger
Initial draft of more formal notebook format....
r4401
ws.cells.append(new_code_cell(
MinRK
unicode-related fixes in rwbase, nbformat tests
r6210 input=u'print "ünîcødé"',
Brian E. Granger
Updates to basic notebook format....
r4402 prompt_number=3,
Brian E. Granger
Added collapsed field to the code cell.
r4533 collapsed=False,
Brian E. Granger
Updates to basic notebook format....
r4402 outputs=[new_output(
output_type=u'pyout',
output_text=u'<array a>',
output_html=u'The HTML rep',
output_latex=u'$a$',
MinRK
fix base64 code in nbformat.v2...
r5175 output_png=png,
output_jpeg=jpeg,
Brian E. Granger
Updates to basic notebook format....
r4402 output_svg=u'<svg>',
output_json=u'json data',
Brian E. Granger
Starting to rename text cell to html cell.
r4498 output_javascript=u'var i=0;',
prompt_number=3
Brian E. Granger
Updates to basic notebook format....
r4402 ),new_output(
output_type=u'display_data',
output_text=u'<array a>',
output_html=u'The HTML rep',
output_latex=u'$a$',
MinRK
fix base64 code in nbformat.v2...
r5175 output_png=png,
output_jpeg=jpeg,
Brian E. Granger
Updates to basic notebook format....
r4402 output_svg=u'<svg>',
output_json=u'json data',
Brian E. Granger
Adding tracebacks, evalue and etype to the nbformat and notebook.
r4540 output_javascript=u'var i=0;'
),new_output(
output_type=u'pyerr',
etype=u'NameError',
evalue=u'NameError was here',
traceback=[u'frame 0', u'frame 1', u'frame 2']
Brian E. Granger
Updates to basic notebook format....
r4402 )]
Brian E. Granger
Initial draft of more formal notebook format....
r4401 ))
Brian E. Granger
Implemented metadata for notebook format.
r4637 authors = [new_author(name='Bart Simpson',email='bsimpson@fox.com',
affiliation=u'Fox',url=u'http://www.fox.com')]
md = new_metadata(name=u'My Notebook',license=u'BSD',created=u'8601_goes_here',
modified=u'8601_goes_here',gistid=u'21341231',authors=authors)
Brian E. Granger
Initial draft of more formal notebook format....
r4401 nb0 = new_notebook(
Brian E. Granger
New .py notebook format implemented.
r4536 worksheets=[ws, new_worksheet(name='worksheet2')],
Brian E. Granger
Implemented metadata for notebook format.
r4637 metadata=md
Brian E. Granger
Initial draft of more formal notebook format....
r4401 )
MinRK
unicode-related fixes in rwbase, nbformat tests
r6210 nb0_py = u"""# -*- coding: utf-8 -*-
Brian Granger
Fixing minor issues with nbformat....
r6048 # <nbformat>%i</nbformat>
Brian E. Granger
Full versioning added to nbformat.
r4406
Brian E. Granger
New .py notebook format implemented.
r4536 # <htmlcell>
# Some NumPy Examples
Brian E. Granger
Full versioning added to nbformat.
r4406 # <codecell>
Brian E. Granger
Initial draft of more formal notebook format....
r4401
import numpy
Brian E. Granger
New .py notebook format implemented.
r4536 # <markdowncell>
# A random array
Brian Granger
Changing rst->plaintext in nbformat.
r6025 # <plaintextcell>
Brian Granger
Adding rst and heading cells to the notebook format.
r6016
# A random array
# <headingcell level=2>
# My Heading
Brian E. Granger
Initial draft of more formal notebook format....
r4401 # <codecell>
a = numpy.random.rand(100)
# <codecell>
MinRK
preserve trailing newlines in ipynb...
r6318 a = 10
b = 5
# <codecell>
a = 10
b = 5
# <codecell>
MinRK
unicode-related fixes in rwbase, nbformat tests
r6210 print "ünîcødé"
Brian E. Granger
Full versioning added to nbformat.
r4406
Brian Granger
Fixing minor issues with nbformat....
r6048 """ % nbformat
Brian E. Granger
Initial draft of more formal notebook format....
r4401