Skip to content

Commit 462bd9f

Browse files
committed
Allow Jupyter+TensorBoard to work w/ hostname other than localhost
Signed-off-by: Cliff Woolley <jwoolley@nvidia.com>
1 parent 373eb09 commit 462bd9f

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

tensorboard/notebook.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,31 @@ def _display_colab(port, height, display_handle):
395395

396396
def _display_ipython(port, height, display_handle):
397397
import IPython.display
398-
iframe = IPython.display.IFrame(
399-
src="http://localhost:%d" % port,
400-
height=height,
401-
width="100%",
402-
)
398+
import cgi
399+
import json
400+
import random
401+
402+
frame_id = "tensorboard-frame-{:08x}".format(random.randrange(0, 1 << 64))
403+
shell = """
404+
<iframe id="%HTML_ID%" width="100%" height="%HEIGHT%" frameborder="0"></iframe>
405+
<script>
406+
(function() {
407+
const frame = document.getElementById(%JSON_ID%);
408+
const url = new URL("/", window.location);
409+
url.port = %PORT%;
410+
frame.src = url;
411+
})();
412+
</script>
413+
"""
414+
replacements = [
415+
("%HTML_ID%", cgi.escape(frame_id, quote=True)),
416+
("%JSON_ID%", json.dumps(frame_id)),
417+
("%PORT%", "%d" % port),
418+
("%HEIGHT%", "%d" % height),
419+
]
420+
for (k, v) in replacements:
421+
shell = shell.replace(k, v)
422+
iframe = IPython.display.HTML(shell)
403423
if display_handle:
404424
display_handle.update(iframe)
405425
else:

0 commit comments

Comments
 (0)