Skip to content

Commit

Permalink
fix!: Notebook 7 uses localhost:8888/nbclassic
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon committed Jul 19, 2023
1 parent 4f2fe81 commit cfb12b5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ The Jupynium server will receive events from Neovim, keep the copy of the buffer
-
```sh
# jupyter-console is optional and used for `:JupyniumKernelOpenInTerminal`
pip install notebook jupyter-console
pip install notebook nbclassic jupyter-console
```

#### Important note about Notebook 7 (breaking change!)
Jupynium does not support Notebook 7 yet. In the meantime, you can change the `default_notebook_URL = "localhost:8888/nbclassic"` in `require("jupynium").setup({ ... })` to use the classic (Notebook 6) interface with Jupynium. This is the new default setting from now on.

Don't forget to upgrade your notebook and install nbclassic (`pip install --upgrade notebook nbclassic`) when you set this.
### Install Python
Don't have system Python 3.7? You can use [Conda](https://docs.conda.io/en/latest/miniconda.html):
Expand Down Expand Up @@ -111,7 +117,7 @@ require("jupynium").setup({
-- python_host = { "conda", "run", "--no-capture-output", "-n", "jupynium", "python" },
python_host = vim.g.python3_host_prog or "python3",
default_notebook_URL = "localhost:8888",
default_notebook_URL = "localhost:8888/nbclassic",
-- Write jupyter command but without "notebook"
-- When you call :JupyniumStartAndAttachToServer and no notebook is open,
Expand All @@ -123,7 +129,7 @@ require("jupynium").setup({
-- Used when notebook is launched by using jupyter_command.
-- If nil or "", it will open at the git directory of the current buffer,
-- but still navigate to the directory of the current buffer. (e.g. localhost:8888/tree/path/to/buffer)
-- but still navigate to the directory of the current buffer. (e.g. localhost:8888/nbclassic/tree/path/to/buffer)
notebook_dir = nil,
-- Used to remember the last session (password etc.).
Expand Down Expand Up @@ -510,7 +516,7 @@ jupynium --nvim_listen_addr localhost:18898
Note that you can attach to remote neovim by changing `localhost` to `servername.com` or using SSH port forwarding.
This will open Firefox with Selenium, defaulting to `http://localhost:8888`.
This will open Firefox with Selenium, defaulting to `http://localhost:8888/nbclassic`.
Additionally,
Expand Down
4 changes: 2 additions & 2 deletions lua/jupynium/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ M.default_opts = {
-- python_host = { "conda", "run", "--no-capture-output", "-n", "jupynium", "python" },
python_host = vim.g.python3_host_prog or "python3",

default_notebook_URL = "localhost:8888",
default_notebook_URL = "localhost:8888/nbclassic",

-- Write jupyter command but without "notebook"
-- When you call :JupyniumStartAndAttachToServer and no notebook is open,
Expand All @@ -18,7 +18,7 @@ M.default_opts = {

-- Used when notebook is launched by using jupyter_command.
-- If nil or "", it will open at the git directory of the current buffer,
-- but still navigate to the directory of the current buffer. (e.g. localhost:8888/tree/path/to/buffer)
-- but still navigate to the directory of the current buffer. (e.g. localhost:8888/nbclassic/tree/path/to/buffer)
notebook_dir = nil,

-- Used to remember the last session (password etc.).
Expand Down
29 changes: 20 additions & 9 deletions src/jupynium/cmds/jupynium.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import traceback
from datetime import datetime
from pathlib import Path
from urllib.parse import urlparse
from urllib.parse import urlparse, urljoin

import coloredlogs
import git
Expand Down Expand Up @@ -123,7 +123,7 @@ def get_parser():
)
parser.add_argument(
"--notebook_URL",
default="localhost:8888",
default="localhost:8888/nbclassic",
help="Jupyter Notebook URL to open from the Selenium browser",
)
parser.add_argument(
Expand Down Expand Up @@ -180,7 +180,7 @@ def get_parser():
type=str,
help="When jupyter notebook has started using --jupyter_command, the root dir will be this.\n"
"If None, open at a git dir of nvim's buffer path and still navigate to the buffer dir.\n"
"(e.g. localhost:8888/tree/path/to/buffer)",
"(e.g. localhost:8888/nbclassic/tree/path/to/buffer)",
)
parser.add_argument(
"--no_auto_close_tab",
Expand Down Expand Up @@ -342,8 +342,12 @@ def kill_notebook_proc(notebook_proc):


def fallback_open_notebook_server(
notebook_port, jupyter_command, notebook_dir, nvim, driver
notebook_port, notebook_url_path, jupyter_command, notebook_dir, nvim, driver
):
"""
Args:
notebook_url_path (str): e.g. "/nbclassic"
"""
# Fallback: if the URL is localhost and if selenium can't connect,
# open the Jupyter Notebook server and even start syncing.
rel_dir = ""
Expand Down Expand Up @@ -391,26 +395,28 @@ def fallback_open_notebook_server(
)
except FileNotFoundError:
# Command doesn't exist
exception_no_notebook(f"localhost:{notebook_port}", nvim)
exception_no_notebook(f"localhost:{notebook_port}{notebook_url_path}", nvim)

time.sleep(1)
for _ in range(20):
try:
driver.get(
f"localhost:{notebook_port}/tree/{rel_dir}?token={notebook_token}"
f"localhost:{notebook_port}{notebook_url_path}/tree/{rel_dir}?token={notebook_token}"
)
break
except WebDriverException:
poll = notebook_proc.poll()
if poll is not None:
# Process finished
exception_no_notebook(f"localhost:{notebook_port}", nvim)
exception_no_notebook(
f"localhost:{notebook_port}{notebook_url_path}", nvim
)

time.sleep(0.3)
else:
# Process still running but timeout for connecting to notebook. Maybe wrong command?
kill_notebook_proc(notebook_proc)
exception_no_notebook(f"localhost:{notebook_port}", nvim)
exception_no_notebook(f"localhost:{notebook_port}{notebook_url_path}", nvim)
return notebook_proc


Expand Down Expand Up @@ -493,7 +499,12 @@ def main():
url = urlparse(notebook_URL)
if url.port is not None and url.hostname in ["localhost", "127.0.0.1"]:
notebook_proc = fallback_open_notebook_server(
url.port, args.jupyter_command, args.notebook_dir, nvim, driver
url.port,
url.path,
args.jupyter_command,
args.notebook_dir,
nvim,
driver,
)

else:
Expand Down
2 changes: 1 addition & 1 deletion src/jupynium/selenium_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
logger = logging.getLogger(__name__)


def wait_until_notebook_loaded(driver, timeout=10):
def wait_until_notebook_loaded(driver, timeout=30):
"""Wait until the Jupyter Notebook is loaded."""
try:
WebDriverWait(driver, timeout).until(
Expand Down

0 comments on commit cfb12b5

Please sign in to comment.