-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathnotebook.py
More file actions
62 lines (50 loc) · 1.67 KB
/
notebook.py
File metadata and controls
62 lines (50 loc) · 1.67 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""%tensorboard line magic that patches TensorBoard's implementation to make use of Jupyter
TensorBoard server extension providing built-in proxying.
Use:
%load_ext tensorboard.notebook
%tensorboard --logdir /logs
"""
import argparse
import uuid
from IPython.display import display, HTML, Javascript
def _tensorboard_magic(line):
"""Line magic function.
Makes an AJAX call to the Jupyter TensorBoard server extension and outputs
an IFrame displaying the TensorBoard instance.
"""
parser = argparse.ArgumentParser()
parser.add_argument('--logdir', default='/kaggle/working')
args = parser.parse_args(line.split())
iframe_id = 'tensorboard-' + str(uuid.uuid4())
html = """
<!-- JUPYTER_TENSORBOARD_TEST_MARKER -->
<script>
const req = {
method: 'POST',
contentType: 'application/json',
body: JSON.stringify({ 'logdir': '%s' }),
headers: { 'Content-Type': 'application/json' }
};
const baseUrl = Jupyter.notebook.base_url;
fetch(baseUrl + 'api/tensorboard', req)
.then(res => res.json())
.then(res => {
const iframe = document.getElementById('%s');
iframe.src = baseUrl + 'tensorboard/' + res.name;
iframe.style.display = 'block';
});
</script>
<iframe
id="%s"
style="width: 100%%; height: 620px; display: none;"
frameBorder="0">
</iframe>
""" % (args.logdir, iframe_id, iframe_id)
display(HTML(html))
def load_ipython_extension(ipython):
"""IPython extension entry point."""
ipython.register_magic_function(
_tensorboard_magic,
magic_kind='line',
magic_name='tensorboard',
)