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

fix!: Notebook 7 uses localhost:8888/nbclassic #86

Merged
merged 1 commit into from
Jul 20, 2023
Merged
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
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
27 changes: 19 additions & 8 deletions src/jupynium/cmds/jupynium.py
Original file line number Diff line number Diff line change
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