Skip to content

Commit 73d691e

Browse files
committed
wip
1 parent d654eca commit 73d691e

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@
245245
}
246246
],
247247
"commands": [
248+
{
249+
"command": "jupyter.helloWorld",
250+
"title": "History",
251+
"category": "Jupyter (Dev)"
252+
},
248253
{
249254
"command": "dataScience.ClearCache",
250255
"title": "%jupyter.command.dataScience.clearCache.title%",
@@ -1009,6 +1014,10 @@
10091014
}
10101015
],
10111016
"commandPalette": [
1017+
{
1018+
"command": "jupyter.helloWorld",
1019+
"title": "History"
1020+
},
10121021
{
10131022
"command": "jupyter.restoreOutput",
10141023
"title": "Restore Cell Output"

pythonFiles/vscMagics/vscodeMagics.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,44 @@ def _rotate_buffer(self):
469469
return old_buffer
470470

471471

472+
class CapturingDisplayHook(object):
473+
def __init__(self, shell, outputs=None, echo=None):
474+
self.shell = shell
475+
if outputs is None:
476+
outputs = []
477+
self.outputs = outputs
478+
self.echo = echo
479+
480+
def __call__(self, result=None):
481+
if result is None:
482+
return
483+
format_dict, md_dict = self.shell.display_formatter.format(result)
484+
self.outputs.append({ 'data': format_dict, 'metadata': md_dict })
485+
if self.echo is not None:
486+
self.echo(result)
487+
488+
489+
from IPython.core.displaypub import DisplayPublisher
490+
from traitlets import List
491+
492+
class CapturingDisplayPublisher(DisplayPublisher):
493+
"""A DisplayPublisher that stores"""
494+
outputs = List()
495+
def __init__(self, echo=None, *args, **kwargs):
496+
super(CapturingDisplayPublisher, self).__init__(*args, **kwargs)
497+
self.echo = echo
498+
499+
def publish(self, data, metadata=None, source=None, *, transient=None, update=False):
500+
self.outputs.append({'data':data, 'metadata':metadata,
501+
'transient':transient, 'update':update})
502+
if self.echo is not None:
503+
self.echo.publish(data, metadata=metadata, transient=transient, update=update)
504+
505+
def clear_output(self, wait=False):
506+
if self.echo is not None:
507+
self.echo.clear_output(wait=wait)
508+
self.outputs.clear()
509+
472510

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

485523
def __enter__(self):
486524
from IPython.core.getipython import get_ipython
487-
from IPython.core.displaypub import CapturingDisplayPublisher
488-
from IPython.core.displayhook import CapturingDisplayHook
525+
# from IPython.core.displaypub import CapturingDisplayPublisher
526+
# from IPython.core.displayhook import CapturingDisplayHook
489527

490528
self.sys_stdout = sys.stdout
491529
self.sys_stderr = sys.stderr
@@ -509,11 +547,11 @@ def __enter__(self):
509547
sys.stderr = stderr
510548
if self.display:
511549
self.save_display_pub = self.shell.display_pub
512-
self.shell.display_pub = CapturingDisplayPublisher()
550+
self.shell.display_pub = CapturingDisplayPublisher(echo=self.shell.display_pub)
513551
outputs = self.shell.display_pub.outputs
514552
self.save_display_hook = sys.displayhook
515553
sys.displayhook = CapturingDisplayHook(shell=self.shell,
516-
outputs=outputs)
554+
outputs=outputs, echo=sys.displayhook)
517555

518556
return CapturedIO(stdout, stderr, outputs)
519557

src/notebooks/controllers/controllerRegistration.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export class ControllerRegistration implements IControllerRegistration, IExtensi
9494
@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer,
9595
@inject(IJupyterServerUriStorage) private readonly serverUriStorage: IJupyterServerUriStorage,
9696
@inject(IKernelFinder) private readonly kernelFinder: IKernelFinder,
97+
@inject(IKernelProvider) private readonly kernelProvider: IKernelProvider,
9798
@inject(IMemento) @named(WORKSPACE_MEMENTO) private readonly workspaceStorage: Memento
9899
) {}
99100
activate(): void {
@@ -164,6 +165,29 @@ export class ControllerRegistration implements IControllerRegistration, IExtensi
164165
this
165166
)
166167
);
168+
this.disposables.push(
169+
commands.registerCommand(
170+
'jupyter.helloWorld',
171+
async () => {
172+
const notebook = window.activeNotebookEditor?.notebook;
173+
if (!notebook) {
174+
return;
175+
}
176+
const kernel = this.kernelProvider.get(notebook);
177+
if (!kernel) {
178+
return;
179+
}
180+
const result = await kernel.session?.kernel?.requestHistory({
181+
hist_access_type: 'tail',
182+
n: 2,
183+
output: true,
184+
raw: true
185+
});
186+
console.error(result);
187+
},
188+
this
189+
)
190+
);
167191

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

0 commit comments

Comments
 (0)