index.ts 11 KB

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