forked from JoaoFelipe/minicurso-mineracao-interativa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slide.py
205 lines (180 loc) · 6.17 KB
/
slide.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from IPython.display import display, Javascript
JAVASCRIPT = """
var TITLE_PREFIX = "Mineração Interativa - " + IPython.notebook.get_notebook_name()
function scrollToCell(cell) {
$('#site').animate({
scrollTop: cell.element.position().top
}, 500);
}
function scrollToSelected() {
var cell = IPython.notebook.get_selected_cell()
scrollToCell(cell);
cell.focus_cell();
}
function selectCell(index, execute) {
var cells = IPython.notebook.get_cells();
IPython.notebook.select(index);
var cell = IPython.notebook.get_selected_cell();
cell.element.toggle(true);
var rx = /# skip-execution/g;
var match = rx.exec(cell.get_text());
if (cell.cell_type == "code" && match == undefined && execute) {
cell.execute();
}
if (cell.cell_type == "markdown") {
var header = $(cell.get_rendered()).filter("h1, h2, h3, h4, h5, h6");
if (header.length > 0) {
var text = $(header[header.length - 1]).clone().children().remove().end().text();
$("#current-title").text(TITLE_PREFIX + " - " + text);
} else {
header = $(cell.get_rendered()).find("h1, h2, h3, h4, h5, h6");
if (header.length > 0) {
var text = $(header[header.length - 1]).clone().children().remove().end().text();
$("#current-title").text(TITLE_PREFIX + " - " + text);
}
}
}
var scrollIndex = undefined;
rx = /<span class=\"notebook-slide-no-scroll\"\/>/g;
match = rx.exec(cell.get_text());
if (match == undefined && cell.cell_type == "markdown") {
scrollIndex = index;
rx = /<span class=\"notebook-slide-scroll\" data-position=\"(.*)?\"\/>/g;
match = rx.exec(cell.get_text());
if (match != undefined) {
var n = parseInt(match[1]);
scrollIndex += n;
}
scrollToCell(cells[scrollIndex]);
}
rx = /<span class=\"notebook-slide-extra\" data-count=\"(.*)?\"\/>/g;
match = rx.exec(cell.get_text());
if (match != undefined) {
var n = parseInt(match[1]);
for (var i = index + 1; i <= index + n; i++) {
cell = cells[i];
cell.element.toggle(true);
}
}
$("#current-slide").text((index + 1) + "/" + (cells.length));
}
function startSlideMode(found) {
var cells = IPython.notebook.get_cells();
var i = 0;
for (var cell of cells) {
cell.element.toggle(false);
}
for (i = 0; i <= found; i++) {
var cell = cells[i];
cell.element.toggle(true);
}
var cell = cells[cells.length - 1];
cell.element.toggle(true);
$(".navbar-nav").toggle(false);
$("#slide-top").remove();
$(".navbar-collapse").append("<div id='slide-top' style='padding: 6px 20px; text-align: center;'></div>")
$("#slide-top").append("<div style='float: left;' id='current-slide'>1</div>")
$("#slide-top").append("<div style='display: inline-block; padding: 0 20px;' id='current-title'>"+ TITLE_PREFIX +"</div>")
$("#slide-top").append("<div style='display: inline-block; padding: 0 20px; float: right;' id='current-name'>João Felipe Pimentel</div>")
selectCell(found, false);
document.documentElement.requestFullscreen();
}
var hide = {
icon: 'fa-eye-slash',
help : 'Hide all',
help_index : 'zz',
handler : function () {
var found = 0;
var i = 0;
for (var cell of IPython.notebook.get_cells()) {
var rx = /<span class=\"notebook-slide-start\"\/>/g;
var match = rx.exec(cell.get_text());
if (match) {
found = i;
}
i += 1;
}
startSlideMode(found);
}
};
var startCurrent = {
icon: 'fa-eye-slash',
help : 'Start slide mode at current cell',
help_index : 'zz',
handler : function () {
startSlideMode(IPython.notebook.get_selected_index());
}
};
var show = {
icon: 'fa-eye',
help : 'Show all',
help_index : 'zz',
handler : function () {
for (var cell of IPython.notebook.get_cells()) {
cell.element.toggle(true);
}
$("#slide-top").remove();
$(".navbar-nav").toggle(true);
document.exitFullscreen();
}
};
var view = {
icon: 'fa-eye',
help : 'Show cell',
help_index : 'zz',
handler : function () {
var index = IPython.notebook.get_selected_index();
selectCell(index, false);
IPython.notebook.select(index);
IPython.notebook.get_selected_cell().element.toggle(true);
scrollToSelected();
}
};
var previous = {
icon: 'fa-arrow-left',
help : 'Previous slide',
help_index : 'zz',
handler : function () {
var cell = IPython.notebook.get_selected_cell();
cell.element.toggle(false);
var index = IPython.notebook.get_selected_index();
if (cell.cell_type == "markdown") {
selectCell(index - 1, false);
}
IPython.notebook.select(index - 1);
IPython.notebook.get_selected_cell().element.toggle(true);
scrollToSelected();
}
};
var next = {
icon: 'fa-arrow-right',
help : 'Previous slide',
help_index : 'zz',
handler : function () {
var index = IPython.notebook.get_selected_index();
selectCell(index + 1, true);
}
};
var prefix = 'slide-notebook';
var actions = Jupyter.actions
IPython.notebook.keyboard_manager.command_shortcuts.add_shortcut(
'[,[', actions.register(previous, 'previous-slide', prefix)
)
IPython.notebook.keyboard_manager.command_shortcuts.add_shortcut(
']', actions.register(next, 'next-slide', prefix)
)
IPython.notebook.keyboard_manager.command_shortcuts.add_shortcut(
'-', actions.register(hide, 'hide-all', prefix)
)
IPython.notebook.keyboard_manager.command_shortcuts.add_shortcut(
'=', actions.register(show, 'show-all', prefix)
)
IPython.notebook.keyboard_manager.command_shortcuts.add_shortcut(
'[,]', actions.register(view, 'show-cell', prefix)
)
IPython.notebook.keyboard_manager.command_shortcuts.add_shortcut(
'0', actions.register(startCurrent, 'start-current', prefix)
)
"""
def load_ipython_extension(shell):
display(Javascript(JAVASCRIPT))