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: error in empty ipynb and default kernel not existing #124

Merged
merged 1 commit into from
Jun 15, 2024
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
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
Loading