Skip to content

Commit 50de78e

Browse files
committed
jupyter notebook multiline strings are actually strings in the in-memory format
1 parent 0cf9eb6 commit 50de78e

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

jupyter_notebook/static/phosphor-notebook/src/NotebookComponent.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MimeBundleComponent extends Component<nbformat.MimeBundle> {
2323
} else if (x = this.data["image/jpg"]) {
2424
return img({src:"data:image/jpg;base64,"+x})
2525
} else if (x = this.data["text/plain"]) {
26-
return pre(typeof x === "string" ? x : x.join('\n'));
26+
return pre(x);
2727
}
2828
}
2929
}
@@ -45,11 +45,10 @@ export var DisplayData = createFactory(DisplayDataComponent);
4545

4646
class StreamComponent extends Component<nbformat.Stream> {
4747
render() {
48-
return pre(this.data.text.join('\n'));
48+
return pre(this.data.text);
4949
}
5050
}
5151
export var Stream = createFactory(StreamComponent);
52-
//export var x;
5352

5453
class JupyterErrorComponent extends Component<nbformat.JupyterError> {
5554
render() {
@@ -102,17 +101,15 @@ class CodeCellComponent extends BaseComponent<nbformat.CodeCell> {
102101
this.node.appendChild(this.editor_node);
103102
this.node.appendChild(this.output_node);
104103

105-
var x = this.data.source;
106104
this._editor = CodeMirror(this.editor_node, {
107105
mode: 'python',
108-
value: typeof x === "string" ? x : x.join(""),
106+
value: this.data.source,
109107
lineNumbers: true})
110108
}
111109

112110
protected onUpdateRequest(msg: IMessage): void {
113-
var x = this.data.source;
114111
// we could call setValue on the editor itself, but the dts file doesn't recognize it.
115-
this._editor.getDoc().setValue(typeof x === "string" ? x : x.join(""));
112+
this._editor.getDoc().setValue(this.data.source);
116113
// we may want to save the refs at some point
117114
render(this.renderOutput(), this.output_node);
118115
}

jupyter_notebook/static/phosphor-notebook/src/demodata.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
import nbformat = require("./nbformat");
2-
export var notebook: nbformat.Notebook = {
2+
3+
var multiToString = function(x: string | string[]): string {
4+
return (typeof x === "string") ? x : x.join("");
5+
}
6+
7+
var diskToMemory = function(disknb: any): nbformat.Notebook {
8+
// Make a copy
9+
var nb = JSON.parse(JSON.stringify(disknb));
10+
// Convert multiline strings that are arrays to just strings
11+
var cells = nb.cells;
12+
for (var i = 0; i<cells.length; i++) {
13+
var c = cells[i];
14+
var c_type = c.cell_type;
15+
if (c_type === "raw" || c_type === "markdown" || c_type === "code") {
16+
c.source = multiToString(c.source)
17+
}
18+
if (c_type === "code") {
19+
for (var j = 0; j < c.outputs.length; j++) {
20+
var out = c.outputs[j];
21+
switch (out.output_type) {
22+
case "stream":
23+
out.text = multiToString(out.text);
24+
break;
25+
case "execute_result":
26+
case "display_data":
27+
var d = out.data;
28+
for (var key in d) {
29+
if (d.hasOwnProperty(key) && key !== "application/json") {
30+
d[key] = multiToString(d[key]);
31+
}
32+
}
33+
break;
34+
}
35+
}
36+
}
37+
}
38+
return nb;
39+
}
40+
41+
var notebook_disk = {
342
"cells": [
443
{
544
"cell_type": "markdown",
@@ -9,9 +48,9 @@ export var notebook: nbformat.Notebook = {
948
"\n",
1049
"This is a *test* of the **markdown** system.\n",
1150
"* Let's use [marked.js](https://github.com/chjj/marked)\n",
12-
"* Does this work?\n",
51+
"* Does this work? <b>hi</b>\n",
1352
"\n",
14-
"Math formulas like $e^{\\pi i} + 1 = 0$ and the following work too. \\\\[\\int x^2\\, dx\\\\]\n"
53+
"Math formulas like $e^{\\pi i} + 1 = 0$ and the following work too. $$\\int x^2\\, dx$$\n"
1554
]
1655
},
1756
{
@@ -153,3 +192,5 @@ export var notebook: nbformat.Notebook = {
153192
"nbformat": 4,
154193
"nbformat_minor": 0
155194
}
195+
196+
export var notebook: nbformat.Notebook = diskToMemory(notebook_disk);

jupyter_notebook/static/phosphor-notebook/src/nbformat.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
// Notebook format interfaces
22

3+
// In the notebook format *disk* representation, this would be string | string[]
4+
export type multilineString = string;
5+
36
export
47
interface MimeBundle {
5-
// values are always string|string[] if we pretend that the application/json key doesn't exist
6-
[key: string]: string | string[];
8+
// values are always multilineString if we pretend that the application/json key doesn't exist
9+
// in fact, the in-memory representation always is a string
10+
[key: string]: multilineString;
711

812
// we fudge the standard a bit here by not telling Typescript about the application/json
9-
// key, which will be a Javascript object if it exists. If we want to tell
13+
// key, which will be a Javascript object if it exists. If we want to tell, then uncomment below:
1014
//"application/json": {};
11-
12-
// we special-case some keys because we what we assume about them
13-
"image/png": string;
14-
"image/jpg": string;
1515
}
1616

1717
export
1818
interface ExecuteResult {
19-
output_type: string; /*"execute_result"*/
19+
output_type: string; // "execute_result"
2020
execution_count: number;
2121
data: MimeBundle;
2222
metadata: {};
2323
}
2424

2525
export
2626
interface DisplayData {
27-
output_type: string; /*"display_data"*/
27+
output_type: string; // "display_data"
2828
data: MimeBundle;
2929
metadata: {};
3030
}
3131

3232
export
3333
interface Stream {
34-
output_type: string; /*"stream"*/
34+
output_type: string; // "stream"
3535
name: string;
36-
text: string[];
36+
text: multilineString;
3737
}
3838

3939
export
4040
interface JupyterError {
41-
output_type: string; /*"error"*/
41+
output_type: string; // "error"
4242
ename: string;
4343
evalue: string;
4444
traceback: string[];
@@ -62,7 +62,7 @@ interface BaseCell {
6262
export
6363
interface RawCell extends BaseCell {
6464
cell_type: string; /*"raw"*/
65-
source: string | string[];
65+
source: multilineString;
6666
metadata: {
6767
format?: string;
6868
}
@@ -71,13 +71,13 @@ interface RawCell extends BaseCell {
7171
export
7272
interface MarkdownCell extends BaseCell {
7373
cell_type: string; /*"markdown"*/
74-
source: string | string[];
74+
source: multilineString;
7575
}
7676

7777
export
7878
interface CodeCell extends BaseCell {
7979
cell_type: string; /*"code"*/
80-
source: string | string[];
80+
source: multilineString;
8181
metadata: {
8282
collapsed?: boolean;
8383
scrolled?: boolean | string;

0 commit comments

Comments
 (0)