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

✨ nbclassic environment support #252

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions nbproject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
"""
__version__ = "0.8.1"

# init jupyter lab frontend immediately on import
# nothing happens if this is not jupyter lab
from .dev._classic_nb_commands import _set_nbclassic_path
from .dev._jupyter_lab_commands import _init_frontend

# init jupyter lab frontend immediately on import
# nothing happens if this is not jupyter lab
_init_frontend()
# tries to set the NBCLASSIC_PATH env variable for classical notebook or nbclassic
_set_nbclassic_path()

from . import dev
from ._header import header
Expand Down
16 changes: 16 additions & 0 deletions nbproject/dev/_classic_nb_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
from .._logger import logger


def _set_nbclassic_path():
try:
from IPython.display import Javascript, clear_output, display_javascript
except ModuleNotFoundError:
logger.warning("Can not import from IPython.")
return None

js = Javascript(
"if(typeof IPython !== 'undefined') {IPython.notebook.kernel.execute(\"import"
" os; os.environ['NBCLASSIC_PATH']=\" + \"'\" + IPython.notebook.notebook_path"
' + "\'")}'
)
display_javascript(js)
clear_output()


def _save_notebook():
try:
from IPython.display import Javascript, display_javascript
Expand Down
21 changes: 14 additions & 7 deletions nbproject/dev/_jupyter_communicate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from itertools import chain
from pathlib import PurePath
from pathlib import Path
from urllib import request

import orjson
Expand Down Expand Up @@ -83,6 +83,10 @@ def notebook_path(return_env=False):
else:
return nb_path

# this allows to set the right env for nbclassic
if "NBCLASSIC_PATH" in os.environ and env is None:
env = "notebook"

servers_nbapp, servers_juserv = running_servers()

try:
Expand Down Expand Up @@ -123,15 +127,13 @@ def notebook_path(return_env=False):
if notebook["kernel"].get("id", None) == kernel_id:
for dir_key in DIR_KEYS:
if dir_key in server:
nb_path = (
PurePath(server[dir_key]) / notebook["notebook"]["path"]
)
nb_path = Path(server[dir_key]) / notebook["notebook"]["path"]

# VScode adaption through "-jvsc-"
nb_path_str = str(nb_path)
if "-jvsc-" in nb_path_str:
split = nb_path_str.split("-jvsc-")
nb_path = PurePath(f"{split[0]}.ipynb")
nb_path = Path(f"{split[0]}.ipynb")
if return_env:
return nb_path, "vs_code" if env is None else env
else:
Expand All @@ -146,19 +148,24 @@ def notebook_path(return_env=False):
else:
return nb_path

# set path for classic notebook or nbclassic if server querying fails
if "NBCLASSIC_PATH" in os.environ:
nb_path = Path.cwd() / os.environ["NBCLASSIC_PATH"]
return (nb_path, "notebook" if env is None else env) if return_env else nb_path

# trying to get the path through ipylab
nb_path = _lab_notebook_path()
if nb_path is not None:
return (nb_path, "lab" if env is None else env) if return_env else nb_path

# for newer versions of lab, less safe as it stays the same after file rename
if "JPY_SESSION_NAME" in os.environ:
nb_path = PurePath(os.environ["JPY_SESSION_NAME"])
nb_path = Path(os.environ["JPY_SESSION_NAME"])
return (nb_path, "lab" if env is None else env) if return_env else nb_path

# vs code specific
if "__vsc_ipynb_file__" in globals():
nb_path = PurePath(__vsc_ipynb_file__) # noqa
nb_path = Path(__vsc_ipynb_file__) # noqa
return (nb_path, "vs_code" if env is None else env) if return_env else nb_path

if server_exception is not None:
Expand Down