nbbase.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. """The basic dict based notebook format.
  2. The Python representation of a notebook is a nested structure of
  3. dictionary subclasses that support attribute access
  4. (ipython_genutils.ipstruct.Struct). The functions in this module are merely
  5. helpers to build the structs in the right form.
  6. Authors:
  7. * Brian Granger
  8. """
  9. #-----------------------------------------------------------------------------
  10. # Copyright (C) 2008-2011 The IPython Development Team
  11. #
  12. # Distributed under the terms of the BSD License. The full license is in
  13. # the file COPYING, distributed as part of this software.
  14. #-----------------------------------------------------------------------------
  15. #-----------------------------------------------------------------------------
  16. # Imports
  17. #-----------------------------------------------------------------------------
  18. import pprint
  19. import uuid
  20. from ipython_genutils.ipstruct import Struct
  21. from ipython_genutils.py3compat import unicode_type
  22. #-----------------------------------------------------------------------------
  23. # Code
  24. #-----------------------------------------------------------------------------
  25. class NotebookNode(Struct):
  26. pass
  27. def from_dict(d):
  28. if isinstance(d, dict):
  29. newd = NotebookNode()
  30. for k,v in d.items():
  31. newd[k] = from_dict(v)
  32. return newd
  33. elif isinstance(d, (tuple, list)):
  34. return [from_dict(i) for i in d]
  35. else:
  36. return d
  37. def new_output(output_type=None, output_text=None, output_png=None,
  38. output_html=None, output_svg=None, output_latex=None, output_json=None,
  39. output_javascript=None, output_jpeg=None, prompt_number=None,
  40. etype=None, evalue=None, traceback=None):
  41. """Create a new code cell with input and output"""
  42. output = NotebookNode()
  43. if output_type is not None:
  44. output.output_type = unicode_type(output_type)
  45. if output_type != 'pyerr':
  46. if output_text is not None:
  47. output.text = unicode_type(output_text)
  48. if output_png is not None:
  49. output.png = bytes(output_png)
  50. if output_jpeg is not None:
  51. output.jpeg = bytes(output_jpeg)
  52. if output_html is not None:
  53. output.html = unicode_type(output_html)
  54. if output_svg is not None:
  55. output.svg = unicode_type(output_svg)
  56. if output_latex is not None:
  57. output.latex = unicode_type(output_latex)
  58. if output_json is not None:
  59. output.json = unicode_type(output_json)
  60. if output_javascript is not None:
  61. output.javascript = unicode_type(output_javascript)
  62. if output_type == u'pyout':
  63. if prompt_number is not None:
  64. output.prompt_number = int(prompt_number)
  65. if output_type == u'pyerr':
  66. if etype is not None:
  67. output.etype = unicode_type(etype)
  68. if evalue is not None:
  69. output.evalue = unicode_type(evalue)
  70. if traceback is not None:
  71. output.traceback = [unicode_type(frame) for frame in list(traceback)]
  72. return output
  73. def new_code_cell(input=None, prompt_number=None, outputs=None,
  74. language=u'python', collapsed=False):
  75. """Create a new code cell with input and output"""
  76. cell = NotebookNode()
  77. cell.cell_type = u'code'
  78. if language is not None:
  79. cell.language = unicode_type(language)
  80. if input is not None:
  81. cell.input = unicode_type(input)
  82. if prompt_number is not None:
  83. cell.prompt_number = int(prompt_number)
  84. if outputs is None:
  85. cell.outputs = []
  86. else:
  87. cell.outputs = outputs
  88. if collapsed is not None:
  89. cell.collapsed = bool(collapsed)
  90. return cell
  91. def new_text_cell(cell_type, source=None, rendered=None):
  92. """Create a new text cell."""
  93. cell = NotebookNode()
  94. if source is not None:
  95. cell.source = unicode_type(source)
  96. if rendered is not None:
  97. cell.rendered = unicode_type(rendered)
  98. cell.cell_type = cell_type
  99. return cell
  100. def new_worksheet(name=None, cells=None):
  101. """Create a worksheet by name with with a list of cells."""
  102. ws = NotebookNode()
  103. if name is not None:
  104. ws.name = unicode_type(name)
  105. if cells is None:
  106. ws.cells = []
  107. else:
  108. ws.cells = list(cells)
  109. return ws
  110. def new_notebook(metadata=None, worksheets=None):
  111. """Create a notebook by name, id and a list of worksheets."""
  112. nb = NotebookNode()
  113. nb.nbformat = 2
  114. if worksheets is None:
  115. nb.worksheets = []
  116. else:
  117. nb.worksheets = list(worksheets)
  118. if metadata is None:
  119. nb.metadata = new_metadata()
  120. else:
  121. nb.metadata = NotebookNode(metadata)
  122. return nb
  123. def new_metadata(name=None, authors=None, license=None, created=None,
  124. modified=None, gistid=None):
  125. """Create a new metadata node."""
  126. metadata = NotebookNode()
  127. if name is not None:
  128. metadata.name = unicode_type(name)
  129. if authors is not None:
  130. metadata.authors = list(authors)
  131. if created is not None:
  132. metadata.created = unicode_type(created)
  133. if modified is not None:
  134. metadata.modified = unicode_type(modified)
  135. if license is not None:
  136. metadata.license = unicode_type(license)
  137. if gistid is not None:
  138. metadata.gistid = unicode_type(gistid)
  139. return metadata
  140. def new_author(name=None, email=None, affiliation=None, url=None):
  141. """Create a new author."""
  142. author = NotebookNode()
  143. if name is not None:
  144. author.name = unicode_type(name)
  145. if email is not None:
  146. author.email = unicode_type(email)
  147. if affiliation is not None:
  148. author.affiliation = unicode_type(affiliation)
  149. if url is not None:
  150. author.url = unicode_type(url)
  151. return author