MyST-NB icon indicating copy to clipboard operation
MyST-NB copied to clipboard

Do not remove empty cells if they have outputs

Open choldgraf opened this issue 3 years ago • 0 comments

Describe the problem/need and solution

Context Currently, we remove the cell from the rendering process if it does not have any content in the input cell:

https://github.com/executablebooks/MyST-NB/blob/646224352ac045ee2475ac44e4f7a00ff19ae55e/myst_nb/parser.py#L152-L155

However, there are some use-cases where users want the cell to remain. One fairly common one is:

  • A teacher is creating notebooks for a class
  • They have a "solutions" notebook with code cells that generate outputs
  • They create a "student" version that strips the inputs of code, but keeps the outputs
  • They ask the students to fill in the code that generates the outputs they see
  • The book they build uses the student version of the notebooks

Currently, myst-nb will remove these cells, because it only checks whether there is input in the code cell. It would be useful if there were some way to either:

  • Explicitly tell MyST NB not to remove cells just because the inputs are empty (e.g. via a config)
  • Add a check for whether there are cell outputs present before deciding to skip the cell

ref: https://github.com/executablebooks/jupyter-book/issues/1258 where it was first mentioned

Guide for implementation

Perhaps the simplest solution would be to add an extra check like:

if len(nb_cell["source"].strip()) == 0 and len(nb_cell["outputs"]) == 0:
    continue

a more complex solution would be a new config value like nb_skip_empty_cells=True - though IMO ideally we could improve this use-case without growing our config surface area.

also cc @chrisjsewell as I'm not sure if this issue will be impacted at all by #380

Tasks and updates

No response

choldgraf avatar Jan 20 '22 18:01 choldgraf