nbexamples.py
104 lines
| 2.3 KiB
| text/x-python
|
PythonLexer
MinRK
|
r18568 | # -*- coding: utf-8 -*- | ||
import os | ||||
from base64 import encodestring | ||||
MinRK
|
r18575 | from ..nbbase import ( | ||
MinRK
|
r18596 | new_code_cell, new_markdown_cell, new_notebook, | ||
MinRK
|
r18573 | new_output, new_raw_cell | ||
MinRK
|
r18568 | ) | ||
# some random base64-encoded *text* | ||||
png = encodestring(os.urandom(5)).decode('ascii') | ||||
jpeg = encodestring(os.urandom(6)).decode('ascii') | ||||
MinRK
|
r18573 | cells = [] | ||
cells.append(new_markdown_cell( | ||||
MinRK
|
r18568 | source='Some NumPy Examples', | ||
)) | ||||
MinRK
|
r18573 | cells.append(new_code_cell( | ||
source='import numpy', | ||||
MinRK
|
r18587 | execution_count=1, | ||
MinRK
|
r18568 | )) | ||
MinRK
|
r18573 | cells.append(new_markdown_cell( | ||
MinRK
|
r18568 | source='A random array', | ||
)) | ||||
MinRK
|
r18573 | cells.append(new_raw_cell( | ||
MinRK
|
r18568 | source='A random array', | ||
)) | ||||
MinRK
|
r18596 | cells.append(new_markdown_cell( | ||
source=u'## My Heading', | ||||
MinRK
|
r18568 | )) | ||
MinRK
|
r18573 | cells.append(new_code_cell( | ||
source='a = numpy.random.rand(100)', | ||||
MinRK
|
r18587 | execution_count=2, | ||
MinRK
|
r18568 | )) | ||
MinRK
|
r18573 | cells.append(new_code_cell( | ||
source='a = 10\nb = 5\n', | ||||
MinRK
|
r18587 | execution_count=3, | ||
MinRK
|
r18568 | )) | ||
MinRK
|
r18573 | cells.append(new_code_cell( | ||
source='a = 10\nb = 5', | ||||
MinRK
|
r18587 | execution_count=4, | ||
MinRK
|
r18568 | )) | ||
MinRK
|
r18573 | cells.append(new_code_cell( | ||
source=u'print "ünîcødé"', | ||||
MinRK
|
r18587 | execution_count=3, | ||
MinRK
|
r18568 | outputs=[new_output( | ||
MinRK
|
r18573 | output_type=u'execute_result', | ||
MinRK
|
r18589 | data={ | ||
MinRK
|
r18573 | 'text/plain': u'<array a>', | ||
'text/html': u'The HTML rep', | ||||
'text/latex': u'$a$', | ||||
'image/png': png, | ||||
'image/jpeg': jpeg, | ||||
'image/svg+xml': u'<svg>', | ||||
'application/json': { | ||||
'key': 'value' | ||||
}, | ||||
'application/javascript': u'var i=0;' | ||||
}, | ||||
MinRK
|
r18587 | execution_count=3 | ||
MinRK
|
r18568 | ),new_output( | ||
output_type=u'display_data', | ||||
MinRK
|
r18589 | data={ | ||
MinRK
|
r18573 | 'text/plain': u'<array a>', | ||
'text/html': u'The HTML rep', | ||||
'text/latex': u'$a$', | ||||
'image/png': png, | ||||
'image/jpeg': jpeg, | ||||
'image/svg+xml': u'<svg>', | ||||
'application/json': { | ||||
'key': 'value' | ||||
}, | ||||
'application/javascript': u'var i=0;' | ||||
}, | ||||
MinRK
|
r18568 | ),new_output( | ||
MinRK
|
r18573 | output_type=u'error', | ||
MinRK
|
r18568 | ename=u'NameError', | ||
evalue=u'NameError was here', | ||||
traceback=[u'frame 0', u'frame 1', u'frame 2'] | ||||
),new_output( | ||||
output_type=u'stream', | ||||
MinRK
|
r18573 | text='foo\rbar\r\n' | ||
MinRK
|
r18568 | ),new_output( | ||
output_type=u'stream', | ||||
MinRK
|
r18576 | name='stderr', | ||
MinRK
|
r18573 | text='\rfoo\rbar\n' | ||
MinRK
|
r18568 | )] | ||
)) | ||||
MinRK
|
r18573 | nb0 = new_notebook(cells=cells, | ||
metadata={ | ||||
'language': 'python', | ||||
} | ||||
MinRK
|
r18568 | ) | ||