opensafely / primary-care-covid-codes-research

0 stars 1 forks source link

hide code chunks #2

Open wjchulme opened 4 years ago

wjchulme commented 4 years ago

Hide code chunks so the notebook displays only outputted text figures and tables. If possible, have a toggle to hide/show the code so that it's there for anybody interested.

lots of options here but haven't found something that works when rendering on github https://stackoverflow.com/questions/27934885/how-to-hide-code-from-cells-in-ipython-notebook-visualized-with-nbviewer

wjchulme commented 3 years ago

Progress so far:

When running locally, adding the code above to the top of a notebook in a 'Raw' cell is working as expected if you export the notebook as html. "Working" = the code blocks are not shown by default and clicking the 'show code' button makes them appear. See https://github.com/opensafely/primary-care-covid-codes-research/blob/2073ff99b2e70c4146e716724a9c63a06c4cabc9/notebooks/html/primary-care-covid-codes.html (may have to download and open).

However, it doesn't render properly on gitHub, see https://github.com/opensafely/primary-care-covid-codes-research/blob/5d09f5f7fcf69b3b05c98e9bdaa23a2ca33a5d88/notebooks/primary-care-covid-codes.ipynb.

When running on the server, the export to html option doesn't work. The 'show code' button is diplayed when you open the html file, but the code blocks are there by default and clicking the button doesn't do anything. The ipynb file is the same, so as before it doesn't render properly on GitHub.

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

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

source: https://stackoverflow.com/a/47017746/4269699