Method to access query string in Voila notebook

UPDATE: This is how to use the notebook that ConorMc posted above with MyBinder to demonstrate the Voila-compatible way to access a query string inside Voila. (Bonus is that it demonstrates a Voila-compatible way of getting the full URL for when running in MyBinder, too.)

Step-by-step:

  1. Launch a binder session with Voila installed and slightly edited version of the notebook ConorMc posted above present by clicking here.

  2. When the session spins up and the demo notebook opens, you’ll notice the content of the notebook includes code setting ‘Kim’ as the username around the middle.

  3. Launch the Voila version of this notebook by pressing ‘Voila’ button from the toolbar.

  4. Voila will open and it will say Hi Kim towards the bottom of the content on the page.

  5. Now to see the passing of the username query string in action, click on the blue link at the bottom that says ‘Link to myself as user Riley’. It should open another tab with Voila again.

  6. Note that this new tab will include ?username=Riley at the end of the URL. The content of the Voila page itself will look different now, too: it will say ‘Hi Riley’ in place of ‘Hi Kim’ because this Voila instance has been passed a username via the URL.

You can see all the envs environment variables that the JupyterHub exposes to Voila by adding the following code to a cell in that demonstration notebook, saving the notebook, and launching Voila via the button on the toolbar.

import os
envs = {k: v for k, v in os.environ.items()}
display (envs)

(Worked out from re-reading first link I posted in this thread and looking over the reference to the implementation therein, in particular, to here. I was too focused on the way I had been using javascript and missing some things. It was key to delete ‘localhost’ in the last cell with code in it in order to use the notebook that ConorMc posted above with MyBinder.)