How to use custom 'not_running.html' template with pre_spawn_hook?

Hey there!

Apologies if this may be a dumb question, however here is my current scenario and what I am trying to accomplish:

  • I have a pre_spawn_hook in my JupyterHub config that does some bootstrapping, but also does some permissions checks based on a custom drop-down option that loads prior to spawning a server
  • I created a custom not_running.html template that extends the base not_running.html template that should display a custom error message if the pre_spawn_hook permissions checks fail

My problem is I’m not quite sure how to go about raising the correct error/exception/etc. in my pre_spawn_hook to load the custom not_running.html template. I’ve tried raising things like tornado.web.HTTPError but that would just raise the error in the logs and take me to the base not_running.html template.

Any tips/advice/help would be greatly appreciated!

1 Like

What you can do is create a custom Exception out of web.HTTPError. I guess you need to set an attribute jupyterhub_message to show the error in the frontend. However, I guess the error will be shown in the spawn pending page as one of the progress messages. Something like this should do:

class SpawnException(web.HTTPError):
    """Custom exception that sets jupyterhub_message attribute"""

    def __init__(
        self,
        status_code: int = 500,
        log_message: Optional[str] = None,
        *args: Any,
        **kwargs: Any,
    ):
        super().__init__(status_code, log_message, *args, **kwargs)
        self.jupyterhub_message = log_message

And raise SpawnExecption in your pre_spawn_hook.

3 Likes

That did the trick! Thank you!

2 Likes