Created
November 22, 2019 12:54
-
-
Save MITsVision/87e40236fca1e8e64f4252dc36023ffc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##### 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
easy way to hide each cell in jupyter notebook