-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ead40f
commit 8a9de41
Showing
4 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
from typing import Dict | ||
from tornado import web | ||
from jupyterhub.utils import url_path_join | ||
from jupyterhub.services.auth import HubOAuthenticated | ||
from jinja2 import Template | ||
|
||
JUPYTERHUB_API_URL = os.environ.get("JUPYTERHUB_API_URL", None) | ||
|
||
|
||
class BaseHandler(HubOAuthenticated, web.RequestHandler): | ||
""" | ||
Base handler for tljh_repo2docker service | ||
""" | ||
|
||
def fetch_user(self) -> Dict: | ||
user = self.current_user | ||
print("AAAAAAAAAAAAA", user) | ||
return user | ||
|
||
def get_template(self, name: str) -> Template: | ||
"""Return the jinja template object for a given name | ||
Args: | ||
name: Template name | ||
Returns: | ||
jinja2.Template object | ||
""" | ||
return self.settings["jinja2_env"].get_template(name) | ||
|
||
def render_template(self, name: str, **kwargs) -> str: | ||
"""Render the given template with the provided arguments | ||
Args: | ||
name: Template name | ||
**kwargs: Template arguments | ||
Returns: | ||
The generated template | ||
""" | ||
template_ns = dict( | ||
service_prefix=self.settings.get("service_prefix", "/"), | ||
static_url=self.static_url, | ||
) | ||
template_ns.update(kwargs) | ||
template = self.get_template(name) | ||
return template.render(**template_ns) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from inspect import isawaitable | ||
from typing import Any, Dict | ||
from jupyterhub.orm import Spawner | ||
from .base import BaseHandler | ||
from tornado import web | ||
|
||
# from .docker import list_images | ||
|
||
|
||
class ServersHandler(BaseHandler): | ||
""" | ||
Handler to show the list of servers available | ||
""" | ||
|
||
@web.authenticated | ||
async def get(self): | ||
images = []#await list_images() | ||
user = self.current_user | ||
# if user.running: | ||
# # trigger poll_and_notify event in case of a server that died | ||
# await user.spawner.poll_and_notify() | ||
# auth_state = await user.get_auth_state() | ||
# named_spawners = user.all_spawners(include_default=False) | ||
server_data = [] | ||
# for sp in named_spawners: | ||
# server_data.append( | ||
# self._spawner_to_server_data(sp, user) | ||
# ) | ||
try: | ||
named_server_limit = await self.get_current_user_named_server_limit() | ||
except Exception: | ||
named_server_limit = 0 | ||
result = self.render_template( | ||
"servers.html", | ||
images=images, | ||
allow_named_servers=True, | ||
named_server_limit_per_user=named_server_limit, | ||
server_data=server_data, | ||
default_server_data={}, | ||
user_is_admin=True | ||
) | ||
|
||
if isawaitable(result): | ||
self.write(await result) | ||
else: | ||
self.write(result) | ||
|
||
def _spawner_to_server_data(self, sp: Spawner, user: Any) -> Dict: | ||
data = { | ||
"name": sp.name, | ||
} | ||
try: | ||
data["url"] = user.server_url(sp.name) | ||
except Exception: | ||
data["url"] = "" | ||
try: | ||
data["last_activity"] = sp.last_activity.isoformat() + "Z" | ||
except Exception: | ||
data["last_activity"] = "" | ||
|
||
try: | ||
data["active"] = sp.active | ||
except Exception: | ||
data["active"] = False | ||
|
||
try: | ||
if sp.user_options: | ||
data["user_options"] = sp.user_options | ||
else: | ||
data["user_options"] = {} | ||
except Exception: | ||
data["user_options"] = {} | ||
return data |