Skip to content

Instantly share code, notes, and snippets.

@juhasch
Last active January 3, 2016 04:19
Show Gist options
  • Save juhasch/8408061 to your computer and use it in GitHub Desktop.
Save juhasch/8408061 to your computer and use it in GitHub Desktop.
My custom.js for the new IPython notebook 2.0 UI
// leave at least 2 line with only a star on it below, or doc generation fails
/**
*
*
*/
/*
* Wait for app_initialized.NotebookApp trigger
*/
$([IPython.events]).on('app_initialized.NotebookApp', function(){
/* load extensions */
require(['/static/custom/breakpoints.js'])
require(['/static/custom/comment.js'])
require(['/static/custom/shift-tab.js'])
require(['/static/custom/printview_button.js'])
require(['/static/custom/codefolding/codefolding.js'])
/* add custom shortcuts */
var add_command_shortcuts = {
'esc' : {
help : 'edit mode',
help_index : 'aa',
handler : function (event) {
IPython.notebook.edit_mode();
return false;
}
},
'home' : {
help : 'go to top',
help_index : 'ga',
handler : function (event) {
IPython.notebook.select(0);
IPython.notebook.scroll_to_top()
return false;
}
},
'end' : {
help : 'go to bottom',
help_index : 'ga',
handler : function (event) {
var ncells = IPython.notebook.ncells();
IPython.notebook.select(ncells-1);
IPython.notebook.scroll_to_bottom()
return false;
}
},
'pageup' : {
help : 'page up',
help_index : 'aa',
handler : function (event) {
var wh = 0.6 * $(window).height();
var cell = IPython.notebook.get_selected_cell();
var h = 0;
/* loop until we have enough cells to span the size of the notebook window (= one page) */
do {
h += cell.element.height();
IPython.notebook.select_prev();
cell = IPython.notebook.get_selected_cell();
} while ( h < wh )
cell.focus_cell();
return false;
}
},
'pagedown' : {
help : 'page down',
help_index : 'aa',
handler : function (event) {
/* jump to bottom if we are already in the last cell */
var ncells = IPython.notebook.ncells();
if ( IPython.notebook.get_selected_index()+1 == ncells) {
IPython.notebook.scroll_to_bottom();
return false;
}
var wh = 0.6*$(window).height();
var cell = IPython.notebook.get_selected_cell();
var h = 0;
/* loop until we have enough cells to span the size of the notebook window (= one page) */
do {
h += cell.element.height();
IPython.notebook.select_next();
cell = IPython.notebook.get_selected_cell();
} while ( h < wh )
cell.focus_cell();
return false;
}
},
'ctrl+end' : {
help : 'run from current cell to end',
help_index : 'xy',
handler : function (event) {
IPython.notebook.execute_cells_below();
return false;
}
},
};
IPython.keyboard_manager.command_shortcuts.add_shortcuts(add_command_shortcuts);
var add_edit_shortcuts = {
'alt+plus' : {
help : 'split cell',
help_index : 'ea',
handler : function (event) {
IPython.notebook.split_cell();
IPython.notebook.edit_mode();
return false;
}
},
'alt+add' : {
help : 'split cell',
help_index : 'eb',
handler : function (event) {
IPython.notebook.split_cell();
IPython.notebook.edit_mode();
return false;
}
},
'alt+-' : {
help : 'merge cell',
help_index : 'ea',
handler : function (event) {
var i = IPython.notebook.get_selected_index();
if (i > 0) {
var c = IPython.notebook.get_cell(i-1);
var l = c.code_mirror.lineCount();
IPython.notebook.merge_cell_above();
var c = IPython.notebook.get_selected_cell();
c.code_mirror.setCursor(l,0);
}
return false;
}
},
'alt+subtract' : {
help : 'merge cell',
help_index : 'eb',
handler : function (event) {
var i = IPython.notebook.get_selected_index();
if (i > 0) {
var c = IPython.notebook.get_cell(i-1);
var l = c.code_mirror.lineCount();
IPython.notebook.merge_cell_above();
var c = IPython.notebook.get_selected_cell();
c.code_mirror.setCursor(l,0);
}
}
},
'shift+enter' : {
help : 'run cell',
help_index : 'ba',
handler : function (event) {
IPython.notebook.execute_cell();
var cell = IPython.notebook.get_selected_cell();
if ((cell instanceof IPython.CodeCell)) {
IPython.notebook.edit_mode();
}
return false;
}
},
'ctrl+enter' : {
help : 'run cell, select next codecell',
help_index : 'bb',
handler : function (event) {
IPython.notebook.execute_cell_and_select_below();
// find next CodeCell and go into edit mode if possible, else stay in next cell
for (var i = IPython.notebook.get_selected_index(); i < IPython.notebook.ncells() ;i++) {
var cell = IPython.notebook.get_cell(i);
if (cell instanceof IPython.CodeCell) {
IPython.notebook.select(i);
IPython.notebook.edit_mode();
break;
}
}
return false;
}
},
'ctrl+home' : {
help : 'go to top',
help_index : 'ga',
handler : function (event) {
IPython.notebook.select(0);
IPython.notebook.scroll_to_top()
return false;
}
},
'ctrl+end' : {
help : 'go to bottom',
help_index : 'ga',
handler : function (event) {
var ncells = IPython.notebook.ncells();
IPython.notebook.select(ncells-1);
IPython.notebook.scroll_to_bottom()
return false;
}
},
'alt+n' : {
help : 'toggle line numbers',
help_index : 'xy',
handler : function (event) {
var cell = IPython.notebook.get_selected_cell();
cell.toggle_line_numbers();
return false;
}
},
};
IPython.keyboard_manager.edit_shortcuts.add_shortcuts(add_edit_shortcuts);
});
/* override clipboard 'paste' and insert new cell from json data in clipboard
* works only with chrome
*/
window.addEventListener('paste', function(event){
var cell = IPython.notebook.get_selected_cell();
if (cell.mode == "command" ) {
event.preventDefault();
var data = event.clipboardData.getData('application/json');
var new_cell_data = JSON.parse(data);
var new_cell = IPython.notebook.insert_cell_below(new_cell_data.cell_type);
new_cell.fromJSON(new_cell_data);
}
});
/* override clipboard 'copy' and copy current cell as json and text to clipboard
* works only with chrome
*/
window.addEventListener('copy', function(event){
var cell = IPython.notebook.get_selected_cell();
if (cell.mode == "command") {
event.preventDefault();
var j = cell.toJSON();
var json = JSON.stringify(j);
var text = cell.code_mirror.getValue();
event.clipboardData.setData('application/json',json);
event.clipboardData.setData("Text", text);
}
});
/* override clipboard 'cut' and copy current cell as json and text to clipboard
* works only with chrome
*/
window.addEventListener('cut', function(event){
var cell = IPython.notebook.get_selected_cell();
if (cell.mode == "command" ) {
event.preventDefault();
var j = cell.toJSON();
var json = JSON.stringify(j);
var data = event.clipboardData.setData('application/json',json);
var str = cell.code_mirror.getValue();
event.clipboardData.setData("Text", str);
IPython.notebook.delete_cell(IPython.notebook.find_cell_index(cell));
}
});
/* Scroll notebook using the mouse wheel */
function MouseWheelHandler(e) {
var cell = IPython.notebook.get_selected_cell();
if (cell.mode == "command" ) {
e.preventDefault();
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
if (delta == -1) {
IPython.notebook.select_next();
var ncells = IPython.notebook.ncells();
if ( IPython.notebook.get_selected_index()+1 == ncells) {
IPython.notebook.scroll_to_bottom();
}
} else {
IPython.notebook.select_prev();
}
cell = IPython.notebook.get_selected_cell();
cell.focus_cell();
}
return false;
}
var mywindow = window;
if (mywindow.addEventListener) {
mywindow.addEventListener("mousewheel", MouseWheelHandler, false); // IE9, Chrome, Safari, Opera
mywindow.addEventListener("DOMMouseScroll", MouseWheelHandler, false); // Firefox
}
else mywindow.attachEvent("onmousewheel", MouseWheelHandler); // IE 6/7/8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment