1

I'd like to do exacly what has been asked and answered in the post How can I hide code and rerun all cells in a jupyter notebook? but in JupyterLab. A while back, this just didn't seem to be possible. But after the release of v0.33 I believe it should be possible since:

Javascript execution in notebook cells has been re-enabled

But If i try to run the script below, the only thing that happens in JupyterLab is that this output is produced:

Output:

Button(description='Refresh', style=ButtonStyle())

Code:

from IPython.display import HTML

HTML('''<script>
  function code_toggle() {
    if (code_shown){
      $('div.input').hide('500');
      $('#toggleButton').val('Show code')
    } else {
      $('div.input').show('500');
      $('#toggleButton').val('Hide code')
    }
    code_shown = !code_shown
  }

  $( document ).ready(function(){
    code_shown=false;
    $('div.input').hide()
  });
</script>
<form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Show code"></form>''')

from IPython.display import Javascript, display
from ipywidgets import widgets

def run_all(ev):
    display(Javascript('IPython.notebook.execute_cells_below()'))

button = widgets.Button(description="Refresh")
button.on_click(run_all)
display(button)

So, does anyone know if this should be possible in JupyterLab?

vestland
  • 55,229
  • 37
  • 187
  • 305
  • 1
    might be wrong by I think you cannot run arbitrary javascript in a cell in JupyterLab for security reasons. It should be bundled into an extension. see https://github.com/jupyterlab/jupyterlab/issues/3748#issuecomment-361748326 – dzang Mar 05 '20 at 14:04
  • @dzang Thank you for responing. Do you have any idea how/if this functionality is possible in another way in JupyterLab? – vestland Mar 05 '20 at 14:09
  • 1
    no idea sorry. I googled for an extenstion, maybe this? https://github.com/jupyterlab/jupyterlab/tree/master/packages/javascript-extension see also https://www.npmjs.com/package/@jupyterlab/javascript-extension – dzang Mar 05 '20 at 14:14

1 Answers1

0

For JupyterLab >= 3.0, you can try using below library:

pip install jupyterlab-hide-code

More information

Prateek Sharma
  • 1,371
  • 13
  • 11