nbbase.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. 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 LICENSE, distributed as part of this software.
  14. # -----------------------------------------------------------------------------
  15. # -----------------------------------------------------------------------------
  16. # Imports
  17. # -----------------------------------------------------------------------------
  18. from __future__ import annotations
  19. from nbformat._struct import Struct
  20. # -----------------------------------------------------------------------------
  21. # Code
  22. # -----------------------------------------------------------------------------
  23. class NotebookNode(Struct):
  24. """A notebook node object."""
  25. def from_dict(d):
  26. """Create notebook node(s) from a value."""
  27. if isinstance(d, dict):
  28. newd = NotebookNode()
  29. for k, v in d.items():
  30. newd[k] = from_dict(v)
  31. return newd
  32. if isinstance(d, (tuple, list)):
  33. return [from_dict(i) for i in d]
  34. return d
  35. def new_output(
  36. output_type=None,
  37. output_text=None,
  38. output_png=None,
  39. output_html=None,
  40. output_svg=None,
  41. output_latex=None,
  42. output_json=None,
  43. output_javascript=None,
  44. output_jpeg=None,
  45. prompt_number=None,
  46. etype=None,
  47. evalue=None,
  48. traceback=None,
  49. ):
  50. """Create a new code cell with input and output"""
  51. output = NotebookNode()
  52. if output_type is not None:
  53. output.output_type = str(output_type)
  54. if output_type != "pyerr":
  55. if output_text is not None:
  56. output.text = str(output_text)
  57. if output_png is not None:
  58. output.png = bytes(output_png)
  59. if output_jpeg is not None:
  60. output.jpeg = bytes(output_jpeg)
  61. if output_html is not None:
  62. output.html = str(output_html)
  63. if output_svg is not None:
  64. output.svg = str(output_svg)
  65. if output_latex is not None:
  66. output.latex = str(output_latex)
  67. if output_json is not None:
  68. output.json = str(output_json)
  69. if output_javascript is not None:
  70. output.javascript = str(output_javascript)
  71. if output_type == "pyout" and prompt_number is not None:
  72. output.prompt_number = int(prompt_number)
  73. if output_type == "pyerr":
  74. if etype is not None:
  75. output.etype = str(etype)
  76. if evalue is not None:
  77. output.evalue = str(evalue)
  78. if traceback is not None:
  79. output.traceback = [str(frame) for frame in list(traceback)]
  80. return output
  81. def new_code_cell(
  82. input=None,
  83. prompt_number=None,
  84. outputs=None,
  85. language="python",
  86. collapsed=False,
  87. ):
  88. """Create a new code cell with input and output"""
  89. cell = NotebookNode()
  90. cell.cell_type = "code"
  91. if language is not None:
  92. cell.language = str(language)
  93. if input is not None:
  94. cell.input = str(input)
  95. if prompt_number is not None:
  96. cell.prompt_number = int(prompt_number)
  97. if outputs is None:
  98. cell.outputs = []
  99. else:
  100. cell.outputs = outputs
  101. if collapsed is not None:
  102. cell.collapsed = bool(collapsed)
  103. return cell
  104. def new_text_cell(cell_type, source=None, rendered=None):
  105. """Create a new text cell."""
  106. cell = NotebookNode()
  107. if source is not None:
  108. cell.source = str(source)
  109. if rendered is not None:
  110. cell.rendered = str(rendered)
  111. cell.cell_type = cell_type
  112. return cell
  113. def new_worksheet(name=None, cells=None):
  114. """Create a worksheet by name with with a list of cells."""
  115. ws = NotebookNode()
  116. if name is not None:
  117. ws.name = str(name)
  118. if cells is None:
  119. ws.cells = []
  120. else:
  121. ws.cells = list(cells)
  122. return ws
  123. def new_notebook(metadata=None, worksheets=None):
  124. """Create a notebook by name, id and a list of worksheets."""
  125. nb = NotebookNode()
  126. nb.nbformat = 2
  127. if worksheets is None:
  128. nb.worksheets = []
  129. else:
  130. nb.worksheets = list(worksheets)
  131. if metadata is None:
  132. nb.metadata = new_metadata()
  133. else:
  134. nb.metadata = NotebookNode(metadata)
  135. return nb
  136. def new_metadata(
  137. name=None,
  138. authors=None,
  139. license=None,
  140. created=None,
  141. modified=None,
  142. gistid=None,
  143. ):
  144. """Create a new metadata node."""
  145. metadata = NotebookNode()
  146. if name is not None:
  147. metadata.name = str(name)
  148. if authors is not None:
  149. metadata.authors = list(authors)
  150. if created is not None:
  151. metadata.created = str(created)
  152. if modified is not None:
  153. metadata.modified = str(modified)
  154. if license is not None:
  155. metadata.license = str(license)
  156. if gistid is not None:
  157. metadata.gistid = str(gistid)
  158. return metadata
  159. def new_author(name=None, email=None, affiliation=None, url=None):
  160. """Create a new author."""
  161. author = NotebookNode()
  162. if name is not None:
  163. author.name = str(name)
  164. if email is not None:
  165. author.email = str(email)
  166. if affiliation is not None:
  167. author.affiliation = str(affiliation)
  168. if url is not None:
  169. author.url = str(url)
  170. return author