Skip to content

Commit

Permalink
Merge pull request #6288 from fcollonval/6274-nb-update-config
Browse files Browse the repository at this point in the history
Refractor updating notebook settings for opened widgets
  • Loading branch information
blink1073 committed May 1, 2019
2 parents 760463c + f881c5b commit 98c82e6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 25 deletions.
47 changes: 23 additions & 24 deletions packages/notebook-extension/src/index.ts
Expand Up @@ -517,9 +517,7 @@ function activateNotebookHandler(
settingRegistry: ISettingRegistry | null
): INotebookTracker {
const services = app.serviceManager;
// An object for tracking the current notebook settings.
let editorConfig = StaticNotebook.defaultEditorConfig;
let notebookConfig = StaticNotebook.defaultNotebookConfig;

const factory = new NotebookWidgetFactory({
name: FACTORY,
fileTypes: ['notebook'],
Expand All @@ -529,8 +527,8 @@ function activateNotebookHandler(
canStartKernel: true,
rendermime: rendermime,
contentFactory,
editorConfig,
notebookConfig,
editorConfig: StaticNotebook.defaultEditorConfig,
notebookConfig: StaticNotebook.defaultNotebookConfig,
mimeTypeService: editorServices.mimeTypeService
});
const { commands } = app;
Expand Down Expand Up @@ -583,6 +581,15 @@ function activateNotebookHandler(
void tracker.add(widget);
});

/**
* Update the settings of the current tracker.
*/
function updateTracker(options: NotebookPanel.IConfig): void {
tracker.forEach(widget => {
widget.setConfig(options);
});
}

/**
* Update the setting values.
*/
Expand Down Expand Up @@ -617,27 +624,17 @@ function activateNotebookHandler(
? raw[key]
: cached[key];
});
factory.editorConfig = editorConfig = { code, markdown, raw };
factory.notebookConfig = notebookConfig = {
factory.editorConfig = { code, markdown, raw };
factory.notebookConfig = {
scrollPastEnd: settings.get('scrollPastEnd').composite as boolean
};
factory.shutdownOnClose = settings.get('kernelShutdown')
.composite as boolean;
}

/**
* Update the settings of the current tracker.
*/
function updateTracker(settings: ISettingRegistry.ISettings | null): void {
tracker.forEach(widget => {
widget.content.editorConfig = editorConfig;
widget.content.notebookConfig = notebookConfig;
// Update kernel shutdown behavior
const kernelPreference = widget.context.session.kernelPreference;
widget.context.session.kernelPreference = {
...kernelPreference,
shutdownOnClose: factory.shutdownOnClose
};
updateTracker({
editorConfig: factory.editorConfig,
notebookConfig: factory.notebookConfig,
kernelShutdown: factory.shutdownOnClose
});
}

Expand All @@ -649,15 +646,17 @@ function activateNotebookHandler(
.then(() => fetchSettings)
.then(settings => {
updateConfig(settings);
updateTracker(settings);
settings.changed.connect(() => {
updateConfig(settings);
updateTracker(settings);
});
})
.catch((reason: Error) => {
console.warn(reason.message);
updateTracker(null);
updateTracker({
editorConfig: factory.editorConfig,
notebookConfig: factory.notebookConfig,
kernelShutdown: factory.shutdownOnClose
});
});

// Add main menu notebook menu.
Expand Down
36 changes: 35 additions & 1 deletion packages/notebook/src/panel.ts
Expand Up @@ -17,7 +17,7 @@ import { RenderMimeRegistry } from '@jupyterlab/rendermime';

import { INotebookModel } from './model';

import { Notebook } from './widget';
import { Notebook, StaticNotebook } from './widget';

/**
* The class name added to notebook panels.
Expand Down Expand Up @@ -108,6 +108,22 @@ export class NotebookPanel extends DocumentWidget<Notebook, INotebookModel> {
return this.content ? this.content.model : null;
}

/**
* Update the options for the current notebook panel.
*
* @param config new options to set
*/
setConfig(config: NotebookPanel.IConfig): void {
this.content.editorConfig = config.editorConfig;
this.content.notebookConfig = config.notebookConfig;
// Update kernel shutdown behavior
const kernelPreference = this.context.session.kernelPreference;
this.context.session.kernelPreference = {
...kernelPreference,
shutdownOnClose: config.kernelShutdown
};
}

/**
* Set URI fragment identifier.
*/
Expand Down Expand Up @@ -184,6 +200,24 @@ export class NotebookPanel extends DocumentWidget<Notebook, INotebookModel> {
* A namespace for `NotebookPanel` statics.
*/
export namespace NotebookPanel {
/**
* Notebook config interface for NotebookPanel
*/
export interface IConfig {
/**
* A config object for cell editors
*/
editorConfig: StaticNotebook.IEditorConfig;
/**
* A config object for notebook widget
*/
notebookConfig: StaticNotebook.INotebookConfig;
/**
* Whether to shut down the kernel when closing the panel or not
*/
kernelShutdown: boolean;
}

/**
* A content factory interface for NotebookPanel.
*/
Expand Down

0 comments on commit 98c82e6

Please sign in to comment.