Skip to content

Instantly share code, notes, and snippets.

@mbdevpl
Last active November 21, 2019 11:44
Show Gist options
  • Save mbdevpl/f97205b73610dd30254652e7817f99cb to your computer and use it in GitHub Desktop.
Save mbdevpl/f97205b73610dd30254652e7817f99cb to your computer and use it in GitHub Desktop.
# solution for:
# https://stackoverflow.com/questions/12544056/how-to-i-get-the-current-ipython-notebook-name
#
# aimed at:
# IPython 4.2.0 and Python 3.5
import json
import os
import urllib.request
import ipykernel
connection_file_path = ipykernel.get_connection_file()
connection_file = os.path.basename(connection_file_path)
kernel_id = connection_file.split('-', 1)[1].split('.')[0]
response = urllib.request.urlopen('http://127.0.0.1:8888/api/sessions')
sessions = json.loads(response.read().decode())
for sess in sessions:
if sess['kernel']['id'] == kernel_id:
print(sess['notebook']['path'])
break
@c-martinez
Copy link

Is there any way to get the host:port from the kernel (in case it is lot 127.0.0.1:8888) ¡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment