panel.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. ISessionContext,
  5. SessionContext,
  6. sessionContextDialogs
  7. } from '@jupyterlab/apputils';
  8. import { IEditorMimeTypeService } from '@jupyterlab/codeeditor';
  9. import { PathExt, Time } from '@jupyterlab/coreutils';
  10. import { UUID } from '@lumino/coreutils';
  11. import {
  12. IRenderMimeRegistry,
  13. RenderMimeRegistry
  14. } from '@jupyterlab/rendermime';
  15. import { ServiceManager } from '@jupyterlab/services';
  16. import { Token } from '@lumino/coreutils';
  17. import { Message } from '@lumino/messaging';
  18. import { Panel } from '@lumino/widgets';
  19. import { CodeConsole } from './widget';
  20. import { IDisposable } from '@lumino/disposable';
  21. /**
  22. * The class name added to console panels.
  23. */
  24. const PANEL_CLASS = 'jp-ConsolePanel';
  25. const CONSOLE_ICON_CLASS = 'jp-CodeConsoleIcon';
  26. /**
  27. * A panel which contains a console and the ability to add other children.
  28. */
  29. export class ConsolePanel extends Panel {
  30. /**
  31. * Construct a console panel.
  32. */
  33. constructor(options: ConsolePanel.IOptions) {
  34. super();
  35. this.addClass(PANEL_CLASS);
  36. let {
  37. rendermime,
  38. mimeTypeService,
  39. path,
  40. basePath,
  41. name,
  42. manager,
  43. modelFactory
  44. } = options;
  45. let contentFactory = (this.contentFactory =
  46. options.contentFactory || ConsolePanel.defaultContentFactory);
  47. let count = Private.count++;
  48. if (!path) {
  49. path = `${basePath || ''}/console-${count}-${UUID.uuid4()}`;
  50. }
  51. let sessionContext = (this._sessionContext = new SessionContext({
  52. sessionManager: manager.sessions,
  53. specsManager: manager.kernelspecs,
  54. path,
  55. name: name || `Console ${count}`,
  56. type: 'console',
  57. kernelPreference: options.kernelPreference,
  58. setBusy: options.setBusy
  59. }));
  60. let resolver = new RenderMimeRegistry.UrlResolver({
  61. session: sessionContext,
  62. contents: manager.contents
  63. });
  64. rendermime = rendermime.clone({ resolver });
  65. this.console = contentFactory.createConsole({
  66. rendermime,
  67. sessionContext: sessionContext,
  68. mimeTypeService,
  69. contentFactory,
  70. modelFactory
  71. });
  72. this.addWidget(this.console);
  73. void sessionContext.initialize().then(async value => {
  74. if (value) {
  75. await sessionContextDialogs.selectKernel(sessionContext);
  76. }
  77. this._connected = new Date();
  78. this._updateTitle();
  79. });
  80. this.console.executed.connect(this._onExecuted, this);
  81. this._updateTitle();
  82. sessionContext.kernelChanged.connect(this._updateTitle, this);
  83. sessionContext.propertyChanged.connect(this._updateTitle, this);
  84. this.title.icon = CONSOLE_ICON_CLASS;
  85. this.title.closable = true;
  86. this.id = `console-${count}`;
  87. }
  88. /**
  89. * The content factory used by the console panel.
  90. */
  91. readonly contentFactory: ConsolePanel.IContentFactory;
  92. /**
  93. * The console widget used by the panel.
  94. */
  95. readonly console: CodeConsole;
  96. /**
  97. * The session used by the panel.
  98. */
  99. get sessionContext(): ISessionContext {
  100. return this._sessionContext;
  101. }
  102. /**
  103. * Dispose of the resources held by the widget.
  104. */
  105. dispose(): void {
  106. this.sessionContext.dispose();
  107. this.console.dispose();
  108. super.dispose();
  109. }
  110. /**
  111. * Handle `'activate-request'` messages.
  112. */
  113. protected onActivateRequest(msg: Message): void {
  114. let prompt = this.console.promptCell;
  115. if (prompt) {
  116. prompt.editor.focus();
  117. }
  118. }
  119. /**
  120. * Handle `'close-request'` messages.
  121. */
  122. protected onCloseRequest(msg: Message): void {
  123. super.onCloseRequest(msg);
  124. this.dispose();
  125. }
  126. /**
  127. * Handle a console execution.
  128. */
  129. private _onExecuted(sender: CodeConsole, args: Date) {
  130. this._executed = args;
  131. this._updateTitle();
  132. }
  133. /**
  134. * Update the console panel title.
  135. */
  136. private _updateTitle(): void {
  137. Private.updateTitle(this, this._connected, this._executed);
  138. }
  139. private _executed: Date | null = null;
  140. private _connected: Date | null = null;
  141. private _sessionContext: SessionContext;
  142. }
  143. /**
  144. * A namespace for ConsolePanel statics.
  145. */
  146. export namespace ConsolePanel {
  147. /**
  148. * The initialization options for a console panel.
  149. */
  150. export interface IOptions {
  151. /**
  152. * The rendermime instance used by the panel.
  153. */
  154. rendermime: IRenderMimeRegistry;
  155. /**
  156. * The content factory for the panel.
  157. */
  158. contentFactory: IContentFactory;
  159. /**
  160. * The service manager used by the panel.
  161. */
  162. manager: ServiceManager.IManager;
  163. /**
  164. * The path of an existing console.
  165. */
  166. path?: string;
  167. /**
  168. * The base path for a new console.
  169. */
  170. basePath?: string;
  171. /**
  172. * The name of the console.
  173. */
  174. name?: string;
  175. /**
  176. * A kernel preference.
  177. */
  178. kernelPreference?: ISessionContext.IKernelPreference;
  179. /**
  180. * The model factory for the console widget.
  181. */
  182. modelFactory?: CodeConsole.IModelFactory;
  183. /**
  184. * The service used to look up mime types.
  185. */
  186. mimeTypeService: IEditorMimeTypeService;
  187. /**
  188. * A function to call when the kernel is busy.
  189. */
  190. setBusy?: () => IDisposable;
  191. }
  192. /**
  193. * The console panel renderer.
  194. */
  195. export interface IContentFactory extends CodeConsole.IContentFactory {
  196. /**
  197. * Create a new console panel.
  198. */
  199. createConsole(options: CodeConsole.IOptions): CodeConsole;
  200. }
  201. /**
  202. * Default implementation of `IContentFactory`.
  203. */
  204. export class ContentFactory extends CodeConsole.ContentFactory
  205. implements IContentFactory {
  206. /**
  207. * Create a new console panel.
  208. */
  209. createConsole(options: CodeConsole.IOptions): CodeConsole {
  210. return new CodeConsole(options);
  211. }
  212. }
  213. /**
  214. * A namespace for the console panel content factory.
  215. */
  216. export namespace ContentFactory {
  217. /**
  218. * Options for the code console content factory.
  219. */
  220. export interface IOptions extends CodeConsole.ContentFactory.IOptions {}
  221. }
  222. /**
  223. * A default code console content factory.
  224. */
  225. export const defaultContentFactory: IContentFactory = new ContentFactory();
  226. /* tslint:disable */
  227. /**
  228. * The console renderer token.
  229. */
  230. export const IContentFactory = new Token<IContentFactory>(
  231. '@jupyterlab/console:IContentFactory'
  232. );
  233. /* tslint:enable */
  234. }
  235. /**
  236. * A namespace for private data.
  237. */
  238. namespace Private {
  239. /**
  240. * The counter for new consoles.
  241. */
  242. export let count = 1;
  243. /**
  244. * Update the title of a console panel.
  245. */
  246. export function updateTitle(
  247. panel: ConsolePanel,
  248. connected: Date | null,
  249. executed: Date | null
  250. ) {
  251. let sessionContext = panel.console.sessionContext.session;
  252. if (sessionContext) {
  253. let caption =
  254. `Name: ${sessionContext.name}\n` +
  255. `Directory: ${PathExt.dirname(sessionContext.path)}\n` +
  256. `Kernel: ${panel.console.sessionContext.kernelDisplayName}`;
  257. if (connected) {
  258. caption += `\nConnected: ${Time.format(connected.toISOString())}`;
  259. }
  260. if (executed) {
  261. caption += `\nLast Execution: ${Time.format(executed.toISOString())}`;
  262. }
  263. panel.title.label = sessionContext.name;
  264. panel.title.caption = caption;
  265. } else {
  266. panel.title.label = 'Console';
  267. panel.title.caption = '';
  268. }
  269. }
  270. }