forked from kirbs-/hide_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhide_code.js
More file actions
161 lines (143 loc) · 5.23 KB
/
Copy pathhide_code.js
File metadata and controls
161 lines (143 loc) · 5.23 KB
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
/* -*- coding: utf-8 -*-
* ----------------------------------------------------------------------------
* Copyright (c) 2015 - Chris Kirby
*
* Distributed under the terms of the MIT License.
*
* An IPython notebook extension to hide code inputs and prompts for presentation.
* -----------------------------------------------------------------------------
*/
define([
'jquery',
'notebook/js/celltoolbar'
],
function ($, celltoolbar){
"use strict";
var ctb = celltoolbar.CellToolbar;
function hideCodeSetter(cell, value){
if (cell.metadata.hideCode == undefined){ cell.metadata.hideCode = false }
cell.metadata.hideCode = value;
toggleHideCode(cell);
}
function hideCodeGetter(cell){
var isHidden = cell.metadata.hideCode;
toggleHideCode(cell);
return (isHidden == undefined) ? undefined : isHidden
}
function hidePromptSetter(cell, value){
if (cell.metadata.hidePrompt == undefined){ cell.metadata.hidePrompt = false }
cell.metadata.hidePrompt = value;
toggleHidePrompt(cell);
}
function hidePromptGetter(cell){
var isHidden = cell.metadata.hidePrompt;
toggleHidePrompt(cell);
return (isHidden == undefined) ? undefined : isHidden
}
function hideOutputSetter(cell, value){
if (cell.metadata.hideOutput == undefined){ cell.metadata.hideOutput = false }
cell.metadata.hideOutput = value;
toggleHideOutput(cell);
}
function hideOutputGetter(cell){
var isHidden = cell.metadata.hideOutput;
toggleHideOutput(cell);
return (isHidden == undefined) ? undefined : isHidden
}
function toggleHideCode(cell){
var c = $(cell.element);
if (cell.metadata.hideCode && cell.class_config.classname != 'MarkdownCell'){
c.find('.input_area').hide();
} else if(cell.class_config.classname != 'MarkdownCell') {
c.find('.input_area').show();
}
}
function toggleHidePrompt(cell){
var c = $(cell.element);
if (cell.metadata.hidePrompt && cell.class_config.classname != 'MarkdownCell'){
c.find('.prompt').css('visibility','hidden');
} else if(cell.class_config.classname != 'MarkdownCell') {
c.find('.prompt').css('visibility','visible');
}
}
function toggleHideOutput(cell){
var c = $(cell.element);
if (cell.metadata.hideOutput && cell.class_config.classname != 'MarkdownCell'){
c.find('.output_wrapper .output_subarea').css('display','none');
} else if(cell.class_config.classname != 'MarkdownCell') {
c.find('.output_wrapper .output_subarea').css('display','block');
}
}
/**
* Add a toolbar button to toggle visibility of all code cells, input/output prompts, and remove any highlighting for the selected cell.
**/
function addHideCodeButtonToToolbar(){
IPython.toolbar.add_buttons_group([
{
'label' : 'Hide/show code',
'icon' : 'fa-code',
'callback' : function() { // toggling visibility is adding display: block to the element. Causing celltoolbar = None not work.
$('.input').toggle();
$('.prompt').toggle();
var ctb = $('.ctb_hideshow');
if(ctb.hasClass('ctb_show')) {
ctb.removeClass('ctb_show');
} else {
ctb.addClass('ctb_show');
}
var ctb = $('.celltoolbar');
if(ctb.hasClass('invisible')){
console.log('visible');
ctb.removeClass('invisible');
} else {
console.log('invisible');
ctb.addClass('invisible');
}
// $('.celltoolbar').toggle();
$('.selected').removeClass('selected').addClass('unselected');
}
},
{
'label' : 'Export to HTML',
'icon' : 'fa-file-text-o',
'callback' : function (){
window.location = window.location.origin + window.location.pathname + "/export/html";
Jupyter.notebook.kernel.reconnect();
}
},
{
'label' : 'Export to PDF via HTML',
'icon' : 'fa-file-pdf-o',
'callback' : function (){
window.location = window.location.origin + window.location.pathname + "/export/pdf";
Jupyter.notebook.kernel.reconnect();
}
},
{
'label' : 'Export to PDF via Latex',
'icon' : 'fa-file-o',
'callback' : function (){
window.location = window.location.origin + window.location.pathname + "/export/latexpdf";
Jupyter.notebook.kernel.reconnect();
}
}
]);
}
var hideCodeCallback = ctb.utils.checkbox_ui_generator( 'Hide Code ', hideCodeSetter, hideCodeGetter);
var hidePromptCallback = ctb.utils.checkbox_ui_generator('Hide Prompts ', hidePromptSetter, hidePromptGetter);
var hideOutputCallback = ctb.utils.checkbox_ui_generator('Hide Outputs ', hideOutputSetter, hideOutputGetter);
function setup(){
ctb.register_callback('hide_code.hideCode', hideCodeCallback);
ctb.register_callback('hide_code.hidePrompts', hidePromptCallback);
ctb.register_callback('hide_code.hideOutputs', hideOutputCallback);
ctb.register_preset('Hide code',['hide_code.hidePrompts','hide_code.hideCode','hide_code.hideOutputs']);
addHideCodeButtonToToolbar();
$.each(Jupyter.notebook.get_cells(), function(index, cell){
toggleHidePrompt(cell);
toggleHideCode(cell);
toggleHideOutput(cell);
});
}
setup();
console.log('hide_code setup complete');
});