FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #494 from Kaggle/tensorboard · RTDataScience/docker-python@a6ba32e · GitHub

Commit a6ba32e

Browse files
Aurelio Agundez
authored
Merge pull request Kaggle#494 from Kaggle/tensorboard
Add TensorBoard to Kaggle Kernels
2 parents 6d5f6a1 + 33ddc9d commit a6ba32e

5 files changed

Lines changed: 143 additions & 0 deletions

File tree

‎Dockerfile‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,5 +514,13 @@ ADD patches/kaggle_gcp.py /root/.local/lib/python3.6/site-packages/kaggle_gcp.py
514514
ADD patches/kaggle_secrets.py /root/.local/lib/python3.6/site-packages/kaggle_secrets.py
515515
ADD patches/sitecustomize.py /root/.local/lib/python3.6/site-packages/sitecustomize.py
516516

517+
# TensorBoard Jupyter extension. Should be replaced with TensorBoard's provided magic once we have
518+
# worker tunneling support in place.
519+
ENV JUPYTER_CONFIG_DIR "/root/.jupyter/"
520+
RUN pip install jupyter_tensorboard && \
521+
jupyter serverextension enable jupyter_tensorboard && \
522+
jupyter tensorboard enable
523+
ADD patches/tensorboard/notebook.py /opt/conda/lib/python3.6/site-packages/tensorboard/notebook.py
524+
517525
# Set backend for matplotlib
518526
ENV MPLBACKEND "agg"

‎gpu.Dockerfile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ RUN pip install pycuda && \
6262
pip install cupy-cuda92 && \
6363
pip install pynvrtc && \
6464
/tmp/clean-layer.sh
65+
66+
# Re-add TensorBoard Jupyter extension patch
67+
ADD patches/tensorboard/notebook.py /opt/conda/lib/python3.6/site-packages/tensorboard/notebook.py

‎patches/tensorboard/notebook.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""%tensorboard line magic that patches TensorBoard's implementation to make use of Jupyter
2+
TensorBoard server extension providing built-in proxying.
3+
4+
Use:
5+
%load_ext tensorboard.notebook
6+
%tensorboard --logdir /logs
7+
"""
8+
9+
import argparse
10+
import uuid
11+
12+
from IPython.display import display, HTML, Javascript
13+
14+
def _tensorboard_magic(line):
15+
"""Line magic function.
16+
17+
Makes an AJAX call to the Jupyter TensorBoard server extension and outputs
18+
an IFrame displaying the TensorBoard instance.
19+
"""
20+
parser = argparse.ArgumentParser()
21+
parser.add_argument('--logdir', default='/kaggle/working')
22+
args = parser.parse_args(line.split())
23+
24+
iframe_id = 'tensorboard-' + str(uuid.uuid4())
25+
26+
html = """
27+
<!-- JUPYTER_TENSORBOARD_TEST_MARKER -->
28+
<script>
29+
const req = {
30+
method: 'POST',
31+
contentType: 'application/json',
32+
body: JSON.stringify({ 'logdir': '%s' }),
33+
headers: { 'Content-Type': 'application/json' }
34+
};
35+
36+
const baseUrl = Jupyter.notebook.base_url;
37+
38+
fetch(baseUrl + 'api/tensorboard', req)
39+
.then(res => res.json())
40+
.then(res => {
41+
const iframe = document.getElementById('%s');
42+
iframe.src = baseUrl + 'tensorboard/' + res.name;
43+
iframe.style.display = 'block';
44+
});
45+
</script>
46+
47+
<iframe
48+
id="%s"
49+
style="width: 100%%; height: 620px; display: none;"
50+
frameBorder="0">
51+
</iframe>
52+
""" % (args.logdir, iframe_id, iframe_id)
53+
54+
display(HTML(html))
55+
56+
def load_ipython_extension(ipython):
57+
"""IPython extension entry point."""
58+
ipython.register_magic_function(
59+
_tensorboard_magic,
60+
magic_kind='line',
61+
magic_name='tensorboard',
62+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"cells": [
3+
{
4+
"metadata": {
5+
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
6+
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
7+
"trusted": true
8+
},
9+
"cell_type": "code",
10+
"source": "%load_ext tensorboard.notebook\n%tensorboard --logdir /logs",
11+
"execution_count": null,
12+
"outputs": []
13+
},
14+
{
15+
"metadata": {
16+
"_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
17+
"collapsed": true,
18+
"_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a",
19+
"trusted": false
20+
},
21+
"cell_type": "code",
22+
"source": "",
23+
"execution_count": null,
24+
"outputs": []
25+
}
26+
],
27+
"metadata": {
28+
"kernelspec": {
29+
"display_name": "Python 3",
30+
"language": "python",
31+
"name": "python3"
32+
},
33+
"language_info": {
34+
"name": "python",
35+
"version": "3.6.4",
36+
"mimetype": "text/x-python",
37+
"codemirror_mode": { "name": "ipython", "version": 3 },
38+
"pygments_lexer": "ipython3",
39+
"nbconvert_exporter": "python",
40+
"file_extension": ".py"
41+
}
42+
},
43+
"nbformat": 4,
44+
"nbformat_minor": 1
45+
}

‎tests/test_jupytertensorboard.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
import subprocess
3+
import unittest
4+
5+
class TestJupyterTensorBoard(unittest.TestCase):
6+
def test_jupytertensorboard(self):
7+
file_path = os.path.realpath(__file__)
8+
ipynb_path = os.path.join(
9+
os.path.dirname(file_path),
10+
'data',
11+
'jupyter_tensorboard.ipynb',
12+
)
13+
14+
result = subprocess.run([
15+
'jupyter',
16+
'nbconvert',
17+
'--execute',
18+
'--stdout',
19+
ipynb_path,
20+
],
21+
stdout=subprocess.PIPE
22+
)
23+
24+
self.assertTrue(result.returncode == 0)
25+
self.assertTrue(b'JUPYTER_TENSORBOARD_TEST_MARKER' in result.stdout)

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL