Skip to content

Instantly share code, notes, and snippets.

@bkreider
Created June 14, 2014 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkreider/bf611f68a7e99b244303 to your computer and use it in GitHub Desktop.
Save bkreider/bf611f68a7e99b244303 to your computer and use it in GitHub Desktop.
Ipython notebook name
# http://stackoverflow.com/questions/12544056/how-to-i-get-the-current-ipython-notebook-name
import json
import os
import urllib2
import IPython
from IPython.lib import kernel
connection_file_path = kernel.get_connection_file()
connection_file = os.path.basename(connection_file_path)
kernel_id = connection_file.split('-', 1)[1].split('.')[0]
# Updated answer with semi-solutions for both IPython 2.x and IPython < 2.x
if IPython.version_info[0] < 2:
## Not sure if it's even possible to get the port for the
## notebook app; so just using the default...
notebooks = json.load(urllib2.urlopen('http://127.0.0.1:8888/notebooks'))
for nb in notebooks:
if nb['kernel_id'] == kernel_id:
print nb['name']
break
else:
sessions = json.load(urllib2.urlopen('http://127.0.0.1:8888/api/sessions'))
for sess in sessions:
if sess['kernel']['id'] == kernel_id:
print sess['notebook']['name']
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment