Skip to content

Commit

Permalink
fix: error in empty ipynb and default kernel not existing (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon authored Jun 15, 2024
1 parent 29ebaed commit 06f66f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lua/jupynium/cells.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ function M.line_type(line)
line = vim.api.nvim_buf_get_lines(0, line - 1, line, false)[1]
end

if vim.startswith(line, "# %% [md]") or vim.startswith(line, "# %% [markdown]") then
if line == nil then
return "empty"
elseif vim.startswith(line, "# %% [md]") or vim.startswith(line, "# %% [markdown]") then
return "cell separator: markdown"
elseif vim.fn.trim(line) == "# %%" then
return "cell separator: code"
Expand Down
42 changes: 31 additions & 11 deletions src/jupynium/events_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
}


class StartSyncError(Exception):
pass


@dataclass
class OnLinesArgs:
lines: list[str]
Expand Down Expand Up @@ -313,7 +317,16 @@ def start_sync_with_filename(
)
if kernel_name is None:
kernel_name = "python3"
kernel_btn = driver.find_element(By.ID, f"kernel-{kernel_name}")

try:
kernel_btn = driver.find_element(By.ID, f"kernel-{kernel_name}")
except NoSuchElementException:
# match anything with ID starting with kernel-
kernel_btns = driver.find_elements(By.CSS_SELECTOR, "[id^=kernel-]")
if len(kernel_btns) == 0:
raise StartSyncError("No kernel found in the kernel list.") from None
kernel_btn = kernel_btns[0]

driver.execute_script("arguments[0].scrollIntoView(true);", kernel_btn)
prev_windows = set(driver.window_handles)
try:
Expand Down Expand Up @@ -448,16 +461,23 @@ def process_request_event(nvim_info: NvimInfo, driver: WebDriver, event: list[An
if ipynb_filename != "" and not ipynb_filename.lower().endswith(".ipynb"):
ipynb_filename += ".ipynb"

start_sync_with_filename(
bufnr,
ipynb_filename,
ask,
content,
buf_filetype,
conda_or_venv_path,
nvim_info,
driver,
)
try:
start_sync_with_filename(
bufnr,
ipynb_filename,
ask,
content,
buf_filetype,
conda_or_venv_path,
nvim_info,
driver,
)
except StartSyncError as e:
nvim_info.nvim.lua.Jupynium_notify.error(
["Error while starting sync:", str(e)], async_=True
)
event[3].send("N")
return False, None
else:
# start sync with tab index
tab_idx = int(ipynb_filename)
Expand Down

0 comments on commit 06f66f2

Please sign in to comment.