Quiero mostrar/ocultar solo una celda determinada/con un botón.
¿Cómo necesito refactorizar el código que funciona solo para una celda determinada? Encontré esta pregunta ¿Cómo ocultar el código de las celdas en el cuaderno de ipython visualizado con nbviewer? pero el fragmento de código solo funciona para todas las celdas. Quiero un botón como el siguiente código. (El segundo fragmento de código funciona perfectamente para una celda determinada, pero es solo con un texto y no con un botón)
import ipywidgets as widgets from IPython.display import display, HTML javascript_functions = {False: "hide()", True: "show()"} button_descriptions = {False: "Show code", True: "Hide code"} def toggle_code(state): """ Toggles the JavaScript show()/hide() function on the div.input element. """ output_string = "<script>$(\"div.input\").{}</script>" output_args = (javascript_functions[state],) output = output_string.format(*output_args) display(HTML(output)) def button_action(value): """ Calls the toggle_code function and updates the button description. """ state = value.new toggle_code(state) value.owner.description = button_descriptions[state] state = False toggle_code(state) button = widgets.ToggleButton(state, description = button_descriptions[state]) button.observe(button_action, "value") display(button) Este código funciona para una celda determinada donde puse el método hide_toggle()
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) #hide_toggle()