-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
25 lines (20 loc) · 834 Bytes
/
convert.py
File metadata and controls
25 lines (20 loc) · 834 Bytes
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
import nbconvert.exporters
import nbconvert.preprocessors
import nbconvert.writers
TAG_CONVERT_MODULE = "convert-module"
NAME_MODULE = "transformer"
class ConvertModulePreprocessor(nbconvert.preprocessors.Preprocessor):
def preprocess(self, nb, resources):
'''Select only cells with correct tag.'''
nb.cells = [
cell for cell in nb.cells
if TAG_CONVERT_MODULE in cell["metadata"].get("tags", [])
]
return nb, resources
if __name__ == "__main__":
exporter = nbconvert.exporters.PythonExporter()
exporter.exclude_input_prompt = True
exporter.register_preprocessor(ConvertModulePreprocessor(), True)
(output, resources) = exporter.from_filename("project.ipynb")
writer = nbconvert.writers.FilesWriter()
writer.write(output, resources, NAME_MODULE)