-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocessing.py
48 lines (34 loc) · 1.24 KB
/
preprocessing.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import logging
from io import StringIO
import nbformat
from nbformat.reader import NotJSONError
def is_valid_notebook(content):
"""
Check if the given content is a valid Jupyter notebook.
Args:
content (str): The content of the notebook.
Returns:
bool: True if the content is a valid notebook, False otherwise.
"""
try:
nbformat.reads(content, as_version=4)
return True
except NotJSONError:
return False
def keep_only_markdown_cells(notebook_content):
"""
Filters out only the markdown cells from a Jupyter Notebook.
Args:
notebook_content (str): The content of the Jupyter Notebook.
Returns:
str: The modified content of the Jupyter Notebook with only markdown cells.
"""
if not is_valid_notebook(notebook_content):
raise ValueError("The content is not a valid Jupyter Notebook")
notebook = nbformat.reads(notebook_content, as_version=4)
markdown_cells = [cell for cell in notebook.cells if cell.cell_type == 'markdown']
notebook.cells = markdown_cells
logging.info("Kept only markdown cells in Jupyter Notebook")
output_stream = StringIO()
nbformat.write(notebook, output_stream)
return output_stream.getvalue()