Would be nice if there was an option that automatically strips trailing whitespace.
@jakirkham Just to clarify... trailing cells at the end of a notebook or from markdown entered in a cell or something completely different.
Closing for now due to lack of info (trying to aggressively triage here...). Can reopen if more info comes along.
That's fine. It's a pretty minor point.
Sorry to not respond for so long, @willingc. I had to restructure my notifications around that time so I could keep track of things better. What I was meaning was trailing whitespace in notebook cells. PyCharm does something similar on saves and I was wondering if such a thing could be pursued with the notebook as well.
Could this feature be provided through the mean of an extension?
Yes, probably. On the server side, you could set up a save hook. And there is probably some way to do it in the JS side as well, but I'm less sure of how that would look.
following @takluyver's suggestion:
jupyter notebook --generate-config
Then add to the generated file:
def stripWS(t):
return '\n'.join([i.rstrip() for i in t.split('\n')])
def scrub_output_pre_save(model=None, **kwargs):
"""strip trailing space before saving"""
if model['type'] == 'notebook':
# only run on nbformat v4
if model['content']['nbformat'] != 4:
print("skipping WS stripping since `nbformat` != 4")
return
print("Stripping WS")
for cell in model['content']['cells']:
if cell['cell_type'] != 'code':
continue
cell['source'] = stripWS(cell['source'])
elif model['type'] == 'file':
if model['format'] == 'text':
print("Stripping WS")
model['content'] = stripWS(model['content'])
c.ContentsManager.pre_save_hook = scrub_output_pre_save
really odd that there isn't an editor option to do this
This should really be re-opened. The functionality of stripping trailing whitespace is pretty basic and expected in any tool that lets you write and edit code.
The ticket was originally closed for lack of information. Here is a precise description of what stripping trailing whitespace means:
Imagine the following python code in a Jupyter cell:
␣␣␣␣def␣foo(x):␣
␣␣␣␣␣␣␣␣print(x)␣␣␣
␣␣␣␣␣␣␣␣return␣1+x␣␣␣␣␣␣␣␣
Upon saving this code, it would automatically be reformatted to the following:
␣␣␣␣def␣foo(x):
␣␣␣␣␣␣␣␣print(x)
␣␣␣␣␣␣␣␣return␣1+x
To view whitespaces, they have been replaced with ␣
In the meantime, based on @casperdcl answer adding this to my ~/.jupyter/jupyter_notebook_config.py file seems to work:
#------------------------------------------------------------------------------
# Custom whitespace stripping
#------------------------------------------------------------------------------
def strip_white_space(text):
return '\n'.join([line.rstrip() for line in text.split('\n')])
def scrub_output_pre_save(model=None, **kwargs):
"""Auto strip trailing white space before saving."""
# If we are dealing with a notebook #
if model['type'] == 'notebook':
# Don't run on anything other than nbformat version 4 #
if model['content']['nbformat'] != 4:
print("Skipping white space stripping since `nbformat` != 4.")
return
# Apply function to every cell #
print("Stripping white space on a notebook.")
for cell in model['content']['cells']:
if cell['cell_type'] != 'code': continue
cell['source'] = strip_white_space(cell['source'])
# If we are dealing with a file #
if model['type'] == 'file':
if model['format'] == 'text':
print("Stripping white space on a file.")
model['content'] = strip_white_space(model['content'])
c.ContentsManager.pre_save_hook = scrub_output_pre_save
In the meantime, based on @casperdcl answer adding this to my
~/.jupyter/jupyter_notebook_config.pyfile seems to work:
An advantage of doing it on the server is that it automatically works for JupyterLab too then.
Most helpful comment
following @takluyver's suggestion:
Then add to the generated file: