nbexamples.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- coding: utf-8 -*-
  2. import os
  3. from base64 import encodestring
  4. from ..nbbase import (
  5. new_code_cell, new_markdown_cell, new_notebook,
  6. new_output, new_raw_cell
  7. )
  8. # some random base64-encoded *text*
  9. png = encodestring(os.urandom(5)).decode('ascii')
  10. jpeg = encodestring(os.urandom(6)).decode('ascii')
  11. cells = []
  12. cells.append(new_markdown_cell(
  13. source='Some NumPy Examples',
  14. ))
  15. cells.append(new_code_cell(
  16. source='import numpy',
  17. execution_count=1,
  18. ))
  19. cells.append(new_markdown_cell(
  20. source='Cell with attachments',
  21. attachments={
  22. 'attachment1': {
  23. 'text/plain': '\n'.join(['a', 'b', 'c']),
  24. 'application/vnd.stuff+json': ['a', 1, 'x'],
  25. }
  26. }
  27. ))
  28. cells.append(new_raw_cell(
  29. source='A random array',
  30. ))
  31. cells.append(new_markdown_cell(
  32. source=u'## My Heading',
  33. ))
  34. cells.append(new_code_cell(
  35. source='a = numpy.random.rand(100)',
  36. execution_count=2,
  37. ))
  38. cells.append(new_code_cell(
  39. source='a = 10\nb = 5\n',
  40. execution_count=3,
  41. ))
  42. cells.append(new_code_cell(
  43. source='a = 10\nb = 5',
  44. execution_count=4,
  45. ))
  46. cells.append(new_code_cell(
  47. source=u'json_outputs()',
  48. execution_count=12,
  49. outputs=[new_output(
  50. output_type=u'display_data',
  51. data={
  52. 'text/plain': u'<json outputs>',
  53. 'application/json': {
  54. 'key': 'value',
  55. 'x': 5,
  56. 'lis': [1, 2, 'x']
  57. },
  58. 'application/vnd.listofstr+json': ['a', 'b', 'c'],
  59. 'application/vnd.numbers+json': [1, 2, 3],
  60. 'application/vnd.number+json': 42,
  61. 'application/vnd.object+json': {
  62. 'number': 5,
  63. 'array': [1,2],
  64. 'str': 'x'
  65. },
  66. 'application/vnd.string+json': 'ok',
  67. },
  68. )]
  69. ))
  70. cells.append(new_code_cell(
  71. source=u'print "ünîcødé"',
  72. execution_count=3,
  73. outputs=[new_output(
  74. output_type=u'execute_result',
  75. data={
  76. 'text/plain': u'<array a>',
  77. 'text/html': u'The HTML rep',
  78. 'text/latex': u'$a$',
  79. 'image/png': png,
  80. 'image/jpeg': jpeg,
  81. 'image/svg+xml': u'<svg>',
  82. 'application/json': {
  83. 'key': 'value'
  84. },
  85. 'application/javascript': u'var i=0;'
  86. },
  87. execution_count=3
  88. ),new_output(
  89. output_type=u'display_data',
  90. data={
  91. 'text/plain': u'<array a>',
  92. 'text/html': u'The HTML rep',
  93. 'text/latex': u'$a$',
  94. 'image/png': png,
  95. 'image/jpeg': jpeg,
  96. 'image/svg+xml': u'<svg>',
  97. 'application/json': {
  98. 'key': 'value'
  99. },
  100. 'application/javascript': u'var i=0;'
  101. },
  102. ),new_output(
  103. output_type=u'error',
  104. ename=u'NameError',
  105. evalue=u'NameError was here',
  106. traceback=[u'frame 0', u'frame 1', u'frame 2']
  107. ),new_output(
  108. output_type=u'stream',
  109. text='foo\rbar\r\n'
  110. ),new_output(
  111. output_type=u'stream',
  112. name='stderr',
  113. text='\rfoo\rbar\n'
  114. )]
  115. ))
  116. nb0 = new_notebook(cells=cells,
  117. metadata={
  118. 'language': 'python',
  119. }
  120. )