Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Jupyter+TensorBoard to work w/ hostname other than localhost #2407

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions tensorboard/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
from __future__ import division
from __future__ import print_function

import cgi
import datetime
import errno
import json
import random
import shlex
import sys
import textwrap
Expand Down Expand Up @@ -395,11 +398,29 @@ def _display_colab(port, height, display_handle):

def _display_ipython(port, height, display_handle):
import IPython.display
iframe = IPython.display.IFrame(
src="http://localhost:%d" % port,
height=height,
width="100%",
)

frame_id = "tensorboard-frame-{:08x}".format(random.getrandbits(64))
shell = """
<iframe id="%HTML_ID%" width="100%" height="%HEIGHT%" frameborder="0">
</iframe>
<script>
(function() {
const frame = document.getElementById(%JSON_ID%);
const url = new URL("/", window.location);
url.port = %PORT%;
frame.src = url;
})();
</script>
"""
replacements = [
("%HTML_ID%", cgi.escape(frame_id, quote=True)),
("%JSON_ID%", json.dumps(frame_id)),
("%PORT%", "%d" % port),
("%HEIGHT%", "%d" % height),
]
for (k, v) in replacements:
shell = shell.replace(k, v)
iframe = IPython.display.HTML(shell)
if display_handle:
display_handle.update(iframe)
else:
Expand Down