Skip to content

Commit

Permalink
Merge pull request #37 from nebari-dev/default-thumbnail
Browse files Browse the repository at this point in the history
Use default thumbnails for all apps
  • Loading branch information
aktech authored Jan 5, 2024
2 parents e486efd + 2951220 commit 06ca96e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions jhub_apps/service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


def setup_logging():
# TODO: Use structlog
logging_format = (
"%(asctime)s %(levelname)9s %(lineno)4s %(module)s: %(message)s"
)
Expand Down
22 changes: 10 additions & 12 deletions jhub_apps/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
from fastapi.templating import Jinja2Templates

from jhub_apps.hub_client.hub_client import HubClient
from jhub_apps.service.utils import get_conda_envs, get_jupyterhub_config, get_spawner_profiles, \
encode_file_to_data_url
from jhub_apps.service.utils import get_conda_envs, get_jupyterhub_config, get_spawner_profiles, get_thumbnail_data_url
from jhub_apps.spawner.types import FRAMEWORKS

app = FastAPI()
Expand All @@ -36,6 +35,7 @@

# TODO: Add response models for all endpoints


@router.get("/oauth_callback", include_in_schema=False)
async def get_token(code: str):
"Callback function for OAuth2AuthorizationCodeBearer scheme"
Expand Down Expand Up @@ -111,11 +111,10 @@ async def create_server(
thumbnail: typing.Optional[UploadFile] = File(None),
user: User = Depends(get_current_user),
):
if thumbnail:
thumbnail_contents = await thumbnail.read()
server.user_options.thumbnail = encode_file_to_data_url(
thumbnail.filename, thumbnail_contents
)
server.user_options.thumbnail = await get_thumbnail_data_url(
framework_name=server.user_options.framework,
thumbnail=thumbnail
)
hub_client = HubClient()
return hub_client.create_server(
username=user.name,
Expand Down Expand Up @@ -149,11 +148,10 @@ async def update_server(
thumbnail: typing.Optional[UploadFile] = File(None),
user: User = Depends(get_current_user), server_name=None
):
if thumbnail:
thumbnail_contents = await thumbnail.read()
server.user_options.thumbnail = encode_file_to_data_url(
thumbnail.filename, thumbnail_contents
)
server.user_options.thumbnail = await get_thumbnail_data_url(
framework_name=server.user_options.framework,
thumbnail=thumbnail
)
hub_client = HubClient()
return hub_client.create_server(
username=user.name,
Expand Down
39 changes: 38 additions & 1 deletion jhub_apps/service/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import base64
import logging
import os

import requests
from jupyterhub.app import JupyterHub
from traitlets.config import LazyConfigValue

from jhub_apps.spawner.types import FrameworkConf, FRAMEWORKS_MAPPING


logger = logging.getLogger(__name__)


def get_jupyterhub_config():
hub = JupyterHub()
Expand Down Expand Up @@ -51,8 +58,38 @@ def encode_file_to_data_url(filename, file_contents):
"""Converts image file to data url to display in browser."""
base64_encoded = base64.b64encode(file_contents)
filename_ = filename.lower()
mime_type = "image/png"
if filename_.endswith(".jpg") or filename_.endswith(".jpeg"):
mime_type = "image/jpeg"
elif filename_.endswith('.svg'):
mime_type = "image/svg+xml"
else:
file_extension = filename_.split('.')[-1]
mime_type = f"image/{file_extension}"
data_url = f"data:{mime_type};base64,{base64_encoded.decode('utf-8')}"
return data_url


def get_default_thumbnail(framework_name):
framework: FrameworkConf = FRAMEWORKS_MAPPING.get(framework_name)
thumbnail_url = framework.logo
try:
response = requests.get(thumbnail_url)
except Exception as e:
logger.info(f"Unable to fetch thumbnail from url: {thumbnail_url}:")
logger.exception(e)
return
if response.status_code == 200:
thumbnail_content = response.content
thumbnail_filename = thumbnail_url.split("/")[-1]
return encode_file_to_data_url(filename=thumbnail_filename, file_contents=thumbnail_content)


async def get_thumbnail_data_url(framework_name, thumbnail):
if thumbnail:
thumbnail_contents = await thumbnail.read()
thumbnail_data_url = encode_file_to_data_url(
thumbnail.filename, thumbnail_contents
)
else:
thumbnail_data_url = get_default_thumbnail(framework_name)
return thumbnail_data_url
2 changes: 1 addition & 1 deletion jhub_apps/spawner/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def values(cls):
FrameworkConf(
name=Framework.gradio.value,
display_name="Gradio",
logo="https://pbs.twimg.com/profile_images/1526964416834510848/Njy4Kh2q_400x400.jpg",
logo="https://www.gradio.app/_app/immutable/assets/gradio.8a5e8876.svg",
),
FrameworkConf(
name=Framework.jupyterlab.value,
Expand Down

0 comments on commit 06ca96e

Please sign in to comment.