index.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ILayoutRestorer, JupyterLab, JupyterLabPlugin
  5. } from '@jupyterlab/application';
  6. import {
  7. ICommandPalette, InstanceTracker
  8. } from '@jupyterlab/apputils';
  9. import {
  10. IConsoleTracker
  11. } from '@jupyterlab/console';
  12. import {
  13. IInspector, InspectionHandler, InspectorPanel, KernelConnector
  14. } from '@jupyterlab/inspector';
  15. import {
  16. INotebookTracker
  17. } from '@jupyterlab/notebook';
  18. import {
  19. InspectorManager
  20. } from './manager';
  21. /**
  22. * The command IDs used by the inspector plugin.
  23. */
  24. namespace CommandIDs {
  25. export
  26. const open = 'inspector:open';
  27. }
  28. /**
  29. * A service providing code introspection.
  30. */
  31. const inspector: JupyterLabPlugin<IInspector> = {
  32. id: '@jupyterlab/inspector-extension:inspector',
  33. requires: [ICommandPalette, ILayoutRestorer],
  34. provides: IInspector,
  35. autoStart: true,
  36. activate: (app: JupyterLab, palette: ICommandPalette, restorer: ILayoutRestorer): IInspector => {
  37. const { commands, shell } = app;
  38. const manager = new InspectorManager();
  39. const category = 'Inspector';
  40. const command = CommandIDs.open;
  41. const label = 'Open Inspector';
  42. const namespace = 'inspector';
  43. const tracker = new InstanceTracker<InspectorPanel>({ namespace });
  44. /**
  45. * Create and track a new inspector.
  46. */
  47. function newInspectorPanel(): InspectorPanel {
  48. const inspector = new InspectorPanel();
  49. inspector.id = 'jp-inspector';
  50. inspector.title.label = 'Inspector';
  51. inspector.title.closable = true;
  52. inspector.disposed.connect(() => {
  53. if (manager.inspector === inspector) {
  54. manager.inspector = null;
  55. }
  56. });
  57. // Track the inspector.
  58. tracker.add(inspector);
  59. // Add the default inspector child items.
  60. Private.defaultInspectorItems.forEach(item => { inspector.add(item); });
  61. return inspector;
  62. }
  63. // Handle state restoration.
  64. restorer.restore(tracker, {
  65. command,
  66. args: () => null,
  67. name: () => 'inspector'
  68. });
  69. // Add command to registry and palette.
  70. commands.addCommand(command, {
  71. label,
  72. execute: () => {
  73. if (!manager.inspector || manager.inspector.isDisposed) {
  74. manager.inspector = newInspectorPanel();
  75. shell.addToMainArea(manager.inspector);
  76. }
  77. if (manager.inspector.isAttached) {
  78. shell.activateById(manager.inspector.id);
  79. }
  80. }
  81. });
  82. palette.addItem({ command, category });
  83. return manager;
  84. }
  85. };
  86. /**
  87. * An extension that registers consoles for inspection.
  88. */
  89. const consoles: JupyterLabPlugin<void> = {
  90. id: '@jupyterlab/inspector-extension:consoles',
  91. requires: [IInspector, IConsoleTracker],
  92. autoStart: true,
  93. activate: (app: JupyterLab, manager: IInspector, consoles: IConsoleTracker): void => {
  94. // Maintain association of new consoles with their respective handlers.
  95. const handlers: { [id: string]: InspectionHandler } = {};
  96. // Create a handler for each console that is created.
  97. consoles.widgetAdded.connect((sender, parent) => {
  98. const session = parent.console.session;
  99. const rendermime = parent.console.rendermime;
  100. const connector = new KernelConnector({ session });
  101. const handler = new InspectionHandler({ connector, rendermime });
  102. // Associate the handler to the widget.
  103. handlers[parent.id] = handler;
  104. // Set the initial editor.
  105. let cell = parent.console.promptCell;
  106. handler.editor = cell && cell.editor;
  107. // Listen for prompt creation.
  108. parent.console.promptCellCreated.connect((sender, cell) => {
  109. handler.editor = cell && cell.editor;
  110. });
  111. // Listen for parent disposal.
  112. parent.disposed.connect(() => {
  113. delete handlers[parent.id];
  114. handler.dispose();
  115. });
  116. });
  117. // Keep track of console instances and set inspector source.
  118. app.shell.currentChanged.connect((sender, args) => {
  119. let widget = args.newValue;
  120. if (!widget || !consoles.has(widget)) {
  121. return;
  122. }
  123. let source = handlers[widget.id];
  124. if (source) {
  125. manager.source = source;
  126. }
  127. });
  128. app.contextMenu.addItem({
  129. command: CommandIDs.open,
  130. selector: '.jp-CodeConsole-promptCell'
  131. });
  132. }
  133. };
  134. /**
  135. * An extension that registers notebooks for inspection.
  136. */
  137. const notebooks: JupyterLabPlugin<void> = {
  138. id: '@jupyterlab/inspector-extension:notebooks',
  139. requires: [IInspector, INotebookTracker],
  140. autoStart: true,
  141. activate: (app: JupyterLab, manager: IInspector, notebooks: INotebookTracker): void => {
  142. // Maintain association of new notebooks with their respective handlers.
  143. const handlers: { [id: string]: InspectionHandler } = {};
  144. // Create a handler for each notebook that is created.
  145. notebooks.widgetAdded.connect((sender, parent) => {
  146. const session = parent.session;
  147. const rendermime = parent.rendermime;
  148. const connector = new KernelConnector({ session });
  149. const handler = new InspectionHandler({ connector, rendermime });
  150. // Associate the handler to the widget.
  151. handlers[parent.id] = handler;
  152. // Set the initial editor.
  153. let cell = parent.notebook.activeCell;
  154. handler.editor = cell && cell.editor;
  155. // Listen for active cell changes.
  156. parent.notebook.activeCellChanged.connect((sender, cell) => {
  157. handler.editor = cell && cell.editor;
  158. });
  159. // Listen for parent disposal.
  160. parent.disposed.connect(() => {
  161. delete handlers[parent.id];
  162. handler.dispose();
  163. });
  164. });
  165. // Keep track of notebook instances and set inspector source.
  166. app.shell.currentChanged.connect((sender, args) => {
  167. let widget = args.newValue;
  168. if (!widget || !notebooks.has(widget)) {
  169. return;
  170. }
  171. let source = handlers[widget.id];
  172. if (source) {
  173. manager.source = source;
  174. }
  175. });
  176. app.contextMenu.addItem({
  177. command: CommandIDs.open,
  178. selector: '.jp-Notebook.jp-mod-focus'
  179. });
  180. }
  181. };
  182. /**
  183. * Export the plugins as default.
  184. */
  185. const plugins: JupyterLabPlugin<any>[] = [inspector, consoles, notebooks];
  186. export default plugins;
  187. /**
  188. * A namespace for private data.
  189. */
  190. namespace Private {
  191. /**
  192. * The default set of inspector items added to the inspector panel.
  193. */
  194. export
  195. const defaultInspectorItems: IInspector.IInspectorItem[] = [
  196. {
  197. className: 'jp-HintsInspectorItem',
  198. name: 'Hints',
  199. rank: 20,
  200. type: 'hints'
  201. }
  202. ];
  203. }