Skip to content

Commit

Permalink
Lab4: Build successful (#1)
Browse files Browse the repository at this point in the history
* add comments to thread Iterator

* my conversion

* the default next function

* build successfull

* _version Updated by hatchling?

* correct starting index
  • Loading branch information
savakarrohan authored Aug 24, 2023
1 parent 91b48a1 commit 39a5087
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 52 deletions.
24 changes: 4 additions & 20 deletions jupyterlab_kernelspy/_version.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
__all__ = ["__version__"]


def _fetchVersion():
import json
import os

HERE = os.path.abspath(os.path.dirname(__file__))

for d, _, _ in os.walk(HERE):
try:
with open(os.path.join(d, "package.json")) as f:
return json.load(f)["version"]
except FileNotFoundError:
pass

raise FileNotFoundError("Could not find package.json under dir {}".format(HERE))


__version__ = _fetchVersion()
# This file is auto-generated by Hatchling. As such, do not:
# - modify
# - track in version control e.g. be sure to add to .gitignore
__version__ = VERSION = '3.1.0'
63 changes: 59 additions & 4 deletions src/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { IIterator } from '@lumino/algorithm';

import { VDomModel } from '@jupyterlab/apputils';

import { Kernel, KernelMessage } from '@jupyterlab/services';
Expand All @@ -15,7 +13,10 @@ function isHeader(
return candidate.msg_id !== undefined;
}

export class ThreadIterator implements IIterator<ThreadIterator.IElement> {
/**
* An iterator class which itterates over the message thread being received
*/
export class ThreadIterator {
constructor(threads: MessageThread[], collapsed: { [key: string]: boolean }) {
this._threads = threads;
this._collapsed = collapsed;
Expand All @@ -27,27 +28,81 @@ export class ThreadIterator implements IIterator<ThreadIterator.IElement> {
return this;
}

[Symbol.iterator]() {
// iterator returns a javascript object having at least 1 next function
return {
next: () => {
if (this._child) {
const next = this._child.next();
// Return the child until it can't be anymore
if (next !== undefined) {
return { value: next, done: false };
}
// once there are no more children,
// Start by saying child is null
this._child = null;
}
// Till this point the index hasn't been added
// Therefore when it first meets this line gets to zero
// Move to next thread
++this._index;
// Hence when reaching end of the array it exits (done)
if (this._index >= this._threads.length) {
return { done: true };
}
// A variable to understand which part of the thread we touch first
// Starting from index 1 then moving ahead
const entry = this._threads[this._index];
if (
entry.children.length > 0 &&
!this._collapsed[entry.args.msg.header.msg_id]
) {
// If there are children and collapse is false
// Iterate over it's children
this._child = new ThreadIterator(entry.children, this._collapsed);
}
// But do return the current index position arguments and
return {
value: { args: entry.args, hasChildren: entry.children.length > 0 },
done: false
};
}
};
}

// The iteration function
next(): ThreadIterator.IElement | undefined {
// if child exists, check if the child has more children.
if (this._child) {
const next = this._child.next();
// Return the child until it can't be anymore
if (next !== undefined) {
return next;
}
// once there are no more children,
// Start by saying child is null
this._child = null;
}
// Till this point the index hasn't been added
// Therefore when it first meets this line gets to zero
// Move to next thread
++this._index;
// Hence when reaching end of the array it exits (done)
if (this._index >= this._threads.length) {
return undefined;
}
// A variable to understand which part of the thread we touch first
// Starting from index 1 then moving ahead
const entry = this._threads[this._index];
if (
entry.children.length > 0 &&
!this._collapsed[entry.args.msg.header.msg_id]
) {
// Iterate over children after this
// If there are children and collapse is false
// Iterate over it's children
this._child = new ThreadIterator(entry.children, this._collapsed);
}
// But do return the current index position arguments and
return { args: entry.args, hasChildren: entry.children.length > 0 };
}

Expand Down
56 changes: 28 additions & 28 deletions src/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {
jsonIcon
} from '@jupyterlab/ui-components';

import { each } from '@lumino/algorithm';

import { UUID } from '@lumino/coreutils';

import { Message as luminoMessage } from '@lumino/messaging';
Expand Down Expand Up @@ -161,34 +159,36 @@ export class MessageLogView extends VDomRenderer<KernelSpyModel> {
const threads = new ThreadIterator(model.tree, this.collapsed);

let first = true;
each(threads, ({ args, hasChildren }) => {
const depth = model.depth(args);
if (depth === 0) {
if (first) {
first = false;
} else {
// Insert spacer between main threads
elements.push(
<span
key={`'divider-${args.msg.header.msg_id}`}
className="jp-kernelspy-divider"
/>
);
for (const thread of threads) {
if (thread) {
const depth = model.depth(thread.args);
if (depth === 0) {
if (first) {
first = false;
} else {
// Insert spacer between main threads
elements.push(
<span
key={`'divider-${thread.args.msg.header.msg_id}`}
className="jp-kernelspy-divider"
/>
);
}
}
const collapsed = this.collapsed[thread.args.msg.header.msg_id];
elements.push(
...Message({
message: thread.args.msg,
depth,
collapsed,
hasChildren: thread.hasChildren,
onCollapse: message => {
this.onCollapse(message);
}
})
);
}
const collapsed = this.collapsed[args.msg.header.msg_id];
elements.push(
...Message({
message: args.msg,
depth,
collapsed,
hasChildren,
onCollapse: message => {
this.onCollapse(message);
}
})
);
});
}
return elements;
}

Expand Down

0 comments on commit 39a5087

Please sign in to comment.