-
Notifications
You must be signed in to change notification settings - Fork 2
/
3142.patch
166 lines (141 loc) · 5.56 KB
/
3142.patch
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
From 5615204ba44a6b8718e58c9b4b875fef5027baaf Mon Sep 17 00:00:00 2001
From: qzchenwl <[email protected]>
Date: Tue, 14 Jan 2020 15:22:27 +0800
Subject: [PATCH 1/2] support jupyterhub with jupyter-server-proxy
---
tensorboard/notebook.py | 53 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/tensorboard/notebook.py b/tensorboard/notebook.py
index fe0e13aa40..778172bd7f 100644
--- a/tensorboard/notebook.py
+++ b/tensorboard/notebook.py
@@ -47,6 +48,7 @@
# details).
_CONTEXT_COLAB = "_CONTEXT_COLAB"
_CONTEXT_IPYTHON = "_CONTEXT_IPYTHON"
+_CONTEXT_JUPYTERHUB = "_CONTEXT_JUPYTERHUB"
_CONTEXT_NONE = "_CONTEXT_NONE"
@@ -83,12 +85,29 @@ def _get_context():
else:
ipython = IPython.get_ipython()
if ipython is not None and ipython.has_trait("kernel"):
+ if os.environ.get('JUPYTERHUB_SERVICE_PREFIX') is not None:
+ return _CONTEXT_JUPYTERHUB
return _CONTEXT_IPYTHON
# Otherwise, we're not in a known notebook context.
return _CONTEXT_NONE
+def _prefix_jupyterhub(port):
+ prefix = os.path.join(os.environ['JUPYTERHUB_SERVICE_PREFIX'], 'proxy/absolute')
+ return '%s/%d/' % (prefix, port)
+
+
+def _patch_args_jupyterhub(parsed_args):
+ if '--port' in parsed_args:
+ arg_idx = parsed_args.index('--port')
+ port = int(parsed_args[arg_idx+1])
+ else:
+ port = 6006
+ parsed_args += ['--port', str(port)]
+ return parsed_args + ['--path_prefix', _prefix_jupyterhub(port)]
+
+
def load_ipython_extension(ipython):
"""Deprecated: use `%load_ext tensorboard` instead.
@@ -159,6 +178,9 @@ def print_or_update(message):
handle.update(IPython.display.Pretty(message))
parsed_args = shlex.split(args_string, comments=True, posix=True)
+ if context == _CONTEXT_JUPYTERHUB:
+ parsed_args = _patch_args_jupyterhub(parsed_args)
+
start_result = manager.start(parsed_args)
if isinstance(start_result, manager.StartLaunched):
@@ -315,6 +337,7 @@ def _display(port=None, height=None, print_message=False, display_handle=None):
fn = {
_CONTEXT_COLAB: _display_colab,
_CONTEXT_IPYTHON: _display_ipython,
+ _CONTEXT_JUPYTERHUB: _display_jupyterhub,
_CONTEXT_NONE: _display_cli,
}[_get_context()]
return fn(port=port, height=height, display_handle=display_handle)
@@ -399,6 +422,36 @@ def _display_ipython(port, height, display_handle):
IPython.display.display(iframe)
+def _display_jupyterhub(port, height, display_handle):
+ import IPython.display
+
+ 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("%PREFIX%", window.location);
+ frame.src = url;
+ })();
+ </script>
+ """
+ replacements = [
+ ("%HTML_ID%", html_escape(frame_id, quote=True)),
+ ("%JSON_ID%", json.dumps(frame_id)),
+ ("%PREFIX%", _prefix_jupyterhub(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:
+ IPython.display.display(iframe)
+
+
def _display_cli(port, height, display_handle):
del height # unused
del display_handle # unused
From e59ca8d45746f459d797f4e69377eda4433e1624 Mon Sep 17 00:00:00 2001
From: qzchenwl <[email protected]>
Date: Tue, 14 Jan 2020 15:56:30 +0800
Subject: [PATCH 2/2] reformat
---
tensorboard/notebook.py | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/tensorboard/notebook.py b/tensorboard/notebook.py
index 778172bd7f..9a961bb81e 100644
--- a/tensorboard/notebook.py
+++ b/tensorboard/notebook.py
@@ -85,7 +85,7 @@ def _get_context():
else:
ipython = IPython.get_ipython()
if ipython is not None and ipython.has_trait("kernel"):
- if os.environ.get('JUPYTERHUB_SERVICE_PREFIX') is not None:
+ if os.environ.get("JUPYTERHUB_SERVICE_PREFIX") is not None:
return _CONTEXT_JUPYTERHUB
return _CONTEXT_IPYTHON
@@ -94,18 +94,20 @@ def _get_context():
def _prefix_jupyterhub(port):
- prefix = os.path.join(os.environ['JUPYTERHUB_SERVICE_PREFIX'], 'proxy/absolute')
- return '%s/%d/' % (prefix, port)
+ prefix = os.path.join(
+ os.environ["JUPYTERHUB_SERVICE_PREFIX"], "proxy/absolute"
+ )
+ return "%s/%d/" % (prefix, port)
def _patch_args_jupyterhub(parsed_args):
- if '--port' in parsed_args:
- arg_idx = parsed_args.index('--port')
- port = int(parsed_args[arg_idx+1])
+ if "--port" in parsed_args:
+ arg_idx = parsed_args.index("--port")
+ port = int(parsed_args[arg_idx + 1])
else:
port = 6006
- parsed_args += ['--port', str(port)]
- return parsed_args + ['--path_prefix', _prefix_jupyterhub(port)]
+ parsed_args += ["--port", str(port)]
+ return parsed_args + ["--path_prefix", _prefix_jupyterhub(port)]
def load_ipython_extension(ipython):
@@ -179,7 +181,7 @@ def print_or_update(message):
parsed_args = shlex.split(args_string, comments=True, posix=True)
if context == _CONTEXT_JUPYTERHUB:
- parsed_args = _patch_args_jupyterhub(parsed_args)
+ parsed_args = _patch_args_jupyterhub(parsed_args)
start_result = manager.start(parsed_args)