Skip to content

Commit 7a667e4

Browse files
author
Jürgen Hasch
committed
Fix codefolding preprocessor configuration
1 parent c200197 commit 7a667e4

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/jupyter_contrib_nbextensions/nbconvert_support/pre_codefolding.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,33 @@
55
"""
66

77
from nbconvert.preprocessors import Preprocessor
8+
from traitlets import Bool, Unicode
89

910

1011
class CodeFoldingPreprocessor(Preprocessor):
1112
"""
1213
:mod:`nbconvert` Preprocessor for the code_folding nbextension.
1314
14-
Folds codecells as displayed in the notebook.
15+
Folds codecells as displayed in the notebook. The hidden code below the fold gets removed.
1516
1617
The preprocessor is installed by default. To enable codefolding with
1718
NbConvert, you need to set the configuration parameter
18-
`NbConvertApp.codefolding=True`.
19+
`CodeFoldingPreprocessor.remove_folded_code=True`.
1920
This can be done either in the `jupyter_nbconvert_config.py` file::
2021
21-
c.NbConvertApp.codefolding = True
22+
c.CodeFoldingPreprocessor.remove_folded_code=True = True
2223
2324
or using a command line parameter when calling NbConvert::
2425
25-
$ jupyter nbconvert --to html --NbConvertApp.codefolding=True mynotebook.ipynb
26+
$ jupyter nbconvert --to html --CodeFoldingPreprocessor.remove_folded_code=True mynotebook.ipynb
27+
28+
The folding mark can be configured using
29+
c.CodeFoldingPreprocessor.fold_mark = '<->'
2630
2731
""" # noqa: E501
2832

29-
fold_mark = u'↔'
33+
remove_folded_code = Bool(False, help="Remove code that was folded").tag(config=True)
34+
fold_mark = Unicode(u'↔', help="Symbol for folded code").tag(config=True)
3035

3136
def fold_cell(self, cell, folded):
3237
"""
@@ -69,10 +74,11 @@ def preprocess_cell(self, cell, resources, index):
6974
index : int
7075
Index of the cell being processed (see base.py)
7176
"""
72-
dofolding = self.config.NbConvertApp.get('codefolding', False) is True
73-
if hasattr(cell, 'source') and cell.cell_type == 'code' and dofolding:
74-
if hasattr(cell['metadata'], 'code_folding'):
75-
folded = cell['metadata']['code_folding']
76-
if len(folded) > 0:
77-
cell.source = self.fold_cell(cell.source, folded)
77+
if self.remove_folded_code:
78+
self.log.debug('Removing folded code in cell')
79+
if hasattr(cell, 'source') and cell.cell_type == 'code':
80+
if hasattr(cell['metadata'], 'code_folding'):
81+
folded = cell['metadata']['code_folding']
82+
if len(folded) > 0:
83+
cell.source = self.fold_cell(cell.source, folded)
7884
return cell, resources

0 commit comments

Comments
 (0)