Parcourir la source

Merge pull request #6063 from vatlab/inject_metadata

Pass metadata from notebook to console
Jason Grout il y a 6 ans
Parent
commit
7635934ef1

+ 5 - 2
packages/console-extension/src/index.ts

@@ -39,7 +39,7 @@ import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
 
 import { find } from '@phosphor/algorithm';
 
-import { ReadonlyJSONObject } from '@phosphor/coreutils';
+import { ReadonlyJSONObject, JSONObject } from '@phosphor/coreutils';
 
 import { DisposableSet } from '@phosphor/disposable';
 
@@ -460,7 +460,10 @@ async function activateConsole(
           if (args['activate'] !== false) {
             shell.activateById(widget.id);
           }
-          void widget.console.inject(args['code'] as string);
+          void widget.console.inject(
+            args['code'] as string,
+            args['metadata'] as JSONObject
+          );
           return true;
         }
         return false;

+ 5 - 2
packages/console/src/widget.ts

@@ -28,7 +28,7 @@ import { KernelMessage } from '@jupyterlab/services';
 
 import { each } from '@phosphor/algorithm';
 
-import { MimeData } from '@phosphor/coreutils';
+import { MimeData, JSONObject } from '@phosphor/coreutils';
 
 import { Drag } from '@phosphor/dragdrop';
 
@@ -337,9 +337,12 @@ export class CodeConsole extends Widget {
    *
    * @returns A promise that indicates when the injected cell's execution ends.
    */
-  inject(code: string): Promise<void> {
+  inject(code: string, metadata: JSONObject = {}): Promise<void> {
     let cell = this.createCodeCell();
     cell.model.value.text = code;
+    for (let key of Object.keys(metadata)) {
+      cell.model.metadata.set(key, metadata[key]);
+    }
     this.addCell(cell);
     return this._execute(cell);
   }

+ 3 - 1
packages/notebook-extension/src/index.ts

@@ -956,6 +956,7 @@ function addCommands(
       const { context, content } = current;
 
       let cell = content.activeCell;
+      let metadata = cell.model.metadata.toJSON();
       let path = context.path;
       // ignore action in non-code cell
       if (!cell || cell.model.type !== 'code') {
@@ -997,7 +998,8 @@ function addCommands(
       await commands.execute('console:inject', {
         activate: false,
         code,
-        path
+        path,
+        metadata
       });
     },
     isEnabled