Skip to content

Instantly share code, notes, and snippets.

@mattharrison
Created May 31, 2019 20:06
Show Gist options
  • Save mattharrison/593df99fdd0e16831f03f091e2e6848c to your computer and use it in GitHub Desktop.
Save mattharrison/593df99fdd0e16831f03f091e2e6848c to your computer and use it in GitHub Desktop.
Jupyter cell to create shortcut (ctl-l) that toggles between scrolling to top of cell and output of cell
%%javascript
var selectedCell = 0;
var viewOutput = false;
Jupyter.keyboard_manager.command_shortcuts.add_shortcut('ctrl-l', function (event) {
function getOutputScrollValue(cell) {
var percent = 0;
var ct = cell.output_area.element.offset().top;
var sme = Jupyter.notebook.scroll_manager.element;
var h = sme.height();
var st = sme.scrollTop();
var t = sme.offset().top;
var scroll_value = st + ct - (t + 0.01 * percent * h);
return scroll_value;
}
var index = Jupyter.notebook.get_selected_index();
var ms = 500;
var cell = Jupyter.notebook.get_cell(index);
var outputtop = getOutputScrollValue(cell);
if (index === selectedCell) {
if (viewOutput) {
// scroll to output
Jupyter.notebook.scroll_manager.element.animate({scrollTop:outputtop}, ms);
}
else {
Jupyter.notebook.scroll_cell_percent(index, 0, ms);
}
}
else {
viewOutput = false;
Jupyter.notebook.scroll_cell_percent(index, 0, ms);
}
viewOutput = !viewOutput;
selectedCell = index;
Jupyter.OutputArea.auto_scroll_threshold = 999; // default 100
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment