Skip to content

Commit

Permalink
Create LICENSE
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt committed Oct 20, 2024
1 parent 155a3a6 commit edc76ce
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Trevor Manz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
69 changes: 35 additions & 34 deletions src/juv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Pep723Meta:

REGEX = r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$"

Command = typing.Literal["lab", "notebook", "nbclassic"]


def parse_pep723_meta(script: str) -> Pep723Meta | None:
name = "script"
Expand All @@ -45,11 +47,11 @@ def parse_pep723_meta(script: str) -> Pep723Meta | None:
return None


def nbcell(source: str) -> dict:
def nbcell(source: str, hidden: bool = False) -> dict:
return {
"cell_type": "code",
"execution_count": None,
"metadata": {},
"metadata": {"jupyter": {"source_hidden": hidden}},
"outputs": [],
"source": source,
}
Expand All @@ -63,7 +65,7 @@ def script_to_nb(script: str) -> str:

if meta_block:
meta_block = meta_block.group(0)
cells.append(nbcell(meta_block))
cells.append(nbcell(meta_block, hidden=True))
script = script.replace(meta_block, "")

cells.append(nbcell(script.strip()))
Expand Down Expand Up @@ -117,7 +119,7 @@ def assert_uv_available():
def run_notebook(
nb_path: pathlib.Path,
pep723_meta: Pep723Meta | None,
command: typing.Literal["lab", "notebook"],
command: Command,
pre_args: list[str],
command_version: str | None,
) -> None:
Expand All @@ -135,19 +137,20 @@ def run_notebook(
cmd.extend(["--with", dep])

if command == "lab":
cmd.extend(
[
"--with",
f"jupyterlab=={command_version}" if command_version else "jupyterlab",
]
)
cmd.extend([
"--with",
f"jupyterlab=={command_version}" if command_version else "jupyterlab",
])
elif command == "notebook":
cmd.extend(
[
"--with",
f"notebook=={command_version}" if command_version else "notebook",
]
)
cmd.extend([
"--with",
f"notebook=={command_version}" if command_version else "notebook",
])
elif command == "nbclassic":
cmd.extend([
"--with",
f"nbclassic=={command_version}" if command_version else "nbclassic",
])

cmd.extend(pre_args)

Expand All @@ -162,48 +165,46 @@ def run_notebook(

def split_args() -> tuple[list[str], list[str], str | None]:
for i, arg in enumerate(sys.argv):
if arg in ["lab", "notebook"]:
if arg in ["lab", "notebook", "nbclassic"]:
return sys.argv[1:i], sys.argv[i:], None

if arg.startswith("lab@") or arg.startswith("notebook@"):
if (
arg.startswith("lab@")
or arg.startswith("notebook@")
or arg.startswith("nbclassic@")
):
# replace the command with the actual command but get the version
command, version = sys.argv[i].split("@", 1)
return sys.argv[1:i], [command] + sys.argv[i + 1 :], version

return [], sys.argv, None


def parse_juv_command(command: str | None) -> typing.Literal["lab", "notebook"] | None:
if command == "lab":
return "lab"
if command == "notebook":
return "notebook"
return None
def is_command(command: typing.Any) -> typing.TypeGuard[Command]:
return command in ["lab", "notebook", "nbclassic"]


def main() -> None:
pre_args, post_args, command_version = split_args()
uv_args, args, command_version = split_args()

help = r"""A wrapper around `[cyan]uv[/cyan]` to launch ephemeral Jupyter notebooks.
help = r"""A wrapper around [cyan]uv[/cyan] to launch ephemeral Jupyter notebooks.
[b]Usage[/b]: juv \[uvx flags] <COMMAND>\[@version] \[PATH]
[b]Examples[/b]:
uvx juv lab script.py
uvx juv nbclassic script.py
uvx juv notebook existing_notebook.ipynb
uvx juv --python=3.8 [email protected] script.ipynb
[b]Notes[/b]:
- Any flags before the 'lab' or 'notebook' command are passed directly to uv."""
uvx juv --python=3.8 [email protected] script.ipynb"""

if "-h" in sys.argv or "--help" in sys.argv:
rich.print(help)
sys.exit(0)

command = parse_juv_command(post_args[0] if post_args else None)
file = post_args[1] if len(post_args) > 1 else None
command = args[0] if args else None
file = args[1] if len(args) > 1 else None

if command is None or not file:
if not is_command(command) or not file:
rich.print(help)
sys.exit(1)

Expand All @@ -219,7 +220,7 @@ def main() -> None:
file = file.with_suffix(".ipynb")
file.write_text(content)

run_notebook(file, meta, command, pre_args, command_version)
run_notebook(file, meta, command, uv_args, command_version)


if __name__ == "__main__":
Expand Down

0 comments on commit edc76ce

Please sign in to comment.