Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Utils] Remove legacy notebook server and bump jupyter server #189

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ name = "pypi"

[packages]
nbconvert = ">=6.4.5"
# notebook.notebookapp has been deprecated since 7.0.0
notebook = ">=6.4, <7.0.0"
liranbg marked this conversation as resolved.
Show resolved Hide resolved
pyyaml = ">=3.13, <7"
requests = ">=2.31.0"
boto3 = ">=1.28.0"
importlib-metadata = "<8.0.0"
ipython = ">=7.27.0"
jupyter_server = ">=2.0.0"

[dev-packages]
flake8 = "*"
Expand Down
32 changes: 5 additions & 27 deletions nuclio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import shlex
from argparse import ArgumentParser
from sys import stdout
from itertools import chain

import ipykernel
from urllib.parse import urlencode, urljoin
Expand Down Expand Up @@ -177,53 +176,32 @@ def str2nametag(input):
# https://github.com/jupyter/notebook/issues/1000#issuecomment-359875246
def notebook_file_name(ikernel):
"""Return the full path of the jupyter notebook."""

# the following code won't work when the notebook is being executed
# through running `jupyter nbconvert --execute` this env var enables to
# overcome it
from notebook.notebookapp import list_running_servers \
as nb_list_running_servers
# installing jupyter-server is optional (only when doing pip install
# nuclio-jupyter[jupyter-server]) therefore don't fail on import error
jupyter_server_supported = False
try:
from jupyter_server.serverapp import list_running_servers \
as jp_list_running_servers
except ImportError:
pass
else:
jupyter_server_supported = True

file_name = environ.get('JUPYTER_NOTEBOOK_FILE_NAME')
if file_name is not None:
return file_name

from jupyter_server.serverapp import list_running_servers

# Check that we're running under notebook
if not (ikernel and ikernel.config['IPKernelApp']):
return

kernel_id = re.search('kernel-(.*).json',
ipykernel.connect.get_connection_file()).group(1)

# list both notebook servers (nbserver-*.json) and the newer
# jupyter servers (jpserver-*.json), remove nb_list_running_servers()
# when fully moving to jupyter servers.
servers = nb_list_running_servers()
if jupyter_server_supported:
servers = chain(nb_list_running_servers(), jp_list_running_servers())
# list jupyter servers (jpserver-*.json)
servers = list_running_servers()
for srv in servers:
query = {'token': srv.get('token', '')}
url = urljoin(srv['url'], 'api/sessions') + '?' + urlencode(query)
for session in json.load(urlopen(url)):
if session['kernel']['id'] == kernel_id:
relative_path = session['notebook']['path']

# remove srv.get('notebook_dir') when fully moving to
# jupyter servers.
return path.join(
srv.get('notebook_dir') or srv.get('root_dir'),
relative_path
)
return path.join(srv.get('root_dir'), relative_path)

# vscode jupyter plugin communicates directly with ipykernel and doesn't execute a server
return ikernel.user_ns.get("__vsc_ipynb_file__")
6 changes: 0 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,6 @@ def load_deps(section):
],
setup_requires=['pytest-runner'],
tests_require=tests_require,
extras_require={
# jupyter-server is the new "infrastructure" of jupyter, in the Iguazio Jupyter we're still using an old version
# which uses the notebook-server. installing jupyter-server there is causing troubles (unwanted upgrade of
# tornado package, so we're installing jupyter-server only if explicitly requested by adding an extra
"jupyter-server": ["jupyter-server~=1.0"],
liranbg marked this conversation as resolved.
Show resolved Hide resolved
},
entry_points={
'nbconvert.exporters': [
'nuclio=nuclio.export:NuclioExporter',
Expand Down
Loading