Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6629 implement gist publish feature for Lab #6635

Merged
merged 2 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions js/lab/src/plugin/gistPublish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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 * as $ from 'jquery';
import { NotebookPanel } from "@jupyterlab/notebook";
import { showDialog, Dialog, ToolbarButton } from '@jupyterlab/apputils';

const CONFIG = {
gistsUrl: 'https://api.github.com/gists',
nbviewerBaseUrl: 'https://nbviewer.jupyter.org/'
};

export function registerFeature(panel: NotebookPanel) {
addActionButton(panel);
}

export function addActionButton(panel: NotebookPanel): void {
const action = {
className: 'fa fa-share-alt',
tooltip: 'Publish...',
onClick: () => openPublishDialog(panel)
};

panel.toolbar.insertItem(9,'publish', new ToolbarButton(action));
}

function openPublishDialog(panel: NotebookPanel) {
showDialog({
title : 'Publish',
body : 'Publish to an anonymous Github Gist, and open in nbviewer?',
buttons: [
Dialog.okButton({ label: 'OK' }),
Dialog.cancelButton({ label: 'Cancel' })
]
})
.then(() => savePanelState(panel))
.then(() => doPublish(panel));
}

function savePanelState(panel: NotebookPanel): Promise<any> {
return new Promise((resolve, reject) => {
panel.context.save().then(() => {
console.log("widgets state has been saved");

if (!panel.isDisposed) {
resolve();
} else {
reject();
}
}, reject);
});
}

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

filedata[panel.context.contentsModel.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);
}

function showErrorDialog(errorMsg) {
showDialog({
title : 'Gist publication error',
body : `Uploading gist failed: ${errorMsg}`,
buttons: [ Dialog.okButton({ label: 'OK' }) ]
});
}
2 changes: 2 additions & 0 deletions js/lab/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { INotebookModel, NotebookPanel } from '@jupyterlab/notebook';
import { registerCommTargets } from './comm';
import { registerCommentOutCmd } from './codeEditor';
import { enableInitializationCellsFeature } from './initializationCells';
import { registerFeature as registerGistPublishFeature } from './gistPublish';

declare global {
interface Window {
Expand Down Expand Up @@ -52,6 +53,7 @@ class BeakerxExtension implements DocumentRegistry.WidgetExtension {

Promise.all([panel.ready, context.ready]).then(function() {
enableInitializationCellsFeature(panel);
registerGistPublishFeature(panel);
registerCommentOutCmd(panel);
registerCommTargets(panel, context);
});
Expand Down
2 changes: 1 addition & 1 deletion js/notebook/src/shared/style/beakerx.scss
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ $focusColor: #66bb6a;

}

.jp-Toolbar-button.fa-calculator {
.jp-Toolbar-button.fa {
line-height: 100%;
}