(output: NotebookCellOutput)
| 485 | }; |
| 486 | } |
| 487 | export function translateCellDisplayOutput(output: NotebookCellOutput): JupyterOutput { |
| 488 | const customMetadata = output.metadata as CellOutputMetadata | undefined; |
| 489 | let result: JupyterOutput; |
| 490 | // Possible some other extension added some output (do best effort to translate & save in ipynb). |
| 491 | // In which case metadata might not contain `outputType`. |
| 492 | const outputType = customMetadata?.outputType as nbformat.OutputType; |
| 493 | switch (outputType) { |
| 494 | case 'error': { |
| 495 | result = translateCellErrorOutput(output); |
| 496 | break; |
| 497 | } |
| 498 | case 'stream': { |
| 499 | result = convertStreamOutput(output); |
| 500 | break; |
| 501 | } |
| 502 | case 'display_data': { |
| 503 | result = { |
| 504 | output_type: 'display_data', |
| 505 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 506 | data: output.items.reduce((prev: any, curr) => { |
| 507 | prev[curr.mime] = convertOutputMimeToJupyterOutput(curr.mime, curr.data as Uint8Array); |
| 508 | return prev; |
| 509 | }, {}), |
| 510 | metadata: customMetadata?.metadata || {} // This can never be undefined. |
| 511 | }; |
| 512 | break; |
| 513 | } |
| 514 | case 'execute_result': { |
| 515 | result = { |
| 516 | output_type: 'execute_result', |
| 517 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 518 | data: output.items.reduce((prev: any, curr) => { |
| 519 | prev[curr.mime] = convertOutputMimeToJupyterOutput(curr.mime, curr.data as Uint8Array); |
| 520 | return prev; |
| 521 | }, {}), |
| 522 | metadata: customMetadata?.metadata || {}, // This can never be undefined. |
| 523 | execution_count: |
| 524 | typeof customMetadata?.executionCount === 'number' ? customMetadata?.executionCount : null // This can never be undefined, only a number or `null`. |
| 525 | }; |
| 526 | break; |
| 527 | } |
| 528 | case 'update_display_data': { |
| 529 | result = { |
| 530 | output_type: 'update_display_data', |
| 531 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 532 | data: output.items.reduce((prev: any, curr) => { |
| 533 | prev[curr.mime] = convertOutputMimeToJupyterOutput(curr.mime, curr.data as Uint8Array); |
| 534 | return prev; |
| 535 | }, {}), |
| 536 | metadata: customMetadata?.metadata || {} // This can never be undefined. |
| 537 | }; |
| 538 | break; |
| 539 | } |
| 540 | default: { |
| 541 | const isError = |
| 542 | output.items.length === 1 && output.items.every((item) => item.mime === CellOutputMimeTypes.error); |
| 543 | const isStream = output.items.every( |
| 544 | (item) => item.mime === CellOutputMimeTypes.stderr || item.mime === CellOutputMimeTypes.stdout |
no test coverage detected