Skip to content

Commit 4e5f6db

Browse files
authored
Refactor code to add DisplayResult component for displaying output in NotebookCell (#223)
1 parent 8070c87 commit 4e5f6db

File tree

6 files changed

+104
-41
lines changed

6 files changed

+104
-41
lines changed

examples/sg-resale-flat-prices/sg-resale-flat-prices.ipynb

Lines changed: 44 additions & 33 deletions
Large diffs are not rendered by default.

webapp/src/components/notebook/cell/Cell.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useState, useEffect, useCallback } from 'react';
2-
import { Card, CardContent } from '@mui/material';
3-
import AceEditor from 'react-ace';
2+
import { Card, CardContent, Typography } from '@mui/material';
43
import 'ace-builds/src-noconflict/mode-python';
54
import 'ace-builds/src-noconflict/mode-markdown';
65
import 'ace-builds/src-noconflict/theme-github';
@@ -15,6 +14,7 @@ import MarkdownEditor from './content/MarkdownEditor';
1514
import TextResult from './result/TextResult';
1615
import ErrorResult from './result/ErrorResult';
1716
import CodeResult from './result/CodeResult';
17+
import DisplayResult from './result/DisplayResult';
1818

1919

2020
function Cell({
@@ -155,7 +155,9 @@ function Cell({
155155
ErrorResult(index, isFocused, output)
156156
) : (output.output_type === OutputType.STREAM ? (
157157
CodeResult(index, output)
158-
) : null)
158+
) : (output.output_type === OutputType.DISPLAY_DATA ? (
159+
DisplayResult(output)
160+
) : null))
159161
)
160162
)}
161163
</div>

webapp/src/components/notebook/cell/result/CodeResult.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ function CodeResult(index, output) {
4242
remarkPlugins={[gfm]}
4343
children={StringUtils.convertJupyterTableToMarkdownTable(output.text)}
4444
components={{
45-
// table: ({node, ...props}) => <table style={{border: '0.2px solid lightgrey'}} {...props} />,
46-
th: ({node, ...props}) => <th style={{border: '0.2px solid lightgrey', padding: '5px'}} {...props} />,
47-
td: ({node, ...props}) => <td style={{border: '0.2px solid lightgrey', padding: '5px'}} {...props} />,
45+
table: ({node, ...props}) => <table style={{borderCollapse: 'collapse'}} {...props} />,
46+
th: ({node, ...props}) => <th style={{border: '0.2px solid lightblue', padding: '3px'}} {...props} />,
47+
td: ({node, ...props}) => <td style={{border: '0.2px solid lightblue', padding: '3px'}} {...props} />,
4848
}} />
4949
) : (
5050
output.text
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Card } from '@mui/material';
2+
3+
4+
function DisplayResult(output) {
5+
6+
return (
7+
< div
8+
variant="body1"
9+
component="div"
10+
sx={{
11+
color: 'black',
12+
width: '85%',
13+
fontFamily: 'Arial',
14+
fontSize: '14px',
15+
fontWeight: 'bold',
16+
textAlign: 'left',
17+
marginLeft: 0,
18+
marginRight: 1,
19+
marginTop: 0,
20+
marginBottom: 1,
21+
marginRight: 1,
22+
}}>
23+
<Card
24+
elevation={0}
25+
sx={{
26+
width: '85%',
27+
marginTop: 0,
28+
marginBottom: 1,
29+
marginLeft: 10,
30+
marginRight: 1,
31+
border: 0.1,
32+
borderColor: 'lightgrey',
33+
backgroundColor: 'rgba(0, 0, 0, 0.01)',
34+
}}>
35+
<img src={`data:image/png;base64,${output.data['image/png']}`} />
36+
</Card>
37+
</div>
38+
)
39+
}
40+
41+
export default DisplayResult;

webapp/src/components/notebook/cell/result/OutputType.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export const OutputType = {
33
ERROR: 'error',
44
STREAM: 'stream',
55
STATUS: 'status',
6+
DISPLAY_DATA: 'display_data',
67
};

webapp/src/models/NotebookModel.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class NotebookModel {
322322
const message = JSON.parse(event.data);
323323
// Only process messages that are in response to this execution
324324
if (message.parent_header.msg_id === msg_id) {
325-
console.log('Received message:', message);
325+
// console.log('Received message:', message);
326326
cell.lastExecutionTime = new Date().toISOString().slice(0, 19).replace('T', ' ');
327327
if (message.header.msg_type === OutputType.STATUS) {
328328
// Update the cell's status
@@ -356,7 +356,15 @@ class NotebookModel {
356356
evalue: message.content.evalue,
357357
traceback: message.content.traceback,
358358
});
359-
}}
359+
} else if (message.header.msg_type === OutputType.DISPLAY_DATA) {
360+
// Add the output to the cell's outputs array
361+
cell.outputs.push({
362+
output_type: OutputType.DISPLAY_DATA,
363+
data: message.content.data,
364+
metadata: message.content.metadata,
365+
});
366+
}
367+
}
360368
};
361369
});
362370

0 commit comments

Comments
 (0)