Skip to content

Commit

Permalink
Create a separate script for the hub app
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Jul 26, 2024
1 parent a93cebc commit b78164f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ The gallery application backend can be run as a standalone server app by executi
jupyterlab-gallery
```

When run in JupyterHub context (with `jupyterhub` package installed), the standalone app will adopt the JupyterHub modifications to the single-user server.
To run it as a single-user server in the JupyterHub context use:

```bash
jupyterhub-gallery
```

## Requirements

Expand Down
27 changes: 0 additions & 27 deletions jupyterlab_gallery/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@
from .manager import GalleryManager


try:
from jupyterhub.singleuser.mixins import make_singleuser_app
except ImportError:

def make_singleuser_app(cls):
return cls


class classproperty(property):
def __get__(self, owner_self, owner_cls):
return self.fget(owner_cls)


class GalleryApp(ExtensionApp):
name = "gallery"

Expand All @@ -39,22 +26,8 @@ def initialize_settings(self):
def initialize_handlers(self):
# setting nbapp is needed for nbgitpuller
self.serverapp.web_app.settings["nbapp"] = self.serverapp

self.log.info(f"Registered {self.name} server extension")

@classproperty
def serverapp_class(cls):
"""If jupyterhub is installed, apply the jupyterhub patches,
but only do this when this property is accessed, which is when
the gallery is used as a standalone app.
"""
if cls._server_cls is None:
cls._server_cls = make_singleuser_app(ServerApp)
return cls._server_cls

_server_cls = None

@classmethod
def make_serverapp(cls, **kwargs) -> ServerApp:
"""Instantiate the ServerApp
Expand Down
28 changes: 28 additions & 0 deletions jupyterlab_gallery/hub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from jupyterhub.singleuser.mixins import make_singleuser_app
from jupyter_server.serverapp import ServerApp
from .app import GalleryApp


class classproperty(property):
def __get__(self, owner_self, owner_cls):
return self.fget(owner_cls)


class HubGalleryApp(GalleryApp):
# This is in a separate file because traitlets metaclass
# will read from each property on start, including serverapp_class,
# which will have side effects for the server if run outside of juypterhub

@classproperty
def serverapp_class(cls):
"""If jupyterhub is installed, apply the jupyterhub patches,
but only do this when this property is accessed, which is when
the gallery is used as a standalone app.
"""

if cls._server_cls is None:
cls._server_cls = make_singleuser_app(ServerApp)
return cls._server_cls

_server_cls = None
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jupyterlab-gallery",
"version": "0.6.1",
"version": "0.6.2",
"description": "A JupyterLab gallery extension for presenting and downloading examples from remote repositories",
"keywords": [
"jupyter",
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dev = ["ruff==0.4.4"]

[project.scripts]
jupyterlab-gallery = "jupyterlab_gallery:GalleryApp.launch_instance"
jupyterhub-gallery = "jupyterlab_gallery.hub:HubGalleryApp.launch_instance"

[tool.hatch.version]
source = "nodejs"
Expand Down

0 comments on commit b78164f

Please sign in to comment.