Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,6 @@ lite/jupyterlite-icortex/style/icortex.png
lite/jupyterlite-icortex/LICENSE
node_modules/
*.tsbuildinfo
*_backup
*_backup
misc/
/icortex.toml
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
repos:
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
# It is recommended to specify the latest version of Python
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
language_version: python3.10
60 changes: 39 additions & 21 deletions icortex/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,14 @@ def from_file(path: str, scope: t.Dict[str, t.Any] = None):
raise FileNotFoundError(f"File {path} does not exist")

with open(path, "r") as f:
dict_ = json.load(f)
context_dict = json.load(f)

return ICortexContext.from_dict(context_dict, scope)

def from_dict(context_dict: t.Dict[str, t.Any], scope: t.Dict[str, t.Any] = None):
ret = ICortexContext(scope=scope)

for cell_dict in dict_["cells"]:
for cell_dict in context_dict["cells"]:
if cell_dict["metadata"]["source_type"] == "code":
cell = CodeCell.from_dict(cell_dict)
elif cell_dict["metadata"]["source_type"] == "var":
Expand All @@ -301,7 +304,7 @@ def from_file(path: str, scope: t.Dict[str, t.Any] = None):
)
ret._cells.append(cell)

ret._vars = [Var.from_dict(v) for v in dict_["metadata"]["variables"]]
ret._vars = [Var.from_dict(v) for v in context_dict["metadata"]["variables"]]

return ret

Expand Down Expand Up @@ -333,33 +336,32 @@ def run(self, notebook_args: t.List[str]):
# Execute the returned code
exec(code, scope)

def bake(self, dest_path: str, format=True):
"""Bake the notebook to a Python script"""
def get_code(self, argparsify=False):
"""Aggregates the code for the notebook"""

# Warn if the extension is not .py
if not dest_path.endswith(".py"):
print(
f"Warning: {dest_path} does not have the .py extension. "
"It is recommended that you use the .py extension for "
"frozen files."
)
output = ""
if argparsify:
# scope = locals()
vars = self.vars

vars = self.vars
scope = locals()

output = "import argparse\n\nparser = argparse.ArgumentParser()\n"
for var in vars:
output += f"parser.add_argument({var.arg!r}, type={var._type.__name__})\n"
output += "args = parser.parse_args()\n\n"
output += "import argparse\n\nparser = argparse.ArgumentParser()\n"
for var in vars:
output += (
f"parser.add_argument({var.arg!r}, type={var._type.__name__})\n"
)
output += "args = parser.parse_args()\n\n"

for cell in self.iter_cells():
if cell.success:
if isinstance(cell, VarCell):
var = cell.var
# Change the value to that of the parsed argument
# var.value = var._type(getattr(parsed_args, var.arg))
code = f"{var.name} = args.{var.arg}\n\n"
# code = var.get_code()
if argparsify:
code = f"{var.name} = args.{var.arg}\n\n"
else:
code = var.get_code()

elif isinstance(cell, CodeCell):
if not is_magic(cell.get_code()):
code = cell.get_code().rstrip() + "\n\n"
Expand All @@ -371,9 +373,25 @@ def bake(self, dest_path: str, format=True):
# Execute the returned code
output += code

return output

def bake(self, dest_path: str, format=True):
"""Bake the notebook to a Python script"""

# Warn if the extension is not .py
if not dest_path.endswith(".py"):
print(
f"Warning: {dest_path} does not have the .py extension. "
"It is recommended that you use the .py extension for "
"frozen files."
)

output = self.get_code(argparsify=True)

# Run black over output
if format:
import black

try:
output = black.format_str(output, mode=black.FileMode())
except:
Expand Down
2 changes: 1 addition & 1 deletion icortex/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
ICortexShell._init_icortex_shell(__main__.get_ipython())
# load_ipython_extension(get_ipython())
else:
raise Exception("IPython is not available, cannot initialize ICortex.")
raise Exception("IPython is not available, cannot initialize ICortex.")
1 change: 1 addition & 0 deletions icortex/services/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from icortex.helper import escape_quotes
from icortex.services.generation_result import GenerationResult


class EchoService(ServiceBase):
name = "echo"
description = "Service used for testing"
Expand Down
5 changes: 2 additions & 3 deletions icortex/services/service_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from icortex.services.service_interaction import ServiceInteraction
from icortex.parser import lex_prompt


def is_str_repr(s: str):
quotes = ["'", '"']
return len(s) >= 2 and s[0] in quotes and s[-1] in quotes
Expand Down Expand Up @@ -159,9 +160,7 @@ def __init__(self, **kwargs: t.Dict[str, t.Any]):
required=DEFAULT_AUTO_INSTALL_PACKAGES,
help="Auto-install packages that are imported in the generated code but missing in the active Python environment.",
)
self.prompt_parser.usage = (
"%%prompt your prompt goes here [-e] [-r] [-p] ..."
)
self.prompt_parser.usage = "%%prompt your prompt goes here [-e] [-r] [-p] ..."

self.prompt_parser.description = self.description

Expand Down
4 changes: 2 additions & 2 deletions icortex/services/textcortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

# Load alternative URI from the environment
try:
from dotenv import load_dotenv
from dotenv import load_dotenv, find_dotenv

load_dotenv()
load_dotenv(find_dotenv(usecwd=True))
ICORTEX_ENDPOINT_URI = os.environ.get("ICORTEX_ENDPOINT_URI", ICORTEX_ENDPOINT_URI)
except:
pass
Expand Down
1 change: 1 addition & 0 deletions icortex/var.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# "set": set,
}


class Var:
def __init__(self, arg, name, value, type, description=None):
self.arg = arg
Expand Down
Loading