jupyterlab-contrib / jupyterlab-topbar

JupyterLab Top Bar extensions
BSD 3-Clause "New" or "Revised" License
104 stars 18 forks source link

Programmatically add text to topbar-text #35

Closed stefanvangastel closed 4 years ago

stefanvangastel commented 4 years ago

Is it possible to add text to the topbar-extension's text bar using a (config) file?

I'm using JupyterLab environments spawned by JupyterHub so I want to add e.g. the used image and version to the textbar after the image starts.

jtpio commented 4 years ago

Is it possible to add text to the topbar-extension's text bar using a (config) file?

Yes it's possible. JupyterLab settings are saved to disk in json files. It should be possible to create these files in a user environment before starting JupyterLab.

For the topbar-text extension the content can be retrieved with the following:

$ cat PREFIX/share/jupyter/lab/user-settings/jupyterlab-topbar-text/plugin.jupyterlab-settings
{
    // Top Bar Text
    // jupyterlab-topbar-text:plugin
    // Top Bar Text
    // *****************************

    // Text
    // Text to display
    "text": "Hello"
}% 

If not in PREFIX/share/jupyter then ~/.jupyter/ might be another place to look at.

I'm using JupyterLab environments spawned by JupyterHub so I want to add e.g. the used image and version to the textbar after the image starts.

With JupyterHub, one idea could be to add a pre_spawn_hook to the spawner and populate the settings files there. Similar to the bootstrapping example here: https://github.com/jupyterhub/jupyterhub/tree/master/examples/bootstrap-script

The image could be retrieved with spawner.image.

stefanvangastel commented 4 years ago

@jtpio thanks for this great answer! I'm using the kubespawner and thus I got it working using the following extra config in the Helm chart config:

singleuser:
  lifecycleHooks:
    postStart:
      exec:
        command:
          - "bash"
          - "-c"
          - >
            mkdir -p /home/$NB_UID/.jupyter/lab/user-settings/jupyterlab-topbar-text/;
            export IMAGE_VERSION=$(basename "$JUPYTER_IMAGE");
            echo "{\"text\":\"$IMAGE_VERSION\"}" > /home/$NB_UID/.jupyter/lab/user-settings/jupyterlab-topbar-text/plugin.jupyterlab-settings;

Works like a charm!

jtpio commented 4 years ago

Great, thanks for sharing this!