Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UI] Jupyter notebook export patch #417

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions ui/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -959,14 +959,15 @@ function ExportJupyterNB() {
q.push([pod, geoScore.substring(0, 2) + "0" + geoScore.substring(2)]);
} else if (pod.type == "CODE") {
let podOutput: any[] = [];
// FIXME, the pod result needs to support an array of execution result as well as the execution order
if (pod.stdout) {
podOutput.push({
output_type: "stream",
name: "stdout",
text: pod.stdout.split(/\r?\n/).map((line) => line + "\n"),
});
}
if (pod.result) {
if (pod.result?.image) {
podOutput.push({
output_type: "display_data",
data: {
Expand All @@ -977,6 +978,29 @@ function ExportJupyterNB() {
},
});
}
if (
pod.result?.text &&
!pod.result?.text.startsWith("ok") &&
!pod.result?.text.startsWith("error")
) {
podOutput.push({
output_type: "execute_result",
data: {
"text/plain": (pod.result.text || "")
.split(/\r?\n/)
.map((line) => line + "\n") || [""],
},
execution_count: pod.result?.count,
});
}
if (pod.error) {
podOutput.push({
output_type: "error",
ename: pod.error?.ename,
evalue: pod.error?.evalue,
traceback: pod.error?.stacktrace,
});
}
jupyterCellList.push({
cell_type: "code",
execution_count: pod.result?.count || 0,
Expand Down Expand Up @@ -1019,6 +1043,7 @@ function ExportJupyterNB() {
}

// Add scope structure as a block comment at the head of each cell
// FIXME, RICH pod should have a different format
let scopeStructureAsComment =
scopes.length > 0
? [
Expand All @@ -1035,21 +1060,26 @@ function ExportJupyterNB() {
cell.source = [...scopeStructureAsComment, ...sourceArray];
}

const fileContent = JSON.stringify({
// hard-code Jupyter Notebook top-level metadata
metadata: {
name: repoName,
kernelspec: {
name: "python3",
display_name: "Python 3",
const fileContent = JSON.stringify(
{
// hard-code Jupyter Notebook top-level metadata
metadata: {
name: repoName,
kernelspec: {
name: "python3",
display_name: "Python 3",
},
language_info: { name: "python" },
Codepod_version: "v0.0.1",
Codepod_repo_id: `${repoId}`,
},
language_info: { name: "python" },
Codepod_version: "v0.0.1",
nbformat: 4.0,
nbformat_minor: 0,
cells: jupyterCellList,
},
nbformat: 4,
nbformat_minor: 0,
cells: jupyterCellList,
});
null,
4
);

// Generate the download link on the fly
let element = document.createElement("a");
Expand Down
14 changes: 11 additions & 3 deletions ui/src/lib/store/canvasSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (

let podResult = { count: cell.execution_count, text: "", image: "" };
let podStdOut = "";
let podError = { ename: "", evalue: "", stacktrace: [] };

for (const cellOutput of cell.cellOutputs) {
if (
Expand All @@ -562,6 +563,14 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
podResult.text = cellOutput["data"]["text/plain"].join("");
podResult.image = cellOutput["data"]["image/png"];
}
if (cellOutput["output_type"] === "execute_result") {
podResult.text = cellOutput["data"]["text/plain"].join("");
}
if (cellOutput["output_type"] === "error") {
podError.ename = cellOutput["ename"];
podError.evalue = cellOutput["evalue"];
podError.stacktrace = cellOutput["traceback"];
}
}
// move the created node to scope and configure the necessary node attributes
const posInsideScope = getNodePositionInsideScope(
Expand Down Expand Up @@ -606,6 +615,7 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
richContent: podRichContent,
stdout: podStdOut === "" ? undefined : podStdOut,
result: podResult.text === "" ? undefined : podResult,
error: podError.ename === "" ? undefined : podError,
// For my local update, set dirty to true to push to DB.
dirty: true,
pending: true,
Expand All @@ -621,9 +631,7 @@ export const createCanvasSlice: StateCreator<MyState, [], [], CanvasSlice> = (
}
get().adjustLevel();
get().buildNode2Children();
// Set initial width as a scale of max line length.
//get().setNodeCharWidth(scopeNode.id, Math.ceil(maxLineLength * 0.8));
get().setNodeCharWidth(scopeNode.id, 30);
// FIXME updateView() reset the pod width to 300, scope width to 400.
get().updateView();
},
autoLayoutOnce: false,
Expand Down
2 changes: 1 addition & 1 deletion ui/src/lib/store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type Pod = {
status?: string;
stdout?: string;
stderr?: string;
error?: { evalue: string; stacktrace: string[] } | null;
error?: { ename: string; evalue: string; stacktrace: string[] } | null;
lastExecutedAt?: Date;
lang: string;
column?: number;
Expand Down