На данный момент jupyter не предоставляет такую функциональность по умолчанию.Тем не менее, вы можете вручную удалить пустые строки и комментарии из файла Python, используя несколько строк кода, например
def process(filename):
"""Removes empty lines and lines that contain only whitespace, and
lines with comments"""
with open(filename) as in_file, open(filename, 'r+') as out_file:
for line in in_file:
if not line.strip().startswith("#") and not line.isspace():
out_file.writelines(line)
Теперь просто вызовите эту функцию в файле Python, который вы конвертировали из блокнота jupyter.
process('test.py')
Кроме того, если вы хотите, чтобы одна служебная функция конвертировала блокнот jupyter в файл python, в котором нет комментариев и пустых строк, вы можете включить приведенный выше код в предложенную ниже функцию здесь :
import nbformat
from nbconvert import PythonExporter
def convertNotebook(notebookPath, out_file):
with open(notebookPath) as fh:
nb = nbformat.reads(fh.read(), nbformat.NO_CONVERT)
exporter = PythonExporter()
source, meta = exporter.from_notebook_node(nb)
with open(out_file, 'w+') as out_file:
out_file.writelines(source)
# include above `process` code here with proper modification