Skip to content

Commit

Permalink
#6586 show error dialog on gist publishing error (#6628)
Browse files Browse the repository at this point in the history
* #6586 show error dialog on gist publishing error

* #6586 move gistPublish to extension dir

* #66586 show the network error in gist publishing dialog
  • Loading branch information
Mariusz Jurowicz authored and Mariusz Jurowicz committed Jan 30, 2018
1 parent 5140729 commit 7b9e66b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 97 deletions.
95 changes: 0 additions & 95 deletions js/notebook/src/GistPublish.js

This file was deleted.

4 changes: 3 additions & 1 deletion js/notebook/src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ define([
'./plot/plotApi',
'./shared/bkCoreManager',
'big.js',
'./GistPublish.js'
'./extension/GistPublish'
], function(
configmod,
comm,
Expand All @@ -75,6 +75,8 @@ define([
var commUtils = require('./extension/comm');
var initCellUtils = require('./extension/initializationCells');

GistPublish.registerFeature();

function installKernelHandler() {
var kernel = Jupyter.notebook.kernel;
if (!window.beakerx) {
Expand Down
115 changes: 115 additions & 0 deletions js/notebook/src/extension/GistPublish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import $ from 'jquery';

const dialog = require('base/js/dialog');
const CONFIG = {
gistsUrl: 'https://api.github.com/gists',
nbviewerBaseUrl: 'https://nbviewer.jupyter.org/'
};

export function registerFeature(): void {
if (!!Jupyter.NotebookList) {
return;
}

Jupyter.toolbar.add_buttons_group([{
'label' : 'Publish...',
'icon' : 'fa-share-alt',
'callback': beforePublish
}]);

const publish_menu = $('<li>')
.attr('id', 'publish_gist')
.append($('<a>')
.attr('href', '#')
.html('Publish...'));

publish_menu.insertAfter($('#print_preview'));
publish_menu.click(beforePublish);
}

function beforePublish(): void {
dialog.modal({
title : 'Publish',
body : 'Publish to an anonymous Github Gist, and open in nbviewer?',
buttons: {
'OK': {
'class' : 'btn-primary',
'click': function() {
saveWidgetsState().then(doPublish);
}
},
'Cancel': {}
}
});
}

function showErrorDialog(errorMsg) {
dialog.modal({
title : 'Gist publication error',
body : `Uploading gist failed: ${errorMsg}`,
buttons: {
'OK': {
'class' : 'btn-primary'
}
}
});
}

function saveWidgetsState(): Promise<any> {
return new Promise((resolve, reject) => {
if (Jupyter.menubar.actions.exists('widgets:save-with-widgets')) {
Jupyter.menubar.actions.call('widgets:save-with-widgets');
console.log("widgets state has been saved");

setTimeout(resolve);
} else {
reject('widgets:save-with-widgets actions is not registered');
}
});
}

function doPublish(): void {
const nbjson = Jupyter.notebook.toJSON();
const filedata = {};

filedata[Jupyter.notebook.notebook_name] = {
content : JSON.stringify(nbjson, undefined, 1)
};

const settings = {
type : 'POST',
headers : {},
data : JSON.stringify({
public : true,
files : filedata
}),
success : (data, status) => {
console.log("gist successfully published: " + data.id);
window.open(CONFIG.nbviewerBaseUrl + data.id);
},
error : (jqXHR, status, err) => {
const errorMsg = jqXHR.readyState === 0 && !err ? 'NETWORK ERROR!' : err;

console.log(errorMsg);
showErrorDialog(errorMsg);
}
};

$.ajax(CONFIG.gistsUrl, settings);
}
3 changes: 2 additions & 1 deletion js/notebook/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"target": "es5",
"allowJs": true,
"moduleResolution": "Node",
"noEmit": true
"noEmit": true,
"lib": ["dom", "es5", "es2015.promise"]
},
"include": [
"./src/**/*.ts"
Expand Down

0 comments on commit 7b9e66b

Please sign in to comment.