Never mind, I found a solution using the "Security in the Jupyter notebook 
server" documentatinon here 
<http://jupyter-notebook.readthedocs.io/en/latest/security.html>.

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

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jupyter/74482888-d5f8-40d0-953c-6ce2a763ca78%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to