nbformat.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. // Notebook format interfaces
  4. // https://nbformat.readthedocs.io/en/latest/format_description.html
  5. // https://github.com/jupyter/nbformat/blob/master/nbformat/v4/nbformat.v4.schema.json
  6. import {
  7. JSONObject, JSONExt
  8. } from '@phosphor/coreutils';
  9. /**
  10. * A namespace for nbformat interfaces.
  11. */
  12. export
  13. namespace nbformat {
  14. /**
  15. * The major version of the notebook format.
  16. */
  17. export
  18. const MAJOR_VERSION: number = 4;
  19. /**
  20. * The minor version of the notebook format.
  21. */
  22. export
  23. const MINOR_VERSION: number = 2;
  24. /**
  25. * The kernelspec metadata.
  26. */
  27. export
  28. interface IKernelspecMetadata extends JSONObject {
  29. name: string;
  30. display_name: string;
  31. }
  32. /**
  33. * The language info metatda
  34. */
  35. export
  36. interface ILanguageInfoMetadata extends JSONObject {
  37. name: string;
  38. codemirror_mode: string | JSONObject;
  39. file_extension: string;
  40. mimetype: string;
  41. pygments_lexer: string;
  42. }
  43. /**
  44. * The default metadata for the notebook.
  45. */
  46. export
  47. interface INotebookMetadata extends JSONObject {
  48. kernelspec: IKernelspecMetadata;
  49. language_info: ILanguageInfoMetadata;
  50. orig_nbformat: number;
  51. }
  52. /**
  53. * The notebook content.
  54. */
  55. export
  56. interface INotebookContent extends JSONObject {
  57. metadata: INotebookMetadata;
  58. nbformat_minor: number;
  59. nbformat: number;
  60. cells: ICell[];
  61. }
  62. /**
  63. * A multiline string.
  64. */
  65. export
  66. type MultilineString = string | string[];
  67. /**
  68. * A mime-type keyed dictionary of data.
  69. */
  70. export
  71. interface IMimeBundle extends JSONObject {
  72. [key: string]: MultilineString | JSONObject;
  73. }
  74. /**
  75. * Media attachments (e.g. inline images).
  76. */
  77. export
  78. interface IAttachments {
  79. [key: string]: IMimeBundle;
  80. }
  81. /**
  82. * The code cell's prompt number. Will be null if the cell has not been run.
  83. */
  84. export
  85. type ExecutionCount = number | null;
  86. /**
  87. * Cell output metadata.
  88. */
  89. export
  90. type OutputMetadata = JSONObject;
  91. /**
  92. * Validate a mime type/value pair.
  93. *
  94. * @param type - The mimetype name.
  95. *
  96. * @param value - The value associated with the type.
  97. *
  98. * @returns Whether the type/value pair are valid.
  99. */
  100. export
  101. function validateMimeValue(type: string, value: MultilineString | JSONObject): boolean {
  102. // Check if "application/json" or "application/foo+json"
  103. const jsonTest = /^application\/(.*?)+\+json$/;
  104. const isJSONType = type === 'application/json' || jsonTest.test(type);
  105. let isString = (x: any) => {
  106. return Object.prototype.toString.call(x) === '[object String]';
  107. };
  108. // If it is an array, make sure if is not a JSON type and it is an
  109. // array of strings.
  110. if (Array.isArray(value)) {
  111. if (isJSONType) {
  112. return false;
  113. }
  114. let valid = true;
  115. (value as string[]).forEach(v => {
  116. if (!isString(v)) {
  117. valid = false;
  118. }
  119. });
  120. return valid;
  121. }
  122. // If it is a string, make sure we are not a JSON type.
  123. if (isString(value)) {
  124. return !isJSONType;
  125. }
  126. // It is not a string, make sure it is a JSON type.
  127. if (!isJSONType) {
  128. return false;
  129. }
  130. // It is a JSON type, make sure it is a valid JSON object.
  131. return JSONExt.isObject(value);
  132. }
  133. /**
  134. * A type which describes the type of cell.
  135. */
  136. export
  137. type CellType = 'code' | 'markdown' | 'raw';
  138. /**
  139. * Cell-level metadata.
  140. */
  141. export
  142. interface IBaseCellMetadata extends JSONObject {
  143. /**
  144. * Whether the cell is trusted.
  145. *
  146. * #### Notes
  147. * This is not strictly part of the nbformat spec, but it is added by
  148. * the contents manager.
  149. *
  150. * See https://jupyter-notebook.readthedocs.io/en/latest/security.html.
  151. */
  152. trusted: boolean;
  153. /**
  154. * The cell's name. If present, must be a non-empty string.
  155. */
  156. name: string;
  157. /**
  158. * The cell's tags. Tags must be unique, and must not contain commas.
  159. */
  160. tags: string[];
  161. }
  162. /**
  163. * The base cell interface.
  164. */
  165. export
  166. interface IBaseCell extends JSONObject {
  167. /**
  168. * String identifying the type of cell.
  169. */
  170. cell_type: string;
  171. /**
  172. * Contents of the cell, represented as an array of lines.
  173. */
  174. source: MultilineString;
  175. /**
  176. * Cell-level metadata.
  177. */
  178. metadata: Partial<ICellMetadata>;
  179. }
  180. /**
  181. * Metadata for the raw cell.
  182. */
  183. export
  184. interface IRawCellMetadata extends IBaseCellMetadata {
  185. /**
  186. * Raw cell metadata format for nbconvert.
  187. */
  188. format: string;
  189. }
  190. /**
  191. * A raw cell.
  192. */
  193. export
  194. interface IRawCell extends IBaseCell {
  195. /**
  196. * String identifying the type of cell.
  197. */
  198. cell_type: 'raw';
  199. /**
  200. * Cell-level metadata.
  201. */
  202. metadata: Partial<IRawCellMetadata>;
  203. /**
  204. * Cell attachments.
  205. */
  206. attachments?: IAttachments;
  207. }
  208. /**
  209. * A markdown cell.
  210. */
  211. export
  212. interface IMarkdownCell extends IBaseCell {
  213. /**
  214. * String identifying the type of cell.
  215. */
  216. cell_type: 'markdown';
  217. /**
  218. * Cell attachments.
  219. */
  220. attachments?: IAttachments;
  221. }
  222. /**
  223. * Metadata for a code cell.
  224. */
  225. export
  226. interface ICodeCellMetadata extends IBaseCellMetadata {
  227. /**
  228. * Whether the cell is collapsed/expanded.
  229. */
  230. collapsed: boolean;
  231. /**
  232. * Whether the cell's output is scrolled, unscrolled, or autoscrolled.
  233. */
  234. scrolled: boolean | 'auto';
  235. }
  236. /**
  237. * A code cell.
  238. */
  239. export
  240. interface ICodeCell extends IBaseCell {
  241. /**
  242. * String identifying the type of cell.
  243. */
  244. cell_type: 'code';
  245. /**
  246. * Cell-level metadata.
  247. */
  248. metadata: Partial<ICodeCellMetadata>;
  249. /**
  250. * Execution, display, or stream outputs.
  251. */
  252. outputs: IOutput[];
  253. /**
  254. * The code cell's prompt number. Will be null if the cell has not been run.
  255. */
  256. execution_count: ExecutionCount;
  257. }
  258. /**
  259. * An unrecognized cell.
  260. */
  261. export
  262. interface IUnrecognizedCell extends IBaseCell { }
  263. /**
  264. * A cell union type.
  265. */
  266. export
  267. type ICell = IRawCell | IMarkdownCell | ICodeCell | IUnrecognizedCell;
  268. /**
  269. * Test whether a cell is a raw cell.
  270. */
  271. export
  272. function isRaw(cell: ICell): cell is IRawCell {
  273. return cell.cell_type === 'raw';
  274. }
  275. /**
  276. * Test whether a cell is a markdown cell.
  277. */
  278. export
  279. function isMarkdown(cell: ICell): cell is IMarkdownCell {
  280. return cell.cell_type === 'markdown';
  281. }
  282. /**
  283. * Test whether a cell is a code cell.
  284. */
  285. export
  286. function isCode(cell: ICell): cell is ICodeCell {
  287. return cell.cell_type === 'code';
  288. }
  289. /**
  290. * A union metadata type.
  291. */
  292. export
  293. type ICellMetadata = IBaseCellMetadata | IRawCellMetadata | ICodeCellMetadata;
  294. /**
  295. * The valid output types.
  296. */
  297. export
  298. type OutputType = 'execute_result' | 'display_data' | 'stream' | 'error';
  299. /**
  300. * The base output type.
  301. */
  302. export
  303. interface IBaseOutput extends JSONObject {
  304. /**
  305. * Type of cell output.
  306. */
  307. output_type: string;
  308. }
  309. /**
  310. * Result of executing a code cell.
  311. */
  312. export
  313. interface IExecuteResult extends IBaseOutput {
  314. /**
  315. * Type of cell output.
  316. */
  317. output_type: 'execute_result';
  318. /**
  319. * A result's prompt number.
  320. */
  321. execution_count: ExecutionCount;
  322. /**
  323. * A mime-type keyed dictionary of data.
  324. */
  325. data: IMimeBundle;
  326. /**
  327. * Cell output metadata.
  328. */
  329. metadata: OutputMetadata;
  330. }
  331. /**
  332. * Data displayed as a result of code cell execution.
  333. */
  334. export
  335. interface IDisplayData extends IBaseOutput {
  336. /**
  337. * Type of cell output.
  338. */
  339. output_type: 'display_data';
  340. /**
  341. * A mime-type keyed dictionary of data.
  342. */
  343. data: IMimeBundle;
  344. /**
  345. * Cell output metadata.
  346. */
  347. metadata: OutputMetadata;
  348. }
  349. /**
  350. * Stream output from a code cell.
  351. */
  352. export
  353. interface IStream extends IBaseOutput {
  354. /**
  355. * Type of cell output.
  356. */
  357. output_type: 'stream';
  358. /**
  359. * The name of the stream.
  360. */
  361. name: StreamType;
  362. /**
  363. * The stream's text output.
  364. */
  365. text: MultilineString;
  366. }
  367. /**
  368. * An alias for a stream type.
  369. */
  370. export
  371. type StreamType = 'stdout' | 'stderr';
  372. /**
  373. * Output of an error that occurred during code cell execution.
  374. */
  375. export
  376. interface IError extends IBaseOutput {
  377. /**
  378. * Type of cell output.
  379. */
  380. output_type: 'error';
  381. /**
  382. * The name of the error.
  383. */
  384. ename: string;
  385. /**
  386. * The value, or message, of the error.
  387. */
  388. evalue: string;
  389. /**
  390. * The error's traceback.
  391. */
  392. traceback: string[];
  393. }
  394. /**
  395. * Unrecognized output.
  396. */
  397. export
  398. interface IUnrecognizedOutput extends IBaseOutput { }
  399. /**
  400. * Test whether an output is an execute result.
  401. */
  402. export
  403. function isExecuteResult(output: IOutput): output is IExecuteResult {
  404. return output.output_type === 'execute_result';
  405. }
  406. /**
  407. * Test whether an output is from display data.
  408. */
  409. export
  410. function isDisplayData(output: IOutput): output is IDisplayData {
  411. return output.output_type === 'display_data';
  412. }
  413. /**
  414. * Test whether an output is from a stream.
  415. */
  416. export
  417. function isStream(output: IOutput): output is IStream {
  418. return output.output_type === 'stream';
  419. }
  420. /**
  421. * Test whether an output is from a stream.
  422. */
  423. export
  424. function isError(output: IOutput): output is IError {
  425. return output.output_type === 'error';
  426. }
  427. /**
  428. * An output union type.
  429. */
  430. export
  431. type IOutput = IUnrecognizedOutput | IExecuteResult | IDisplayData | IStream | IError;
  432. }