Skip to content

Commit f540620

Browse files
Use staticfilehandler to serve icons
1 parent 38fc180 commit f540620

File tree

2 files changed

+19
-43
lines changed

2 files changed

+19
-43
lines changed

jupyter_server_proxy/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,16 @@ def _load_jupyter_server_extension(nbapp):
5858
serverproxy_config,
5959
)
6060

61-
icons = {}
61+
icon_handlers = []
6262
for sp in server_processes:
6363
if sp.launcher_entry.enabled and sp.launcher_entry.icon_path:
64-
icons[sp.name] = sp.launcher_entry.icon_path
64+
icon_handlers.append(
65+
(
66+
ujoin(base_url, f"server-proxy/icon/{sp.name}"),
67+
IconHandler,
68+
{"path": sp.launcher_entry.icon_path}
69+
)
70+
)
6571

6672
nbapp.web_app.add_handlers(
6773
".*",
@@ -71,11 +77,6 @@ def _load_jupyter_server_extension(nbapp):
7177
ServersInfoHandler,
7278
{"server_processes": server_processes},
7379
),
74-
(
75-
ujoin(base_url, r"server-proxy/icon/(?P<name>.*)"),
76-
IconHandler,
77-
{"icons": icons}
78-
),
7980
(
8081
ujoin(base_url, r"api/server-proxy"),
8182
ListServersAPIHandler
@@ -84,7 +85,7 @@ def _load_jupyter_server_extension(nbapp):
8485
ujoin(base_url, r"api/server-proxy/(?P<name>.*)"),
8586
ServersAPIHandler
8687
),
87-
],
88+
] + icon_handlers,
8889
)
8990

9091
nbapp.log.debug(

jupyter_server_proxy/api.py

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import mimetypes
21
import json
32

43
from jupyter_server.base.handlers import JupyterHandler
@@ -37,42 +36,18 @@ async def get(self):
3736
self.write({"server_processes": data})
3837

3938

40-
# FIXME: Should be a StaticFileHandler subclass
41-
class IconHandler(JupyterHandler):
42-
"""
43-
Serve launcher icons
44-
"""
39+
# Took it from JupyterHub LogoHandler
40+
class IconHandler(web.StaticFileHandler):
41+
"""A singular handler for serving the icon."""
4542

46-
def initialize(self, icons):
47-
"""
48-
icons is a dict of titles to paths
49-
"""
50-
self.icons = icons
43+
def get(self):
44+
return super().get('')
5145

52-
async def get(self, name):
53-
if name not in self.icons:
54-
raise web.HTTPError(404)
55-
path = self.icons[name]
56-
57-
# Guess mimetype appropriately
58-
# Stolen from https://github.com/tornadoweb/tornado/blob/b399a9d19c45951e4561e6e580d7e8cf396ef9ff/tornado/web.py#L2881
59-
mime_type, encoding = mimetypes.guess_type(path)
60-
if encoding == "gzip":
61-
content_type = "application/gzip"
62-
# As of 2015-07-21 there is no bzip2 encoding defined at
63-
# http://www.iana.org/assignments/media-types/media-types.xhtml
64-
# So for that (and any other encoding), use octet-stream.
65-
elif encoding is not None:
66-
content_type = "application/octet-stream"
67-
elif mime_type is not None:
68-
content_type = mime_type
69-
# if mime_type not detected, use application/octet-stream
70-
else:
71-
content_type = "application/octet-stream"
72-
73-
with open(self.icons[name]) as f:
74-
self.write(f.read())
75-
self.set_header("Content-Type", content_type)
46+
@classmethod
47+
def get_absolute_path(cls, root, path):
48+
"""We only serve one file, ignore relative path"""
49+
import os
50+
return os.path.abspath(root)
7651

7752

7853
class ServersAPIHandler(JupyterHandler):

0 commit comments

Comments
 (0)