Skip to content

Commit a848642

Browse files
committed
render submit result to the bottom of notebook
1 parent 6a189e2 commit a848642

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

jupyterlab_leetcode/handlers/leetcode_handler.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,6 @@ async def check(self, cnt=0):
482482
return
483483

484484
self.check_result = json.loads(resp.body)
485-
print(
486-
f"Checking submission {self.submission_id}, attempts: {cnt}, result: {self.check_result}"
487-
)
488485
self.write_message(
489486
{
490487
"type": "submissionResult",

src/components/LeetCode.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const LeetCode: React.FC<{
4343
}, []);
4444

4545
const openNoteBook = (path: string) => {
46+
// TODO: notebook.NotebookActions.runCells of pre-code cell
4647
docManager.openOrReveal(path);
4748
};
4849

src/components/LeetCodeNotebookHeader.tsx

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useEffect, useState } from 'react';
2-
import { NotebookPanel } from '@jupyterlab/notebook';
2+
import { NotebookPanel, NotebookActions } from '@jupyterlab/notebook';
3+
import { ICellModel } from '@jupyterlab/cells';
34
import { submitNotebook } from '../services/notebook';
45
import { makeWebSocket } from '../services/handler';
56
import {
@@ -48,6 +49,61 @@ const LeetCodeNotebookHeader: React.FC<{ notebook: NotebookPanel }> = ({
4849
return ws;
4950
};
5051

52+
const getResultCell = () => {
53+
let resultCellModel: ICellModel | null = null;
54+
const cells = notebook.content.model?.cells ?? [];
55+
for (const cell of cells) {
56+
if (cell.metadata['id'] === 'result') {
57+
resultCellModel = cell;
58+
}
59+
}
60+
if (!resultCellModel) {
61+
const activeCellIdx = cells.length ? cells.length - 1 : 0;
62+
notebook.content.activeCellIndex = activeCellIdx;
63+
NotebookActions.insertBelow(notebook.content);
64+
const activeCell = notebook.content.activeCell;
65+
if (activeCell) {
66+
resultCellModel = activeCell.model;
67+
resultCellModel.setMetadata('id', 'result');
68+
}
69+
}
70+
return resultCellModel;
71+
};
72+
73+
const populateResultCell = (
74+
cellModel: ICellModel,
75+
result: Extract<LeetCodeSubmissionResult, { state: 'SUCCESS' }>
76+
) => {
77+
let source = '';
78+
switch (result.status_msg) {
79+
case 'Accepted': {
80+
source = `# Accepted\n\nRuntime: ${result.display_runtime}\nMemory: ${result.status_memory}\n\nOutput:\n\`\`\`\n${result.code_output}\n\`\`\``;
81+
break;
82+
}
83+
case 'Wrong Answer': {
84+
source = `# Wrong Answer\n\nExpected: ${result.expected_output}\nGot: ${result.code_output}\n\nInput:\n\`\`\`\n${result.input_formatted}\n\`\`\``;
85+
break;
86+
}
87+
case 'Time Limit Exceeded': {
88+
source = `# Time Limit Exceeded\n\nRuntime: ${result.display_runtime}\nMemory: ${result.status_memory}\n\nInput:\n\`\`\`\n${result.input_formatted}\n\`\`\``;
89+
break;
90+
}
91+
case 'Memory Limit Exceeded': {
92+
source = `# Memory Limit Exceeded\n\nRuntime: ${result.display_runtime}\nMemory: ${result.status_memory}\n\nInput:\n\`\`\`\n${result.input_formatted}\n\`\`\``;
93+
break;
94+
}
95+
case 'Runtime Error': {
96+
source = `# Runtime Error\n\nRuntime: ${result.display_runtime}\nMemory: ${result.status_memory}\n\nOutput:\n\`\`\`\n${result.code_output}\n\`\`\`\n\nInput:\n\`\`\`\n${result.input_formatted}\n\`\`\``;
97+
break;
98+
}
99+
case 'Internal Error': {
100+
source = `# Internal Error\n\nRuntime: ${result.display_runtime}\nMemory: ${result.status_memory}\n\nOutput:\n\`\`\`\n${result.code_output}\n\`\`\`\n\nInput:\n\`\`\`\n${result.input_formatted}\n\`\`\``;
101+
break;
102+
}
103+
}
104+
cellModel.sharedModel.setSource(source);
105+
};
106+
51107
useEffect(() => {
52108
if (!submissionId) {
53109
return;
@@ -70,6 +126,18 @@ const LeetCodeNotebookHeader: React.FC<{ notebook: NotebookPanel }> = ({
70126
}
71127
}, [ws, ws?.readyState]);
72128

129+
useEffect(() => {
130+
if (result?.state !== 'SUCCESS') {
131+
return;
132+
}
133+
const resultCellModel = getResultCell();
134+
if (resultCellModel) {
135+
populateResultCell(resultCellModel, result);
136+
NotebookActions.changeCellType(notebook.content, 'markdown');
137+
NotebookActions.run(notebook.content);
138+
}
139+
}, [result?.state]);
140+
73141
return (
74142
<div>
75143
<button onClick={submit}>Submit</button>

src/types/leetcode.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,5 @@ export type LeetCodeSubmissionResult =
108108
memory_percentile: number | null;
109109
input_formatted: string;
110110
input: string;
111-
status_msg: string;
111+
status_msg: 'Accepted' | 'Wrong Answer' | string;
112112
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy