From 66653e5ce94009ff99d89f3d3e1f8b039df11bca Mon Sep 17 00:00:00 2001 From: kirbycm Date: Sun, 12 Feb 2017 17:39:05 -0500 Subject: [PATCH 01/13] Added support for nbextensions. Resolves # 27. --- hide_code/hide_code.py | 15 +++++++++++++++ hide_code/hide_code_html_exporter.py | 3 --- setup.py | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/hide_code/hide_code.py b/hide_code/hide_code.py index 9db2850..84860a2 100644 --- a/hide_code/hide_code.py +++ b/hide_code/hide_code.py @@ -62,6 +62,21 @@ def get(self, *args): def __main__(): install() +def _jupyter_nbextension_paths(): + return [dict( + section="notebook", + # the path is relative to the `my_fancy_module` directory + src="", + # directory in the `nbextension/` namespace + dest="hide_code", + # _also_ in the `nbextension/` namespace + require="hide_code/hide_code")] + +def _jupyter_server_extension_paths(): + return [{ + "module": "hide_code" + }] + def load_jupyter_server_extension(nb_app): nb_app.log.info("hide_code: Attempting to load hid_code export handler extensions.") web_app = nb_app.web_app diff --git a/hide_code/hide_code_html_exporter.py b/hide_code/hide_code_html_exporter.py index 105f220..ff0107d 100644 --- a/hide_code/hide_code_html_exporter.py +++ b/hide_code/hide_code_html_exporter.py @@ -13,9 +13,6 @@ def __init__(self, config=None, **kw): self.preprocessors = ['hide_code.HideCodePreprocessor'] self._init_preprocessors() - # def _default_template_path_default(self): - # return "/Users/ckirby/.virtualenvs/hide_code/lib/python2.7/site-packages/hide_code/Templates" - def _template_file_default(self): return 'hide_code' diff --git a/setup.py b/setup.py index 1ac1b02..bb49a0b 100644 --- a/setup.py +++ b/setup.py @@ -40,7 +40,7 @@ def run(self): # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/single_source_version.html - version='0.3.1', + version='0.4.0', description='A Jupyter notebook extension to hide code, prompts and outputs.', long_description=long_description, From f419eb97cfb0cebc9a94d78687cbe858de8579f5 Mon Sep 17 00:00:00 2001 From: kirbycm Date: Mon, 13 Feb 2017 20:45:20 -0500 Subject: [PATCH 02/13] Added keyboard shortcut to hide/show selected cells code. --- hide_code/hide_code.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/hide_code/hide_code.js b/hide_code/hide_code.js index 1ffc89c..4c2b542 100644 --- a/hide_code/hide_code.js +++ b/hide_code/hide_code.js @@ -10,9 +10,10 @@ define([ 'jquery', -'notebook/js/celltoolbar' +'notebook/js/celltoolbar', +'notebook/js/keyboard_manager' ], -function ($, celltoolbar){ +function ($, celltoolbar, keyboard_manager){ "use strict"; var ctb = celltoolbar.CellToolbar; @@ -149,6 +150,11 @@ function ($, celltoolbar){ ctb.register_callback('hide_code.hideOutputs', hideOutputCallback); ctb.register_preset('Hide code',['hide_code.hidePrompts','hide_code.hideCode','hide_code.hideOutputs']); addHideCodeButtonToToolbar(); + // Add keyboard shortcuts + keyboard_manager.command_shortcuts.add_shortcut('ctrl-q', toggleHideCode); + keyboard_manager.command_shortcuts.add_shortcut('ctrl-w', toggleHidePrompt); + keyboard_manager.command_shortcuts.add_shortcut('ctrl-e', toggleHideOutput); + $.each(Jupyter.notebook.get_cells(), function(index, cell){ toggleHidePrompt(cell); toggleHideCode(cell); From 2fba1d8363c341c78b6fa638d2d24136fa6ed7cd Mon Sep 17 00:00:00 2001 From: kirbycm Date: Tue, 14 Feb 2017 21:07:47 -0500 Subject: [PATCH 03/13] Finished keyboard shortcuts. Resolves # 32. --- hide_code/hide_code.js | 136 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 130 insertions(+), 6 deletions(-) diff --git a/hide_code/hide_code.js b/hide_code/hide_code.js index 4c2b542..ea036a9 100644 --- a/hide_code/hide_code.js +++ b/hide_code/hide_code.js @@ -11,9 +11,9 @@ define([ 'jquery', 'notebook/js/celltoolbar', -'notebook/js/keyboard_manager' +'base/js/namespace' ], -function ($, celltoolbar, keyboard_manager){ +function ($, celltoolbar, Jupyter){ "use strict"; var ctb = celltoolbar.CellToolbar; @@ -30,6 +30,11 @@ function ($, celltoolbar, keyboard_manager){ return (isHidden == undefined) ? undefined : isHidden } + /* + * Sets notebook cell's hide code metadata. + * @param {Notebook Cell} cell + * @param {Boolean} value + */ function hidePromptSetter(cell, value){ if (cell.metadata.hidePrompt == undefined){ cell.metadata.hidePrompt = false } cell.metadata.hidePrompt = value; @@ -144,16 +149,135 @@ function ($, celltoolbar, keyboard_manager){ var hideOutputCallback = ctb.utils.checkbox_ui_generator('Hide Outputs ', hideOutputSetter, hideOutputGetter); + var hideCodeKeyboardAction = Jupyter.keyboard_manager.actions.register({ + help: 'Hides selected cell\'s code', + icon: 'fa-code', + help_index: '', + handler: function(env){ + var cells = env.notebook.get_selected_cells(); + $.each(cells, function(index, value){ + hideCodeSetter(cells[index], true); + try{ + $($(Jupyter.notebook.get_selected_cell().celltoolbar.inner_element[0]).find('input')[1]).prop('checked', true); + } catch(err){ + //do nothing + } + }); + }, + }, 'hide_code_action','hide_code'); + + /** + * Keyboard short cut action to hide selected cells. + * Binds to W + **/ + var showCodeKeyboardAction = Jupyter.keyboard_manager.actions.register({ + help: 'Shows selected cell\'s code', + icon: 'fa-code', + help_index: '', + handler: function(env){ + var cells = env.notebook.get_selected_cells(); + $.each(cells, function(index, value){ + hideCodeSetter(cells[index], false); + try{ + $($(Jupyter.notebook.get_selected_cell().celltoolbar.inner_element[0]).find('input')[1]).prop('checked', false); + } catch(err){ + //do nothing + } + }); + }, + }, 'show_code_action','hide_code'); + + var hidePromptKeyboardAction = Jupyter.keyboard_manager.actions.register({ + help: 'Hides selected cell\'s prompts.', + icon: 'fa-code', + help_index: '', + handler: function(env){ + var cells = env.notebook.get_selected_cells(); + $.each(cells, function(index, value){ + hidePromptSetter(cells[index], true);try{ + $($(Jupyter.notebook.get_selected_cell().celltoolbar.inner_element[0]).find('input')[0]).prop('checked', true); + } catch(err){ + //do nothing + } + }); + }, + }, 'hide_prompt_action','hide_code'); + + /** + * Keyboard short cut action to hide selected cells. + * Binds to W,W + **/ + var showPromptKeyboardAction = Jupyter.keyboard_manager.actions.register({ + help: 'Shows selected cell\'s prompts.', + icon: 'fa-code', + help_index: '', + handler: function(env){ + var cells = env.notebook.get_selected_cells(); + $.each(cells, function(index, value){ + hidePromptSetter(cells[index], false); + try{ + $($(Jupyter.notebook.get_selected_cell().celltoolbar.inner_element[0]).find('input')[0]).prop('checked', false) + } catch(err){ + //do nothing + }; + }); + }, + }, 'show_prompt_action','hide_code'); + + var hideOutputKeyboardAction = Jupyter.keyboard_manager.actions.register({ + help: 'Hides selected cell\'s prompts.', + icon: 'fa-code', + help_index: '', + handler: function(env){ + var cells = env.notebook.get_selected_cells(); + $.each(cells, function(index, value){ + hideOutputSetter(cells[index], true); + try{ + $($(Jupyter.notebook.get_selected_cell().celltoolbar.inner_element[0]).find('input')[2]).prop('checked', true); + } catch(err){ + //do nothing + } + }); + }, + }, 'hide_output_action','hide_code'); + + /** + * Keyboard short cut action to hide selected cells. + * Binds to W,W + **/ + var showOutputKeyboardAction = Jupyter.keyboard_manager.actions.register({ + help: 'Shows selected cell\'s prompts.', + icon: 'fa-code', + help_index: '', + handler: function(env){ + var cells = env.notebook.get_selected_cells(); + $.each(cells, function(index, value){ + hideOutputSetter(cells[index], false); + try{ + $($(Jupyter.notebook.get_selected_cell().celltoolbar.inner_element[0]).find('input')[2]).prop('checked', false); + } catch(err){ + //do nothing + } + }); + }, + }, 'show_output_action','hide_code'); + + function addKeyboardShortcutBindings(){ + Jupyter.keyboard_manager.command_shortcuts.add_shortcut('e', 'hide_code:hide_code_action', 'hide_code'); + Jupyter.keyboard_manager.command_shortcuts.add_shortcut('shift-e', 'hide_code:show_code_action', 'hide_code'); + Jupyter.keyboard_manager.command_shortcuts.add_shortcut('w', 'hide_code:hide_prompt_action', 'hide_code'); + Jupyter.keyboard_manager.command_shortcuts.add_shortcut('shift-w', 'hide_code:show_prompt_action', 'hide_code'); + Jupyter.keyboard_manager.command_shortcuts.add_shortcut('r', 'hide_code:hide_output_action', 'hide_code'); + Jupyter.keyboard_manager.command_shortcuts.add_shortcut('shift-r', 'hide_code:show_output_action', 'hide_code'); + } + 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(); - // Add keyboard shortcuts - keyboard_manager.command_shortcuts.add_shortcut('ctrl-q', toggleHideCode); - keyboard_manager.command_shortcuts.add_shortcut('ctrl-w', toggleHidePrompt); - keyboard_manager.command_shortcuts.add_shortcut('ctrl-e', toggleHideOutput); + addKeyboardShortcutBindings(); $.each(Jupyter.notebook.get_cells(), function(index, cell){ toggleHidePrompt(cell); From 40046e3614882da23eba7abca30adc905032e06b Mon Sep 17 00:00:00 2001 From: kirbycm Date: Mon, 20 Feb 2017 10:11:12 -0500 Subject: [PATCH 04/13] Added menubar and menubar item constructors. Added Hide Code menubar item. --- hide_code/hide_code.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/hide_code/hide_code.js b/hide_code/hide_code.js index ea036a9..b2cce60 100644 --- a/hide_code/hide_code.js +++ b/hide_code/hide_code.js @@ -271,6 +271,37 @@ function ($, celltoolbar, Jupyter){ Jupyter.keyboard_manager.command_shortcuts.add_shortcut('shift-r', 'hide_code:show_output_action', 'hide_code'); } + function addHideCodeMenuItem(){ + var menu = getMenuBar() + menu.append(menuItem('Hide Code','#')); + + getHideCodeMenu().append(dropdownMenuItem('PDF Export', '#', 'HTML PDF exporter.')); + } + + function getMenuBar(){ + return $('#menus').find('.navbar-nav') + } + + function getHideCodeMenu(){ + return $('#hide_code_menu_list') + } + + function dropdownMenuItem(text, url, tooltip){ + var item = $('
  • ', {title: tooltip, }); + var link = $('', {href: url, text: text}); + item.append(link); + return item + } + + function menuItem(text, url){ + var menu_item = $('
  • ', {class: 'dropdown', id: 'hide_code_menu_item'}); + var link = $('', {class: 'dropdown-toggle', href: url, text: text, 'data-toggle': 'dropdown'}); + var dropdown_menu = $('