-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_utils.py
33 lines (23 loc) · 856 Bytes
/
_utils.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
import datetime
import socket
from nbconvert import NotebookExporter
from traitlets.config import Config
__all__ = ["clear_notebook", "get_free_port", "isotime"]
def get_free_port():
"""Return a free port number."""
sock = socket.socket()
sock.bind(("", 0))
return sock.getsockname()[1]
def clear_notebook(input_notebook, output_notebook):
"""Write out a copy of the notebook with output and metadata removed."""
c = Config()
c.NotebookExporter.preprocessors = [
"nbconvert.preprocessors.ClearOutputPreprocessor",
"nbconvert.preprocessors.ClearMetadataPreprocessor",
]
exporter = NotebookExporter(config=c)
body, resources = exporter.from_filename(input_notebook)
with open(output_notebook, "w") as f:
f.write(body)
def isotime():
return datetime.datetime.now().isoformat()