Skip to content

Commit fa2189a

Browse files
authored
Merge pull request #1370 from jasongrout/fixsaving
Fix widget saving in the classic notebook.
2 parents c991830 + c059166 commit fa2189a

File tree

1 file changed

+22
-54
lines changed

1 file changed

+22
-54
lines changed

widgetsnbextension/src/manager.js

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ var WidgetManager = function (comm_manager, notebook) {
2424
this.keyboard_manager = notebook.keyboard_manager;
2525
this.comm_manager = comm_manager;
2626

27+
var widget_md = notebook.metadata.widgets
28+
if (widget_md && widget_md['application/vnd.jupyter.widget-state+json']) {
29+
this.set_state(notebook.metadata.widgets['application/vnd.jupyter.widget-state+json'])
30+
}
31+
2732
// Register with the comm manager.
2833
this.comm_manager.register_target(this.comm_target_name, _.bind(this.handle_comm_open,this));
2934

@@ -79,66 +84,13 @@ var WidgetManager = function (comm_manager, notebook) {
7984
});
8085
});
8186

82-
// Setup state saving code.
83-
this.notebook.events.on('before_save.Notebook', (function() {
84-
var save_callback = WidgetManager._save_callback;
85-
if (save_callback) {
86-
this.get_state(WidgetManager._get_state_options).then((function(state) {
87-
save_callback.call(this, state);
88-
}).bind(this)).catch(widgets.reject('Could not call widget save state callback.', true));
89-
}
90-
}).bind(this));
91-
9287
// Create the actions and menu
9388
this._init_actions();
9489
this._init_menu();
9590
};
9691

9792
WidgetManager.prototype = Object.create(widgets.ManagerBase.prototype);
9893
WidgetManager._managers = []; /* List of widget managers */
99-
WidgetManager._load_callback = null;
100-
WidgetManager._save_callback = null;
101-
102-
WidgetManager.set_state_callbacks = function (load_callback, save_callback, options) {
103-
/**
104-
* Registers callbacks for widget state persistence.
105-
*
106-
* Parameters
107-
* ----------
108-
* load_callback: function()
109-
* function that is called when the widget manager state should be
110-
* loaded. This function should return a promise for the widget
111-
* manager state. An empty state is an empty dictionary `{}`.
112-
* save_callback: function(state as dictionary)
113-
* function that is called when the notebook is saved or autosaved.
114-
* The current state of the widget manager is passed in as the first
115-
* argument.
116-
*/
117-
WidgetManager._load_callback = load_callback;
118-
WidgetManager._save_callback = save_callback;
119-
WidgetManager._get_state_options = options || {};
120-
121-
// Use the load callback to immediately load widget states.
122-
WidgetManager._managers.forEach(function(manager) {
123-
if (load_callback) {
124-
Promise.resolve().then(function () {
125-
return load_callback.call(manager);
126-
}).then(function(state) {
127-
manager.set_state(state);
128-
}).catch(widgets.reject('Error loading widget manager state', true));
129-
}
130-
});
131-
};
132-
133-
var url = [window.location.protocol, '//', window.location.host, window.location.pathname].join('');
134-
var key = 'widgets:' + url;
135-
WidgetManager.set_state_callbacks(function() {
136-
if (Jupyter.notebook.metadata.widgets) {
137-
return Promise.resolve(Jupyter.notebook.metadata.widgets.state);
138-
} else {
139-
return Promise.resolve({});
140-
}
141-
});
14294

14395
WidgetManager.prototype.loadClass = function(className, moduleName, moduleVersion) {
14496
if (moduleName === "jupyter-js-widgets") {
@@ -182,6 +134,17 @@ WidgetManager.prototype._init_actions = function() {
182134
help: 'Save the notebook with the widget state information for static rendering'
183135
};
184136
Jupyter.menubar.actions.register(this.saveWidgetsAction, 'save-with-widgets', 'widgets');
137+
138+
this.clearWidgetsAction = {
139+
handler: (function() {
140+
delete Jupyter.notebook.metadata.widgets;
141+
Jupyter.menubar.actions.get('jupyter-notebook:save-notebook').handler({
142+
notebook: Jupyter.notebook
143+
});
144+
}),
145+
help: 'Clear the widget state information from the notebook'
146+
};
147+
Jupyter.menubar.actions.register(this.saveWidgetsAction, 'save-clear-widgets', 'widgets');
185148
};
186149

187150
/**
@@ -207,7 +170,12 @@ WidgetManager.prototype._init_menu = function() {
207170
widgetsSubmenu.classList.add('dropdown-menu');
208171
widgetsMenu.appendChild(widgetsSubmenu);
209172

210-
widgetsSubmenu.appendChild(this._createMenuItem('Save Notebook with Widgets', this.saveWidgetsAction));
173+
var divider = document.createElement('ul');
174+
divider.classList.add('divider');
175+
176+
widgetsSubmenu.appendChild(this._createMenuItem('Save Notebook Widget State', this.saveWidgetsAction));
177+
widgetsSubmenu.appendChild(this._createMenuItem('Clear Notebook Widget State', this.clearWidgetsAction));
178+
widgetsSubmenu.appendChild(divider);
211179
widgetsSubmenu.appendChild(this._createMenuItem('Download Widget State', saveState.action));
212180
widgetsSubmenu.appendChild(this._createMenuItem('Embed Widgets', embedWidgets.action));
213181
};

0 commit comments

Comments
 (0)