Skip to content

Commit cbbe9fe

Browse files
committed
implement #5219 -- jupyter notebook cell ids: save our when exporting to ipynb (and importing from ipynb)
- this is mainly for compatibility with the jupyter ecosystem and tooling; not used internally at all.
1 parent b67cce8 commit cbbe9fe

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/packages/jupyter/ipynb/export-to-ipynb.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function cell_to_ipynb(id: string, opts: any) {
5050
cell_type: (left = cell.get("cell_type")) != null ? left : "code",
5151
source: diff_friendly((left1 = cell.get("input")) != null ? left1 : ""),
5252
metadata,
53+
id,
5354
};
5455

5556
// Handle any extra metadata (mostly user defined) that we don't handle in a special
@@ -87,7 +88,7 @@ function cell_to_ipynb(id: string, opts: any) {
8788
output,
8889
exec_count,
8990
opts.more_output != null ? opts.more_output[id] : undefined,
90-
opts.blob_store
91+
opts.blob_store,
9192
);
9293
} else if (obj.outputs == null && obj.cell_type === "code") {
9394
obj.outputs = []; // annoying requirement of ipynb file format.
@@ -130,7 +131,7 @@ function process_other_metadata(obj: any, other_metadata: any) {
130131
function process_attachments(
131132
obj: any,
132133
attachments: any,
133-
blob_store: BlobStore | undefined
134+
blob_store: BlobStore | undefined,
134135
) {
135136
if (attachments == null || blob_store == null) {
136137
// don't have to or can't do anything (https://github.com/sagemathinc/cocalc/issues/4272)
@@ -155,7 +156,7 @@ function ipynb_outputs(
155156
output: any,
156157
exec_count: any,
157158
more_output: any,
158-
blob_store: BlobStore | undefined
159+
blob_store: BlobStore | undefined,
159160
) {
160161
// If the last message has the more_output field, then there may be
161162
// more output messages stored, which are not in the cells object.
@@ -170,7 +171,7 @@ function ipynb_outputs(
170171
immutable.fromJS({
171172
text: "WARNING: Some output was deleted.\n",
172173
name: "stderr",
173-
})
174+
}),
174175
);
175176
} else {
176177
// Indeed, the last message has the more_output field.
@@ -204,7 +205,7 @@ function ipynb_outputs(
204205
function process_output_n(
205206
output_n: any,
206207
exec_count: any,
207-
blob_store: BlobStore | undefined
208+
blob_store: BlobStore | undefined,
208209
) {
209210
if (output_n == null) {
210211
return;

src/packages/jupyter/ipynb/import-from-ipynb.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ export class IPynbImporter {
255255
);
256256
};
257257

258-
_get_new_id = () => {
258+
_get_new_id = (cell) => {
259+
if (cell?.id && this._id_is_available(cell.id)) {
260+
// attempt to use id in the ipynb file
261+
return cell.id;
262+
}
259263
if (this._new_id != null) {
260264
return this._new_id(this._id_is_available);
261265
} else {
@@ -333,7 +337,7 @@ export class IPynbImporter {
333337
? this._existing_ids != null
334338
? this._existing_ids[n]
335339
: undefined
336-
: this._get_new_id();
340+
: this._get_new_id(cell);
337341
const obj: any = {
338342
type: "cell",
339343
id,
@@ -344,12 +348,12 @@ export class IPynbImporter {
344348
cell.metadata != null && cell.metadata.cocalc != null
345349
? cell.metadata.cocalc.outputs
346350
: undefined,
347-
id
351+
id,
348352
),
349353
cell_type: this._get_cell_type(cell.cell_type),
350354
exec_count: this._get_exec_count(
351355
cell.execution_count,
352-
cell.prompt_number
356+
cell.prompt_number,
353357
),
354358
};
355359

0 commit comments

Comments
 (0)