Username in Jupyterhub header

41 views
Skip to first unread message

Scott Calabrese Barton

unread,
Oct 22, 2017, 11:15:56 AM10/22/17
to Project Jupyter
As a supervisor I log into a lot of different JupyterHub accounts, and I'd like to be able to see the username of the current login. I wrote a short iPython extension below, which adds the username to the kernel_logo_widget element. It works with the %load_ext magic, but I'm not sure how to make it work when automatically loaded.

I've tried adding the following to ipython_kernel_config.py:

c.IPKernelApp.extensions = [ 'NameInHeader' ]

with the idea that the extension would load with the kernel. This loads the extension, but maybe too soon because the header is not changed.  Is there a better way to accomplish what I'm trying to do?

# NameInHeader.py

def load_ipython_extension(ipython):
    
    import re, os
    from IPython.display import display, Javascript

    uname= os.getenv('USER')

    s='''var s = document.getElementById('kernel_logo_widget').innerHTML;
         var news= '<span style=\\'color:#919191\\'>_user_ </span>' + s;
         document.getElementById('kernel_logo_widget').innerHTML=news;
    '''

    func=re.compile('_user_')
    s=func.sub(uname, s)

    display(Javascript(s))

Thomas Kluyver

unread,
Oct 23, 2017, 5:56:33 AM10/23/17
to Project Jupyter
I think the issue is that output which doesn't come from executing a cell is not displayed (in the case of Javascript, that means not run). You might be able to make this more easily as an nbextension (a Javascript extension for the Jupyter notebook interface). I don't know if the current username is directly accessible from Javascript in Jupyterhub.

You might also want to open an issue on Jupyterhub for an officially supported way to do the same thing.

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+unsubscribe@googlegroups.com.
To post to this group, send email to jup...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/0b075d02-3afb-44bd-9003-b3ad34a4975f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Scott Calabrese Barton

unread,
Oct 24, 2017, 8:24:36 AM10/24/17
to Project Jupyter
Thanks for the reply. As far as I can tell, the user name is only accessible from the server side, thus the need for python. I tried writing a notebook server extension but that is intended for "RESTful API handlers" which is a bit beyond my abilities. 

I'll try opening an issue on Jupyterhub.


On Monday, October 23, 2017 at 5:56:33 AM UTC-4, takowl wrote:
I think the issue is that output which doesn't come from executing a cell is not displayed (in the case of Javascript, that means not run). You might be able to make this more easily as an nbextension (a Javascript extension for the Jupyter notebook interface). I don't know if the current username is directly accessible from Javascript in Jupyterhub.

You might also want to open an issue on Jupyterhub for an officially supported way to do the same thing.
On 22 October 2017 at 17:15, Scott Calabrese Barton <s.calabr...@gmail.com> wrote:
As a supervisor I log into a lot of different JupyterHub accounts, and I'd like to be able to see the username of the current login. I wrote a short iPython extension below, which adds the username to the kernel_logo_widget element. It works with the %load_ext magic, but I'm not sure how to make it work when automatically loaded.

I've tried adding the following to ipython_kernel_config.py:

c.IPKernelApp.extensions = [ 'NameInHeader' ]

with the idea that the extension would load with the kernel. This loads the extension, but maybe too soon because the header is not changed.  Is there a better way to accomplish what I'm trying to do?

# NameInHeader.py

def load_ipython_extension(ipython):
    
    import re, os
    from IPython.display import display, Javascript

    uname= os.getenv('USER')

    s='''var s = document.getElementById('kernel_logo_widget').innerHTML;
         var news= '<span style=\\'color:#919191\\'>_user_ </span>' + s;
         document.getElementById('kernel_logo_widget').innerHTML=news;
    '''

    func=re.compile('_user_')
    s=func.sub(uname, s)

    display(Javascript(s))

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages