Skip to content

Commit 7552bff

Browse files
committed
[exercise,exercise2] make buttons & hotkeys configurable
1 parent 39049a6 commit 7552bff

File tree

4 files changed

+86
-8
lines changed

4 files changed

+86
-8
lines changed
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
Type: IPython Notebook Extension
1+
Type: Jupyter Notebook Extension
22
Name: Exercise
3-
Description: Exercise - Define a group of cells as a "solution". Then it is possible to hide/show these solutions cells by clicking on a cell widget.
3+
Description: |
4+
Define a group of cells as an "exercise".
5+
The first cell is the question,
6+
while the rest of the group from the answer or solution.
7+
The solution can be hidden/shown by clicking on a widget added to the
8+
question cell.
49
Link: readme.md
510
Icon: icon.png
611
Main: main.js
712
Compatibility: 4.x, 5.x
13+
Parameters:
14+
15+
- name: add_button
16+
description: Add a toolbar button to create/remove an exercise
17+
input_type: checkbox
18+
default: true
19+
20+
- name: use_hotkey
21+
description: Add a keyboard shortcut to create/remove an exercise
22+
input_type: checkbox
23+
default: true
24+
25+
- name: hotkey
26+
description: Keyboard shortcut optionally used to create/remove an exercise
27+
input_type: hotkey
28+
default: 'Alt-D'

src/jupyter_contrib_nbextensions/nbextensions/exercise/main.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ define([
3030
], function(IPython, $, requirejs, events) {
3131
"use strict";
3232

33+
var cfg = {
34+
add_button: true,
35+
use_hotkey: true,
36+
hotkey: 'Alt-S',
37+
};
38+
3339
/**
3440
* handle click event
3541
*
@@ -141,8 +147,20 @@ define([
141147
handler : create_remove_exercise
142148
}, 'create_remove_exercise', 'exercise');
143149

144-
IPython.toolbar.add_buttons_group([action_name]);
145-
IPython.keyboard_manager.command_shortcuts.add_shortcuts({'Alt-S': action_name}});
150+
IPython.notebook.config.loaded.then(function() {
151+
$.extend(true, cfg, IPython.notebook.config.data);
152+
153+
if (cfg.add_button) {
154+
IPython.toolbar.add_buttons_group([action_name]);
155+
}
156+
if (cfg.use_hotkey && cfg.hotkey) {
157+
var cmd_shrts = {};
158+
cmd_shrts[cfg.hotkey] = action_name;
159+
IPython.keyboard_manager.command_shortcuts.add_shortcuts(cmd_shrts);
160+
}
161+
}).catch(function(err) {
162+
console.warn('[exercise] error:', err);
163+
});
146164
}
147165

148166
return {
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
Type: IPython Notebook Extension
1+
Type: Jupyter Notebook Extension
22
Name: Exercise2
3-
Description: Exercise2 - Define a group of cells as a "solution". Then it is possible to hide/show these solutions cells by clicking on a cell widget.
3+
Description: |
4+
Define a group of cells as an "exercise".
5+
The first cell is the question,
6+
while the rest of the group from the answer or solution.
7+
The solution can be hidden/shown by clicking on a widget added to the
8+
question cell.
49
Link: readme.md
510
Icon: icon.png
611
Main: main.js
712
Compatibility: 4.x, 5.x
13+
Parameters:
14+
15+
- name: add_button
16+
description: Add a toolbar button to create/remove an exercise
17+
input_type: checkbox
18+
default: true
19+
20+
- name: use_hotkey
21+
description: Add a keyboard shortcut to create/remove an exercise
22+
input_type: checkbox
23+
default: true
24+
25+
- name: hotkey
26+
description: Keyboard shortcut optionally used to create/remove an exercise
27+
input_type: hotkey
28+
default: 'Alt-D'

src/jupyter_contrib_nbextensions/nbextensions/exercise2/main.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ define([
1616
], function(IPython, $, requirejs, events) {
1717
"use strict";
1818

19+
var cfg = {
20+
add_button: true,
21+
use_hotkey: true,
22+
hotkey: 'Alt-D',
23+
};
24+
1925
/**
2026
* handle click event
2127
*
@@ -141,8 +147,20 @@ define([
141147
handler : create_remove_exercise,
142148
}, 'create_remove_exercise', 'exercise2');
143149

144-
IPython.toolbar.add_buttons_group([action_name]);
145-
IPython.keyboard_manager.command_shortcuts.add_shortcuts({'Alt-D': action_name}});
150+
return IPython.notebook.config.loaded.then(function() {
151+
$.extend(true, cfg, IPython.notebook.config.data.exercise2);
152+
153+
if (cfg.add_button) {
154+
IPython.toolbar.add_buttons_group([action_name]);
155+
}
156+
if (cfg.use_hotkey && cfg.hotkey) {
157+
var cmd_shrts = {};
158+
cmd_shrts[cfg.hotkey] = action_name;
159+
IPython.keyboard_manager.command_shortcuts.add_shortcuts(cmd_shrts);
160+
}
161+
}).catch(function(err) {
162+
console.warn('[exercise2] error:', err);
163+
});
146164
}
147165

148166
return {

0 commit comments

Comments
 (0)