Skip to content

Instantly share code, notes, and snippets.

@MITsVision
Created November 22, 2019 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MITsVision/87e40236fca1e8e64f4252dc36023ffc to your computer and use it in GitHub Desktop.
Save MITsVision/87e40236fca1e8e64f4252dc36023ffc to your computer and use it in GitHub Desktop.
##### add at the first cell of the jupyter notebook
##### call using hide_toggle() functin
from IPython.display import HTML
import random
def hide_toggle(for_next=False):
this_cell = """$('div.cell.code_cell.rendered.selected')"""
next_cell = this_cell + '.next()'
toggle_text = 'Toggle show/hide' # text shown on toggle link
target_cell = this_cell # target cell to control with toggle
js_hide_current = '' # bit of JS to permanently hide code in current cell (only when toggling next cell)
if for_next:
target_cell = next_cell
toggle_text += ' next cell'
js_hide_current = this_cell + '.find("div.input").hide();'
js_f_name = 'code_toggle_{}'.format(str(random.randint(1,2**64)))
html = """
<script>
function {f_name}() {{
{cell_selector}.find('div.input').toggle();
}}
{js_hide_current}
</script>
<a href="javascript:{f_name}()">{toggle_text}</a>
""".format(
f_name=js_f_name,
cell_selector=target_cell,
js_hide_current=js_hide_current,
toggle_text=toggle_text
)
return HTML(html)
@MITsVision
Copy link
Author

easy way to hide each cell in jupyter notebook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment