From ab25859b102f297b15d04486452b7b5e611a2989 Mon Sep 17 00:00:00 2001 From: krassowski <5832902+krassowski@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:41:11 +0100 Subject: [PATCH] Only apply the singleuser hub modifications if needed --- jupyterlab_gallery/app.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/jupyterlab_gallery/app.py b/jupyterlab_gallery/app.py index c1a0387..3549dc6 100644 --- a/jupyterlab_gallery/app.py +++ b/jupyterlab_gallery/app.py @@ -12,8 +12,9 @@ def make_singleuser_app(cls): return cls -# if jupyterhub is installed, apply jupyterhub patches -ServerAppClass = make_singleuser_app(ServerApp) +class classproperty(property): + def __get__(self, owner_self, owner_cls): + return self.fget(owner_cls) class GalleryApp(ExtensionApp): @@ -41,7 +42,18 @@ def initialize_handlers(self): self.log.info(f"Registered {self.name} server extension") - serverapp_class = ServerAppClass + @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: @@ -52,7 +64,7 @@ def make_serverapp(cls, **kwargs) -> ServerApp: code (`kernels` service). """ server_app = super().make_serverapp(**kwargs) - assert isinstance(server_app, ServerAppClass) + assert isinstance(server_app, cls.serverapp_class) assert len(server_app.default_services) > 1 server_app.default_services = ("auth", "security") return server_app