jupyter/nbconvert

Notebook to script does not respect leading whitespace

drblarg opened this issue · 0 comments

I am using nbconvert as a package vs command line to convert a notebook to a script using ScriptExporter class in python:

contents, meta = nbconvert.exporters.export(
    nbconvert.exporters.ScriptExporter,
    notebook
)

When the contents of a notebook cell are all indented by the same amount, this intention is removed in the output. This whitespace should not be removed. Is there already a way to enforce this that I am missing?

A use case for keeping the white space is to enclose multiple output cells within some scope, such as an if statement. For example:

In[1]
if is_true:

In[2]
    do_stuff1()

In[3]
   do_stuff2()

This allows greater flexibility in using the notebook as a notebook (where you choose the cells to run), and the generated script as a script (e.g. AWS pipelines) without putting scoping statements in every cell.