nbexamples.py
152 lines
| 2.9 KiB
| text/x-python
|
PythonLexer
|
r6210 | # -*- coding: utf-8 -*- | ||
|
r5175 | import os | ||
from base64 import encodestring | ||||
|
r4406 | from ..nbbase import ( | ||
|
r4401 | NotebookNode, | ||
|
r4637 | new_code_cell, new_text_cell, new_worksheet, new_notebook, new_output, | ||
|
r7566 | new_metadata, new_author, new_heading_cell, nbformat, nbformat_minor | ||
|
r4401 | ) | ||
|
r12390 | # some random base64-encoded *text* | ||
png = encodestring(os.urandom(5)).decode('ascii') | ||||
jpeg = encodestring(os.urandom(6)).decode('ascii') | ||||
|
r4401 | |||
|
r18711 | ws = new_worksheet() | ||
|
r4401 | |||
|
r4511 | ws.cells.append(new_text_cell( | ||
u'html', | ||||
source='Some NumPy Examples', | ||||
|
r4401 | )) | ||
ws.cells.append(new_code_cell( | ||||
|
r4402 | input='import numpy', | ||
|
r4533 | prompt_number=1, | ||
collapsed=False | ||||
|
r4401 | )) | ||
|
r4511 | ws.cells.append(new_text_cell( | ||
u'markdown', | ||||
|
r4536 | source='A random array', | ||
|
r4511 | )) | ||
|
r6016 | ws.cells.append(new_text_cell( | ||
|
r6248 | u'raw', | ||
|
r6016 | source='A random array', | ||
)) | ||||
ws.cells.append(new_heading_cell( | ||||
u'My Heading', | ||||
level=2 | ||||
)) | ||||
|
r4401 | ws.cells.append(new_code_cell( | ||
|
r4402 | input='a = numpy.random.rand(100)', | ||
|
r4533 | prompt_number=2, | ||
collapsed=True | ||||
|
r4401 | )) | ||
|
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, | ||||
)) | ||||
|
r4401 | |||
ws.cells.append(new_code_cell( | ||||
|
r6210 | input=u'print "ünîcødé"', | ||
|
r4402 | prompt_number=3, | ||
|
r4533 | collapsed=False, | ||
|
r4402 | outputs=[new_output( | ||
output_type=u'pyout', | ||||
output_text=u'<array a>', | ||||
output_html=u'The HTML rep', | ||||
output_latex=u'$a$', | ||||
|
r5175 | output_png=png, | ||
output_jpeg=jpeg, | ||||
|
r4402 | output_svg=u'<svg>', | ||
|
r18711 | output_json=u'{"json": "data"}', | ||
|
r4498 | output_javascript=u'var i=0;', | ||
prompt_number=3 | ||||
|
r4402 | ),new_output( | ||
output_type=u'display_data', | ||||
output_text=u'<array a>', | ||||
output_html=u'The HTML rep', | ||||
output_latex=u'$a$', | ||||
|
r5175 | output_png=png, | ||
output_jpeg=jpeg, | ||||
|
r4402 | output_svg=u'<svg>', | ||
|
r18711 | output_json=u'{"json": "data"}', | ||
|
r4540 | output_javascript=u'var i=0;' | ||
),new_output( | ||||
output_type=u'pyerr', | ||||
|
r10233 | ename=u'NameError', | ||
|
r4540 | evalue=u'NameError was here', | ||
traceback=[u'frame 0', u'frame 1', u'frame 2'] | ||||
|
r7335 | ),new_output( | ||
output_type=u'stream', | ||||
output_text='foo\rbar\r\n' | ||||
),new_output( | ||||
output_type=u'stream', | ||||
|
r10183 | stream='stderr', | ||
|
r7335 | output_text='\rfoo\rbar\n' | ||
|
r4402 | )] | ||
|
r4401 | )) | ||
|
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) | ||||
|
r4401 | nb0 = new_notebook( | ||
|
r18711 | worksheets=[ws, new_worksheet()], | ||
|
r4637 | metadata=md | ||
|
r4401 | ) | ||
|
r6210 | nb0_py = u"""# -*- coding: utf-8 -*- | ||
|
r7566 | # <nbformat>%i.%i</nbformat> | ||
|
r4406 | |||
|
r4536 | # <htmlcell> | ||
# Some NumPy Examples | ||||
|
r4406 | # <codecell> | ||
|
r4401 | |||
import numpy | ||||
|
r4536 | # <markdowncell> | ||
# A random array | ||||
|
r6248 | # <rawcell> | ||
|
r6016 | |||
# A random array | ||||
# <headingcell level=2> | ||||
# My Heading | ||||
|
r4401 | # <codecell> | ||
a = numpy.random.rand(100) | ||||
# <codecell> | ||||
|
r6318 | a = 10 | ||
b = 5 | ||||
# <codecell> | ||||
a = 10 | ||||
b = 5 | ||||
# <codecell> | ||||
|
r6210 | print "ünîcødé" | ||
|
r4406 | |||
|
r7566 | """ % (nbformat, nbformat_minor) | ||
|
r4401 | |||