ipynbname icon indicating copy to clipboard operation
ipynbname copied to clipboard

not working in Visual Studio Code notebook viewer

Open mind6 opened this issue 4 years ago • 3 comments

vscode Version 1.53

import ipynbname
print(ipynbname.name())

Running this cell would never return (as it searches for servers?)

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-1-fe00ddc43357> in <module>
      3 
      4 import ipynbname
----> 5 print(ipynbname.name())
      6 
      7 import datetime

d:\Miniconda3\envs\thirdwave\lib\site-packages\ipynbname\__init__.py in name()
     77         or raises a FileNotFoundError exception if it cannot be determined.
     78     """
---> 79     _, path = _find_nb_path()
     80     if path:
     81         return path.stem

d:\Miniconda3\envs\thirdwave\lib\site-packages\ipynbname\__init__.py in _find_nb_path()
     64     for srv in _list_maybe_running_servers():
     65         try:
---> 66             sessions = _get_sessions(srv)
     67             for sess in sessions:
     68                 if sess['kernel']['id'] == kernel_id:

d:\Miniconda3\envs\thirdwave\lib\site-packages\ipynbname\__init__.py in _get_sessions(srv)
     51             qry_str = f"?token={token}"
     52         url = f"{srv['url']}api/sessions{qry_str}"
---> 53         with urllib.request.urlopen(url) as req:
     54             return json.load(req)
     55     except Exception:

d:\Miniconda3\envs\thirdwave\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 
    224 def install_opener(opener):

d:\Miniconda3\envs\thirdwave\lib\urllib\request.py in open(self, fullurl, data, timeout)
    523 
    524         sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 525         response = self._open(req, data)
    526 
    527         # post-process response

d:\Miniconda3\envs\thirdwave\lib\urllib\request.py in _open(self, req, data)
    540 
    541         protocol = req.type
--> 542         result = self._call_chain(self.handle_open, protocol, protocol +
    543                                   '_open', req)
    544         if result:

d:\Miniconda3\envs\thirdwave\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
    500         for handler in handlers:
    501             func = getattr(handler, meth_name)
--> 502             result = func(*args)
    503             if result is not None:
    504                 return result

d:\Miniconda3\envs\thirdwave\lib\urllib\request.py in http_open(self, req)
   1377 
   1378     def http_open(self, req):
-> 1379         return self.do_open(http.client.HTTPConnection, req)
   1380 
   1381     http_request = AbstractHTTPHandler.do_request_

d:\Miniconda3\envs\thirdwave\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
   1352             except OSError as err: # timeout error
   1353                 raise URLError(err)
-> 1354             r = h.getresponse()
   1355         except:
   1356             h.close()

d:\Miniconda3\envs\thirdwave\lib\http\client.py in getresponse(self)
   1345         try:
   1346             try:
-> 1347                 response.begin()
   1348             except ConnectionError:
   1349                 self.close()

d:\Miniconda3\envs\thirdwave\lib\http\client.py in begin(self)
    305         # read until we get a non-100 response
    306         while True:
--> 307             version, status, reason = self._read_status()
    308             if status != CONTINUE:
    309                 break

d:\Miniconda3\envs\thirdwave\lib\http\client.py in _read_status(self)
    266 
    267     def _read_status(self):
--> 268         line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    269         if len(line) > _MAXLINE:
    270             raise LineTooLong("status line")

d:\Miniconda3\envs\thirdwave\lib\socket.py in readinto(self, b)
    667         while True:
    668             try:
--> 669                 return self._sock.recv_into(b)
    670             except timeout:
    671                 self._timeout_occurred = True

KeyboardInterrupt: 

mind6 avatar Feb 06 '21 18:02 mind6

I just tried this and get a different result: image

I created a new blank notebook in VS Code, with default server, i.e. local. Were you connecting to some remote, or maybe existing local server?

msm1089 avatar Feb 07 '21 13:02 msm1089

The problem appears to be at the point it tries to get the sessions. It manages to construct the url to query, e.g. http://localhost:8888/api/sessions?token=8b3cbe84593547819ed5e78c97a6cadda392f2ce7469bcdb, but connection to this is refused and I cannot work out why so far. Maybe something to do with the way VS Code creates the server?

msm1089 avatar Feb 07 '21 14:02 msm1089

I was am using local server as well, Python 3.8.6

jupyter core     : 4.6.3
jupyter-notebook : 6.1.4
qtconsole        : 4.7.7
ipython          : 7.18.1
ipykernel        : 5.3.4
jupyter client   : 6.1.7
jupyter lab      : 2.2.9
nbconvert        : 6.0.7
ipywidgets       : 7.5.1
nbformat         : 5.0.8
traitlets        : 5.0.5

mind6 avatar Feb 08 '21 12:02 mind6

Workaround provided on other issue for VS Code:

https://github.com/msm1089/ipynbname/issues/17#issuecomment-1293269863

msm1089 avatar Apr 09 '23 15:04 msm1089