Skip to content

Commit bccd908

Browse files
noklamjcb91
authored andcommitted
Go to current running cell (#1384)
* copy the template from other extension, edit README * add follow running cell, tested shortcut works * update README * add button * add gif demo, update icon * Update config, user can config shortcut, follow running cell with checkbox * update default button icon config * Change edit to command shortcut, remove debug log, use input instead of hotkey config * update README and compress gif * move variable to define scropt, remove timeout
1 parent 9cb30aa commit bccd908

File tree

7 files changed

+174
-0
lines changed

7 files changed

+174
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Go to Running Cell
2+
==================
3+
4+
This is an extension allows you to jump to the current running cell. You can also activate this functionality automatically, i.e., your view is always scolling to the current cell.
5+
6+
Button: A button with eye icon that you can go to the first running cell.
7+
![button](anchor.png)
8+
9+
Keyboard shortcuts:
10+
-------------------
11+
__*Alt-I*__ (Jump to first running cell)
12+
__*Meta-[*__ (Follow executing cell On)
13+
__*Meta-]*__(Follow executing cell Off)
14+
15+
Demo
16+
----
17+
### Jump to first running cell
18+
![button](jump_to_cell.gif)
19+
20+
### Follow executing cell
21+
22+
![button](auto_focus.gif)
676 Bytes
Loading
1.67 MB
Loading
722 Bytes
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Type: Jupyter Notebook Extension
2+
Name: Go to Current Running Cells
3+
Description: Go to Running cell and always scroll into current running cell view
4+
Link: README.md
5+
Main: main.js
6+
Compatibility: 4.x, 5.x
7+
8+
Parameters:
9+
- name: is_follow_cell
10+
description: Activate follow executing cells, default behavior is false.
11+
input_type: checkbox
12+
- name: go_to_running_cell_shortcut
13+
description: Go to first running cell
14+
input_type: input
15+
default: Alt-I
16+
- name: follow_cell_on_shortcut
17+
description: Enable following running cell
18+
input_type: input
19+
default: Alt-;
20+
- name: follow_cell_off_shortcut
21+
description: Disable following running cell
22+
input_type: input
23+
default: Alt-'
24+
- name: button_icon
25+
description: Button for go to first running cell
26+
default: fa-anchor
875 KB
Loading
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright (c) Jupyter-Contrib Team.
2+
// Distributed under the terms of the Modified BSD License.
3+
4+
// This is an extension allows you to jump to the current running cell.
5+
// You can also activate this functionality automatically,
6+
// i.e., your view is always scolling to the current cell.
7+
8+
//
9+
// Keyboard shortcuts: Alt-I and Alt-down (works with single cells also -- this is useful!)
10+
// The extension is simple, create function and then register the action and shortkey separately,
11+
// so that user can update the shortkey according to their need.
12+
13+
14+
15+
define([
16+
'base/js/namespace',
17+
'jquery',
18+
'require',
19+
'base/js/events'
20+
], function (Jupyter, $, requirejs, events) {
21+
"use strict";
22+
23+
var action_follow_cell_on; // set on registration
24+
var action_follow_cell_off; // set on registration
25+
var action_go_to_runing_cell; // set on registration
26+
var params = {
27+
is_follow_cell: false,
28+
go_to_running_cell_shortcut: 'Alt-I',
29+
follow_cell_on_shortcut: "Alt-;",
30+
follow_cell_off_shortcut: "Alt-'",
31+
button_icon: 'fa-anchor'
32+
};
33+
34+
function scrollIntoRunningCell(event, data) {
35+
$('.running')[0].scrollIntoView({ behavior: 'smooth', inline: 'center' });
36+
}
37+
38+
// update params with any specified in the server's config file
39+
var update_params = function () {
40+
var config = Jupyter.notebook.config;
41+
for (var key in params) {
42+
if (config.data.hasOwnProperty(key))
43+
params[key] = config.data[key];
44+
}
45+
};
46+
47+
// Go to Running cell shortcut
48+
function go_to_running_cell(event) {
49+
50+
// Find running cell and click the first one
51+
if ($('.running').length > 0) {
52+
$('.running')[0].scrollIntoView();
53+
}
54+
return false;
55+
}
56+
57+
function follow_running_cell_on(event) {
58+
Jupyter.notebook.events.on('finished_execute.CodeCell', scrollIntoRunningCell);
59+
return false;
60+
}
61+
62+
function follow_running_cell_off(event) {
63+
Jupyter.notebook.events.off('finished_execute.CodeCell', scrollIntoRunningCell);
64+
return false;
65+
}
66+
67+
// Register actions to collapse and uncollapse the selected heading cell
68+
69+
function register_new_actions() {
70+
action_go_to_runing_cell = Jupyter.keyboard_manager.actions.register({
71+
handler: go_to_running_cell,
72+
help: "Go to first executing cell",
73+
help_index: 'aa',
74+
icon: params.button_icon
75+
}, 'Go to first running cell', 'Go To Running Cell'
76+
)
77+
action_follow_cell_on = Jupyter.keyboard_manager.actions.register({
78+
handler: follow_running_cell_on,
79+
help: "Follow running cell on",
80+
help_index: 'aa'
81+
}, 'Follow running cell on', 'Go To Running Cell'
82+
)
83+
action_follow_cell_off = Jupyter.keyboard_manager.actions.register({
84+
handler: follow_running_cell_off,
85+
help: "Follow running cell off",
86+
help_index: 'aa'
87+
}, 'Follow running cell off', 'Go To Running Cell'
88+
);
89+
90+
if (params.is_follow_cell) {
91+
Jupyter.notebook.events.on('finished_execute.CodeCell', scrollIntoRunningCell);
92+
}
93+
}
94+
95+
// Register keyboard shortcuts according to parameters
96+
function register_keyboard_shortcuts() {
97+
98+
var shortcut, edit_shortcuts = Jupyter.keyboard_manager.command_shortcuts;
99+
shortcut = params.go_to_running_cell_shortcut;
100+
if (shortcut) {
101+
edit_shortcuts.add_shortcut(shortcut, action_go_to_runing_cell);
102+
}
103+
104+
shortcut = params.follow_cell_on_shortcut;
105+
if (shortcut) {
106+
edit_shortcuts.add_shortcut(shortcut, action_follow_cell_on);
107+
}
108+
109+
shortcut = params.follow_cell_off_shortcut;
110+
if (shortcut) {
111+
edit_shortcuts.add_shortcut(shortcut, action_follow_cell_off);
112+
}
113+
}
114+
115+
function load_ipython_extension() {
116+
update_params();
117+
register_new_actions();
118+
register_keyboard_shortcuts();
119+
Jupyter.toolbar.add_buttons_group([action_go_to_runing_cell])
120+
}
121+
122+
return {
123+
load_ipython_extension: load_ipython_extension,
124+
};
125+
126+
});

0 commit comments

Comments
 (0)