Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne committed Apr 2, 2023
1 parent d654eca commit 73d691e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@
}
],
"commands": [
{
"command": "jupyter.helloWorld",
"title": "History",
"category": "Jupyter (Dev)"
},
{
"command": "dataScience.ClearCache",
"title": "%jupyter.command.dataScience.clearCache.title%",
Expand Down Expand Up @@ -1009,6 +1014,10 @@
}
],
"commandPalette": [
{
"command": "jupyter.helloWorld",
"title": "History"
},
{
"command": "jupyter.restoreOutput",
"title": "Restore Cell Output"
Expand Down
46 changes: 42 additions & 4 deletions pythonFiles/vscMagics/vscodeMagics.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,44 @@ def _rotate_buffer(self):
return old_buffer


class CapturingDisplayHook(object):
def __init__(self, shell, outputs=None, echo=None):
self.shell = shell
if outputs is None:
outputs = []
self.outputs = outputs
self.echo = echo

def __call__(self, result=None):
if result is None:
return
format_dict, md_dict = self.shell.display_formatter.format(result)
self.outputs.append({ 'data': format_dict, 'metadata': md_dict })
if self.echo is not None:
self.echo(result)


from IPython.core.displaypub import DisplayPublisher
from traitlets import List

class CapturingDisplayPublisher(DisplayPublisher):
"""A DisplayPublisher that stores"""
outputs = List()
def __init__(self, echo=None, *args, **kwargs):
super(CapturingDisplayPublisher, self).__init__(*args, **kwargs)
self.echo = echo

def publish(self, data, metadata=None, source=None, *, transient=None, update=False):
self.outputs.append({'data':data, 'metadata':metadata,
'transient':transient, 'update':update})
if self.echo is not None:
self.echo.publish(data, metadata=metadata, transient=transient, update=update)

def clear_output(self, wait=False):
if self.echo is not None:
self.echo.clear_output(wait=wait)
self.outputs.clear()


class capture_output(object):
"""context manager for capturing stdout/err"""
Expand All @@ -484,8 +522,8 @@ def __init__(self, stdout=True, stderr=True, display=True):

def __enter__(self):
from IPython.core.getipython import get_ipython
from IPython.core.displaypub import CapturingDisplayPublisher
from IPython.core.displayhook import CapturingDisplayHook
# from IPython.core.displaypub import CapturingDisplayPublisher
# from IPython.core.displayhook import CapturingDisplayHook

self.sys_stdout = sys.stdout
self.sys_stderr = sys.stderr
Expand All @@ -509,11 +547,11 @@ def __enter__(self):
sys.stderr = stderr
if self.display:
self.save_display_pub = self.shell.display_pub
self.shell.display_pub = CapturingDisplayPublisher()
self.shell.display_pub = CapturingDisplayPublisher(echo=self.shell.display_pub)
outputs = self.shell.display_pub.outputs
self.save_display_hook = sys.displayhook
sys.displayhook = CapturingDisplayHook(shell=self.shell,
outputs=outputs)
outputs=outputs, echo=sys.displayhook)

return CapturedIO(stdout, stderr, outputs)

Expand Down
24 changes: 24 additions & 0 deletions src/notebooks/controllers/controllerRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class ControllerRegistration implements IControllerRegistration, IExtensi
@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer,
@inject(IJupyterServerUriStorage) private readonly serverUriStorage: IJupyterServerUriStorage,
@inject(IKernelFinder) private readonly kernelFinder: IKernelFinder,
@inject(IKernelProvider) private readonly kernelProvider: IKernelProvider,
@inject(IMemento) @named(WORKSPACE_MEMENTO) private readonly workspaceStorage: Memento
) {}
activate(): void {
Expand Down Expand Up @@ -164,6 +165,29 @@ export class ControllerRegistration implements IControllerRegistration, IExtensi
this
)
);
this.disposables.push(
commands.registerCommand(
'jupyter.helloWorld',
async () => {
const notebook = window.activeNotebookEditor?.notebook;
if (!notebook) {
return;
}
const kernel = this.kernelProvider.get(notebook);
if (!kernel) {
return;
}
const result = await kernel.session?.kernel?.requestHistory({
hist_access_type: 'tail',
n: 2,
output: true,
raw: true
});
console.error(result);
},
this
)
);

const restoreOutputs = async (notebook: NotebookDocument) => {
const slowInfo = this.workspaceStorage.get<
Expand Down

0 comments on commit 73d691e

Please sign in to comment.