Commit 5f3ee05a authored by Tobias Hangleiter's avatar Tobias Hangleiter
Browse files

Make requests optional dependency for progress bar

and restructure the parsing of which progress bar to show.
parent 84f3d312
......@@ -12,98 +12,91 @@ import json
import os
import re
import sys
from typing import Iterable, List, Union
import requests
from requests.compat import urljoin
from typing import Generator, Iterable
try:
import jupyter_client
from notebook.utils import check_pid
import requests
from jupyter_core.paths import jupyter_runtime_dir
_has_notebook = True
from notebook.utils import check_pid
from requests.compat import urljoin
def _list_running_servers(runtime_dir: str = None) -> Generator:
"""Iterate over the server info files of running notebook servers.
Given a runtime directory, find nbserver-* files in the security
directory, and yield dicts of their information, each one pertaining to
a currently running notebook server instance.
Copied from notebook.notebookapp.list_running_servers() (version 5.7.8)
since the highest version compatible with Python 3.5 (version 5.6.0)
has a bug.
"""
if runtime_dir is None:
runtime_dir = jupyter_runtime_dir()
# The runtime dir might not exist
if not os.path.isdir(runtime_dir):
return
for file_name in os.listdir(runtime_dir):
if re.match('nbserver-(.+).json', file_name):
with io.open(os.path.join(runtime_dir, file_name),
encoding='utf-8') as f:
info = json.load(f)
# Simple check whether that process is really still running
# Also remove leftover files from IPython 2.x without a pid
# field
if ('pid' in info) and check_pid(info['pid']):
yield info
else:
# If the process has died, try to delete its info file
try:
os.unlink(os.path.join(runtime_dir, file_name))
except OSError:
pass # TODO: This should warn or log or something
def _get_notebook_name() -> str:
"""
Return the full path of the jupyter notebook.
See https://github.com/jupyter/notebook/issues/1000
"""
try:
connection_file = jupyter_client.find_connection_file()
except OSError:
return ''
kernel_id = re.search('kernel-(.*).json', connection_file).group(1)
servers = _list_running_servers()
for ss in servers:
response = requests.get(urljoin(ss['url'], 'api/sessions'),
params={'token': ss.get('token', '')})
for nn in json.loads(response.text):
if nn['kernel']['id'] == kernel_id:
relative_path = nn['notebook']['path']
return os.path.join(ss['notebook_dir'], relative_path)
_notebook_name = _get_notebook_name()
except ImportError:
_has_notebook = False
def _list_running_servers(runtime_dir: str = None) -> List[str]:
"""Iterate over the server info files of running notebook servers.
Given a runtime directory, find nbserver-* files in the security directory,
and yield dicts of their information, each one pertaining to
a currently running notebook server instance.
Copied from notebook.notebookapp.list_running_servers() (version 5.7.8)
since the highest version compatible with Python 3.5 (version 5.6.0) has a
bug.
"""
if runtime_dir is None:
runtime_dir = jupyter_runtime_dir()
# The runtime dir might not exist
if not os.path.isdir(runtime_dir):
return
for file_name in os.listdir(runtime_dir):
if re.match('nbserver-(.+).json', file_name):
with io.open(os.path.join(runtime_dir, file_name),
encoding='utf-8') as f:
info = json.load(f)
# Simple check whether that process is really still running
# Also remove leftover files from IPython 2.x without a pid field
if ('pid' in info) and check_pid(info['pid']):
yield info
else:
# If the process has died, try to delete its info file
try:
os.unlink(os.path.join(runtime_dir, file_name))
except OSError:
pass # TODO: This should warn or log or something
def _get_notebook_name() -> Union[None, str]:
"""
Return the full path of the jupyter notebook.
See https://github.com/jupyter/notebook/issues/1000
"""
try:
connection_file = jupyter_client.find_connection_file()
except OSError:
return
kernel_id = re.search('kernel-(.*).json', connection_file).group(1)
servers = _list_running_servers()
for ss in servers:
response = requests.get(urljoin(ss['url'], 'api/sessions'),
params={'token': ss.get('token', '')})
for nn in json.loads(response.text):
if nn['kernel']['id'] == kernel_id:
relative_path = nn['notebook']['path']
return os.path.join(ss['notebook_dir'], relative_path)
_notebook_name = ''
try:
if _has_notebook and _get_notebook_name():
if _notebook_name:
from tqdm import tqdm_notebook as tqdm
else:
# Either not running notebook or not able to determine
from tqdm import tqdm
_has_tqdm = True
except ImportError:
_has_tqdm = False
tqdm = None
__all__ = ['progressbar']
def _progressbar(iterable: Iterable, prefix: str = "Computing: ",
size: int = 25, file=sys.stdout):
"""
Primitive base progress bar stolen from
https://stackoverflow.com/a/34482761
"""
def _simple_progressbar(iterable: Iterable, prefix: str = "Computing: ",
size: int = 25, file=sys.stdout):
"""https://stackoverflow.com/a/34482761"""
count = len(iterable)
def show(j):
......@@ -131,7 +124,7 @@ def progressbar(iterable: Iterable, *args, **kwargs):
for i in progressbar(range(10)):
do_something()
"""
if _has_tqdm:
if tqdm is not None:
return tqdm(iterable, *args, **kwargs)
else:
return _progressbar(iterable, *args, **kwargs)
return _simple_progressbar(iterable, *args, **kwargs)
......@@ -34,7 +34,7 @@ setup(name='qutil',
package_dir={'qutil': 'qutil'},
python_requires='>=3.5',
install_requires=['numpy', 'hdf5storage', 'pandas', 'scipy'],
extras_require={'fancy_progressbar': ['tqdm']},
extras_require={'fancy_progressbar': ['tqdm', 'requests']},
classifiers=[
"Programming Language :: Python :: 3",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment