Get notebook/user context from python?

I’m running JHub 0.8.2 on AWS EKS cluster

I would like to know if it is possible to retrieve the owner of the current notebook server.

E.g.
Let’s assume I navigate to https://myjupyterhubhost.com/user/john-doe/notebooks/SampleNotebook.ipynb

In python, I want to do this:

user = get_current_context().get_notebook_user_owner()
print(f"The user who owns this notebook server: {user}")

# Output:
# The user who owns this notebook server: john-doe

By default JUPYTERHUB_USER environment variable is filled with the user. So any method to access the variable will do:

!echo JUPYTERHUB_USER

import os
os.environ['JUPYTERHUB_USER']

This is defined in https://github.com/jupyterhub/jupyterhub/blob/b1606f21e6afbcce6b9fd4bbdd07b29448a8f3f5/jupyterhub/spawner.py#L716 for reference.

1 Like