Adapted from the notebook linked from post here (direct link for the notebook here)that didn't seem to quite work for me; however, I followed what I think it was trying to do and could use some variations and tricks I picked up to make one that would work via MyBinder.org for demonstrations.
Launch a session using the following link:
http://mybinder.org/v2/gh/binder-examples/requirements/master/?urlpath=notebooks/index.ipynb
In the notebook that comes up enter the following in a cell:
!curl -OL https://gist.githubusercontent.com/fomightez/3ff6d30085720cdf8b7e0acac298e146/raw/90ff60a6924f06e948b57b6fdcc477d3a8e3dd80/query-strings_demo.ipynb
Keeping in mind you can use your browser window or tab switching abilities to come back here and follow along these directions when you open a new one, use the File
menu above on the toolbar to open a new notebook: 'File' > 'New Notebook'.
We really don't want to use this new notebook; however, we want to use it's tab window while navigating to the dashboard, staying in the classic interface. (The shortcut used to be to use clicking on the Jupyter logo; however, that now defaults to opening JupyterLab and we want to stay in the classic interface.) Therefore, in your browser's URL address bar, edit the end to replace 'Untitled.ipynb' to read 'tree' and hit return. It may verify you mean to navigate away from that new notebook and select 'Leave' from the alert prompt.
You should now see the classic interface navigation tree/Jupyter dashboard in this new tab or window.
From the Jupyter Dashboard tab, select the notebook 'query-strings_demo' and open it. It should open in yet another tab or window. It will have these directions and so you can follow along in that from now on.
Run the next cell to grab the URL of the current page.
%%javascript
IPython.notebook.kernel.execute("URL = '" + window.location + "'");
(That cell above is based on https://stackoverflow.com/a/54504064/8508004 and https://stackoverflow.com/a/37134476/8508004.)
Let's see the URL as a Python variable now by running the next cell:
URL
That's the basic URL. The next cell will genterate a link to a new page of this notebook with a parameter to pass to the notebook appended.
import IPython.display
IPython.display.HTML(data=f"""
<a href="{URL}?username=Riley">Link to this notebook but with URL that can be used to assign username as Riley</a>
""")
Click on that link above, confirm 'Leave' if prompted, to navigate away to open this same notebook and return to running this notebook from this point on.
Things will look similar upon reload; however, the difference is we have a parameter defined in the end of URL.
If you look at the address bar in your browser, you should see ?username=Riley
now at the end when the reload happens.
We now want to pass that information from the URL into the notebook to access username setting.
First we need to update URL
to be the current one by using javascript to get the page URL again. We'll do that witg the same code as earlier.
%%javascript
IPython.notebook.kernel.execute("URL = '" + window.location + "'");
You could add a cell with URL
or print(URL)
as the content to verify the difference, if you'd like.
Let's parse the updated URL to get the query string and assign it as query_string
.
# parsing the URL based on https://stackoverflow.com/a/49168071/8508004
from urllib.parse import urlparse
from urllib.parse import parse_qs
#product_url = Var1.find('product_url').text
parsed = urlparse(URL)
query_string = parse_qs(parsed.query)
query_string
That looks promising. Unfortunately the parameters come in as a list in this way; however, that is easily resolved since there's only one item in the list. We just take the first item.
username_from_URL = query_string.get('username')[0]
To prove that did something. We'll invoke username_from_URL
in the next cell and see what is returned.
username_from_URL
Enjoy.