Last active
November 21, 2019 11:44
-
-
Save mbdevpl/f97205b73610dd30254652e7817f99cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there any way to get the host:port from the kernel (in case it is lot 127.0.0.1:8888) ¡