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

Commented magic command becomes uncommented on notebook (#28) and profiles ini is nil by default #29

Merged
merged 1 commit into from
Jan 31, 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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ require("jupynium").setup({
notebook_dir = nil,

-- Used to remember the last session (password etc.).
-- You may need to change the path.
firefox_profiles_ini_path = vim.fn.isdirectory(vim.fn.expand "~/snap/firefox/common/.mozilla/firefox")
and "~/snap/firefox/common/.mozilla/firefox/profiles.ini"
or "~/.mozilla/firefox/profiles.ini",
firefox_profile_name = nil, -- nil means the default profile
-- e.g. '~/.mozilla/firefox/profiles.ini'
-- or '~/snap/firefox/common/.mozilla/firefox/profiles.ini'
firefox_profiles_ini_path = nil,
-- nil means the profile with Default=1
-- or set to something like 'default-release'
firefox_profile_name = nil,

-- Open the Jupynium server if it is not already running
-- which means that it will open the Selenium browser when you open this file.
Expand Down
11 changes: 6 additions & 5 deletions lua/jupynium/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ M.default_opts = {
notebook_dir = nil,

-- Used to remember the last session (password etc.).
-- You may need to change the path.
firefox_profiles_ini_path = vim.fn.isdirectory(vim.fn.expand "~/snap/firefox/common/.mozilla/firefox")
and "~/snap/firefox/common/.mozilla/firefox/profiles.ini"
or "~/.mozilla/firefox/profiles.ini",
firefox_profile_name = nil, -- nil means the default profile
-- e.g. '~/.mozilla/firefox/profiles.ini'
-- or '~/snap/firefox/common/.mozilla/firefox/profiles.ini'
firefox_profiles_ini_path = nil,
-- nil means the profile with Default=1
-- or set to something like 'default-release'
firefox_profile_name = nil,

-- Open the Jupynium server if it is not already running
-- which means that it will open the Selenium browser when you open this file.
Expand Down
6 changes: 5 additions & 1 deletion lua/jupynium/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,15 @@ function M.start_and_attach_to_server_cmd(args)
if notebook_URL == "" then
notebook_URL = options.opts.default_notebook_URL
end
args = { "--notebook_URL", notebook_URL, "--firefox_profiles_ini_path", options.opts.firefox_profiles_ini_path }
args = { "--notebook_URL", notebook_URL }
if options.opts.notebook_dir ~= nil and options.opts.notebook_dir ~= "" then
table.insert(args, "--notebook_dir")
table.insert(args, options.opts.notebook_dir)
end
if options.opts.firefox_profiles_ini_path ~= nil and options.opts.firefox_profiles_ini_path ~= "" then
table.insert(args, "--firefox_profiles_ini_path")
table.insert(args, options.opts.firefox_profiles_ini_path)
end
if options.opts.firefox_profile_name ~= nil and options.opts.firefox_profile_name ~= "" then
table.insert(args, "--firefox_profile_name")
table.insert(args, options.opts.firefox_profile_name)
Expand Down
13 changes: 12 additions & 1 deletion src/jupynium/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class JupyniumBuffer:
"""

def __init__(self, buf: list[str] = [""]):
"""
self.buf is a list of lines of the nvim buffer,
with the exception that the commented magic commands are normal magic commands.
e.g. '# %time' -> '%time'
"""
self.buf = buf
if self.buf == [""]:
self.num_rows_per_cell: list[int] = [
Expand All @@ -34,7 +39,7 @@ def full_analyse_buf(self):
num_rows_this_cell = 0
num_rows_per_cell = []
cell_types = ["header"]
for line in self.buf:
for row, line in enumerate(self.buf):
if (
line.startswith("# %%%")
or line.startswith("# %% [md]")
Expand All @@ -53,6 +58,12 @@ def full_analyse_buf(self):
num_rows_per_cell.append(num_rows_this_cell)
num_rows_this_cell = 1
cell_types.append("code")
elif line.startswith("# %"):
# Use '# %' for magic commands
# e.g. '# %matplotlib inline'
# Remove the comment
self.buf[row] = self.buf[row][2:]
num_rows_this_cell += 1
else:
num_rows_this_cell += 1
num_rows_per_cell.append(num_rows_this_cell)
Expand Down
20 changes: 11 additions & 9 deletions src/jupynium/cmds/jupynium.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ def get_parser():
"--attach_only",
action="store_true",
help="Attach to an existing Jupynium instance."
"If False, start a new instance or attach to an existing one."
"If False, start a new instance or attach to an existing one.\n"
"If True, all arguments except nvim_listen_addr and notebook_URL are ignored.",
)
parser.add_argument(
"--sleep_time_idle",
type=float,
default=0.1,
default=0.05,
help="Sleep time when there is no event to process.",
)
parser.add_argument(
Expand All @@ -149,8 +149,10 @@ def get_parser():
)
parser.add_argument(
"--firefox_profiles_ini_path",
default="~/.mozilla/firefox/profiles.ini",
help="Path to firefox profiles.ini which will be used to remember the last session (password, etc.)",
help="Path to firefox profiles.ini which will be used to remember the last session (password, etc.)\n"
"Example path:\n"
"~/.mozilla/firefox/profiles.ini\n"
"~/snap/firefox/common/.mozilla/firefox/profiles.ini",
)
parser.add_argument(
"--firefox_profile_name",
Expand All @@ -161,16 +163,16 @@ def get_parser():
type=str,
nargs="+",
default=["jupyter"],
help="Command to start Jupyter Notebook (but without notebook)."
"To use conda env, use `--jupyter_command ~/miniconda3/envs/env_name/bin/jupyter`."
"Don't use `conda run ..` as it won't be killed afterwards (it opens another process with different pid so it's hard to keep track of it.)"
help="Command to start Jupyter Notebook (but without notebook).\n"
"To use conda env, use `--jupyter_command ~/miniconda3/envs/env_name/bin/jupyter`.\n"
"Don't use `conda run ..` as it won't be killed afterwards (it opens another process with different pid so it's hard to keep track of it.)\n"
"It is used only when the --notebook_URL is localhost, and is not running.",
)
parser.add_argument(
"--notebook_dir",
type=str,
help="When jupyter notebook has started using --jupyter_command, the root dir will be this."
"If None, open at a git dir of nvim's buffer path and still navigate to the buffer dir."
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)",
)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ def test_buffer_1():
assert buffer.cell_types == ["header", "code"]


def test_magic_command_1():
"""
Everything else except magic commands should be preserved after __init__() or fully_analysed_buf()
"""
lines = ["a", "b", "c", "# %%", "# %time", "e", "f"]
buffer = JupyniumBuffer(lines)
assert buffer.buf[4] == "%time"
for i, line in enumerate(lines):
if i == 4:
continue
assert buffer.buf[i] == line


def test_buffer_markdown():
buffer = JupyniumBuffer(["a", "b", "c", "# %%%", "d", "# %%", "f"])
assert buffer.num_rows_per_cell == [3, 2, 2]
Expand Down