|
69 | 69 | """
|
70 | 70 | import functools
|
71 | 71 | import inspect
|
72 |
| -import json |
73 | 72 | import operator
|
74 |
| -import os |
75 |
| -import re |
76 | 73 | import string
|
77 | 74 | from itertools import zip_longest
|
78 | 75 | from typing import Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
|
|
84 | 81 | from .types import Operator, State
|
85 | 82 |
|
86 | 83 | 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): |
160 | 87 | _NOTEBOOK_NAME = ''
|
161 | 88 |
|
162 | 89 | if _NOTEBOOK_NAME:
|
|
0 commit comments