Skip to content

Commit aabeb0d

Browse files
authored
Merge pull request #50 from qutech/refactor/fancy_progressbar_notebook_detection
Refactor/fancy progressbar notebook detection
2 parents b411e41 + 3d87227 commit aabeb0d

File tree

3 files changed

+18
-87
lines changed

3 files changed

+18
-87
lines changed

.travis.yml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,32 @@ env:
1313
- INSTALL_DEPS="numpy scipy opt_einsum sparse tqdm"
1414
- INSTALL_TEST_DEPS="pytest>=4.6 pytest-cov codecov"
1515
- plotting="matplotlib"
16-
- fancy_progressbar="jupyter requests"
16+
- fancy_progressbar="ipynbname jupyter"
1717
- bloch_sphere_visualization="matplotlib qutip"
1818
jobs:
1919
- INSTALL_EXTRAS=[plotting,bloch_sphere_visualization,fancy_progressbar]
2020
- INSTALL_EXTRAS=[plotting,fancy_progressbar]
2121
- INSTALL_EXTRAS=[none]
22-
22+
2323
before_install:
2424
# Resolve INSTALL_EXTRAS into required packages for use with conda. HACKY
25+
# Split by comma and replace by parameter designator ($)
2526
- TMP=${INSTALL_EXTRAS//,/ $}
27+
# Take care of opening bracket separately
2628
- TMP=${TMP/[/$}
27-
- INSTALL_EXTRAS_PACKAGES=$(eval echo ${TMP%]})
28-
29+
# Replace the abstract extra names by the packages they require
30+
- TMP=$(eval echo ${TMP%]})
31+
# Remove ipynbname from conda packages (pip only)
32+
- INSTALL_EXTRAS_CONDA=${TMP// ipynbname/}
33+
2934
# Useful info for debugging
3035
- echo $TRAVIS_PYTHON_VERSION
3136
- echo $INSTALL_DEPS
3237
- echo $INSTALL_TEST_DEPS
3338
- echo $INSTALL_EXTRAS
34-
- echo $INSTALL_EXTRAS_PACKAGES
39+
- echo $INSTALL_EXTRAS_CONDA
3540

3641
# https://github.com/conda/conda/blob/master/docs/source/user-guide/tasks/use-conda-with-travis-ci.rst
37-
# We do this conditionally because it saves us some downloading if the
38-
# version is the same.
3942
- wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
4043
- bash miniconda.sh -b -u -p $HOME/miniconda
4144
- export PATH="$HOME/miniconda/bin:$PATH"
@@ -46,11 +49,12 @@ before_install:
4649
# Useful for debugging any issues with conda
4750
- conda info -a
4851

49-
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION $INSTALL_DEPS $INSTALL_TEST_DEPS $INSTALL_EXTRAS_PACKAGES
52+
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION $INSTALL_DEPS $INSTALL_TEST_DEPS $INSTALL_EXTRAS_CONDA
5053
- source activate test-environment
51-
54+
5255
install:
53-
- pip install .
56+
# Everything from INSTALL_EXTRAS should already be installed via conda, except for ipynbname
57+
- pip install .$INSTALL_EXTRAS
5458

5559
script:
5660
- pytest

filter_functions/util.py

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@
6969
"""
7070
import functools
7171
import inspect
72-
import json
7372
import operator
74-
import os
75-
import re
7673
import string
7774
from itertools import zip_longest
7875
from typing import Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
@@ -84,79 +81,9 @@
8481
from .types import Operator, State
8582

8683
try:
87-
import jupyter_client
88-
import requests
89-
from notebook.notebookapp import list_running_servers
90-
from requests.compat import urljoin
91-
92-
def _get_notebook_name() -> str:
93-
"""
94-
Return the full path of the jupyter notebook.
95-
96-
See https://github.com/jupyter/notebook/issues/1000
97-
98-
Jupyter notebook is licensed as follows:
99-
100-
This project is licensed under the terms of the Modified BSD License
101-
(also known as New or Revised or 3-Clause BSD), as follows:
102-
103-
- Copyright (c) 2001-2015, IPython Development Team
104-
- Copyright (c) 2015-, Jupyter Development Team
105-
106-
All rights reserved.
107-
108-
Redistribution and use in source and binary forms, with or without
109-
modification, are permitted provided that the following conditions are
110-
met:
111-
112-
Redistributions of source code must retain the above copyright notice,
113-
this list of conditions and the following disclaimer.
114-
115-
Redistributions in binary form must reproduce the above copyright
116-
notice, this list of conditions and the following disclaimer in the
117-
documentation and/or other materials provided with the distribution.
118-
119-
Neither the name of the Jupyter Development Team nor the names of its
120-
contributors may be used to endorse or promote products derived from
121-
this software without specific prior written permission.
122-
123-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
124-
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
125-
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
126-
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
127-
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
128-
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
129-
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
130-
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
131-
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
132-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
133-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
134-
"""
135-
try:
136-
connection_file = jupyter_client.find_connection_file()
137-
except OSError:
138-
return ''
139-
140-
kernel_id = re.search('kernel-(.*).json', connection_file).group(1)
141-
servers = list_running_servers()
142-
for ss in servers:
143-
response = requests.get(urljoin(ss['url'], 'api/sessions'),
144-
params={'token': ss.get('token', '')})
145-
for nn in json.loads(response.text):
146-
try:
147-
if nn['kernel']['id'] == kernel_id:
148-
try:
149-
relative_path = nn['notebook']['path']
150-
return os.path.join(ss['notebook_dir'], relative_path)
151-
except KeyError:
152-
return ''
153-
except TypeError:
154-
return ''
155-
156-
return ''
157-
158-
_NOTEBOOK_NAME = _get_notebook_name()
159-
except ImportError:
84+
import ipynbname
85+
_NOTEBOOK_NAME = ipynbname.name()
86+
except (ImportError, IndexError, FileNotFoundError):
16087
_NOTEBOOK_NAME = ''
16188

16289
if _NOTEBOOK_NAME:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def extract_version(version_file):
2424

2525
extras_require = {'plotting': ['matplotlib'],
2626
'bloch_sphere_visualization': ['qutip', 'matplotlib'],
27-
'fancy_progressbar': ['requests'],
27+
'fancy_progressbar': ['ipynbname', 'jupyter'],
2828
'doc': ['jupyter', 'nbsphinx', 'numpydoc', 'sphinx', 'sphinx_rtd_theme'],
2929
'tests': ['pytest>=4.6', 'pytest-cov', 'codecov']}
3030

0 commit comments

Comments
 (0)