New version of Jupyter breaks code to get notebook file path

71 views
Skip to first unread message

DG

unread,
Jun 18, 2017, 9:00:34 PM6/18/17
to Project Jupyter

So I did a fresh new installation (anaconda + Python 2.7) for other reasons and now the code I used to get the file path of the currently running notebook does not work anymore. This is annoying to say the least.

This is my code, screapped together from various places. First, I find what the port of the notebook server is:

%%javascript
var nb = Jupyter.notebook;
var port = window.location.port;
//nb.kernel.execute("NB_NAME = '" + nb.notebook_name + "'");
nb.kernel.execute("NB_Port = '" + port + "'");

Then, I get the file path:

# Get file path of this notebook
connection_file_path = kernel.get_connection_file()
connection_file = os.path.basename(connection_file_path)
kernel_id = connection_file.split('-', 1)[1].split('.')[0]
sessions = json.load(urllib2.urlopen('http://127.0.0.1:'+NB_Port+'/api/sessions'))
for sess in sessions:
    if sess['kernel']['id'] == kernel_id:
        nb_rel_path = (sess['notebook']['path'])
        break
res = !echo ~
nb_path = os.path.join(res[0],nb_rel_path)
nb_path
nb_dir,nb_filename = os.path.split(nb_path)

Well, now the line 
sessions = 

Fails with 
ValueError: No JSON object could be decoded

Any suggestions? TIA.

Roland Weber

unread,
Jun 19, 2017, 2:04:32 AM6/19/17
to Project Jupyter
I think there were changes regarding authentication. You might have to pass a security token as a query parameter or header now. Try some command-line debugging with curl on the /api/sessions URL.

If you know the old and new versions of the various Jupyter components, have a look at the release notes.

hope that helps,
  Roland

DG

unread,
Jun 19, 2017, 2:17:12 PM6/19/17
to Project Jupyter
Thanks Roland!

Changing the line of code to:
sessions = json.load(urllib2.urlopen('http://127.0.0.1:'+NB_Port+'/api/sessions?token=e72d0fdd7891272a7db87f391c00dfead81a8659f43a917e'))


Made the script work again. The token however was manually copied/pasted from the terminal after starting jupyter. 

Now the question becomes: How do I get the security token automatically?

BTW, I could not find those "release notes" you mention, documenting /api/sessions. Care to point me to the correct location?

sp...@draves.org

unread,
Jun 19, 2017, 2:30:46 PM6/19/17
to jup...@googlegroups.com
Isn't the Python kernel run with its current working directory set to be the same as the noteobook?
In which case you can os.getcwd()...?
perhaps I misunderstand your objective.

--
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/e3df37c0-2c21-480a-85f1-590439710329%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

sp...@draves.org

unread,
Jun 19, 2017, 2:35:23 PM6/19/17
to jup...@googlegroups.com
Oh I see you want the name of the file, not the directory.

DG

unread,
Jun 19, 2017, 2:37:06 PM6/19/17
to Project Jupyter
Never mind, I found a solution using the "Security in the Jupyter notebook server" documentatinon here.

And here's the new code:

First, we get the port of the notebook (I could also use the output of the next cell to do so):
%%javascript
var nb = Jupyter.notebook;
var port = window.location.port;
//nb.kernel.execute("NB_NAME = '" + nb.notebook_name + "'");
nb.kernel.execute("NB_Port = '" + port + "'");


Next, we get the security token:
# Get security token
# Get list of running notebooks. This gives the security token as parts of the 
# URL of each notebook. Assume notebook of interest is first item in the list (skip header).
nb_list = %sx jupyter notebook list
url=nb_list[1]
token=url.split('token=')[1].split(' ')[0]

And finally we issue the json request using the security token:
# Get file path of this notebook
connection_file_path = kernel.get_connection_file()
connection_file = os.path.basename(connection_file_path)
kernel_id = connection_file.split('-', 1)[1].split('.')[0]
sessions = json.load(urllib2.urlopen('http://127.0.0.1:'+NB_Port+'/api/sessions?token='+token))
for sess in sessions:
    if sess['kernel']['id'] == kernel_id:
        nb_rel_path = (sess['notebook']['path'])
        break
res = !echo ~
nb_path = os.path.join(res[0],nb_rel_path)
nb_path
nb_dir,nb_filename = os.path.split(nb_path)


I hope this can help somebody

Roland Weber

unread,
Jun 20, 2017, 2:00:57 AM6/20/17
to Project Jupyter
I'm glad you could sort out the problem.

On Monday, June 19, 2017 at 8:17:12 PM UTC+2, DG wrote:
BTW, I could not find those "release notes" you mention, documenting /api/sessions. Care to point me to the correct location?

It's not about /api/sessions in particular. Token authentication was enabled by default for all APIs, in release 4.3 of Jupyter Notebook.

cheers,
  Roland

Reply all
Reply to author
Forward
0 new messages