/
preprocess.py
executable file
路34 lines (28 loc) 路 1.13 KB
/
preprocess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from nbconvert.preprocessors import Preprocessor
from nbformat import NotebookNode
from os.path import join
class RemoveExerciceCells(Preprocessor):
def preprocess(self, notebook, resources):
executable_cells = []
for cell in notebook.cells:
if cell.cell_type == 'markdown':
exerc_idx = cell.source.find('## Exerc')
if exerc_idx != -1:
if exerc_idx > 0:
cell.source = cell.source[:exerc_idx]
break
elif cell.cell_type == 'code':
if not cell.source:
continue
executable_cells.append(cell)
notebook.cells = executable_cells
return notebook, resources
class AddBinderComponent(Preprocessor):
def preprocess(self, notebook, resources):
filepath = join(resources['metadata']['path'], resources['metadata']['name']) + '.ipynb'
notebook.cells.append(NotebookNode(
cell_type='markdown',
metadata={},
source=f'<Binder filepath="{filepath}" />'
))
return notebook, resources