Skip to content

Commit

Permalink
Merge pull request #10 from epuerta9/7-run-script
Browse files Browse the repository at this point in the history
feat(cli): added dynamic module import
  • Loading branch information
epuerta9 authored Oct 26, 2024
2 parents 86b8f99 + 78dbb3b commit f8ace41
Show file tree
Hide file tree
Showing 6 changed files with 3,089 additions and 34 deletions.
27 changes: 16 additions & 11 deletions kitchenai/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import typer
from django.conf import settings
from cookiecutter.main import cookiecutter
from typing_extensions import Annotated


logger = logging.getLogger(__name__)
app = typer.Typer()
Expand All @@ -19,55 +21,57 @@ def add(module: str = typer.Argument("app.kitchen:kitchen")):
def init():
from django.core.management import execute_from_command_line
execute_from_command_line(["manage", "migrate"])

execute_from_command_line(["manage", "init"])


@app.command()
def qcluster() -> None:
"""Run Django-q cluster."""
from django.core.management import execute_from_command_line

# execute_from_command_line(["manage", "qcluster", *argv[2:]])
execute_from_command_line(["manage", "qcluster"])


@app.command()
def runserver() -> None:
def runserver(module: Annotated[str, typer.Option(help="Python module to load.")] = "") -> None:
"""Run Django runserver."""
# from django.core.management import execute_from_command_line
#sys.argv pop to remove the command line "dev" before extending the gunicorn command
sys.argv.pop(1)
#NOTE: doing this to reset the sys.argv for gunicorn command.
sys.argv = [sys.argv[0]]

django.setup()
from kitchenai.api import api
from kitchenai.core.utils import setup

setup(
api
api,
module=module
)
_run_dev_uvicorn(sys.argv)

@app.command()
def run() -> None:
def run(module: Annotated[str, typer.Option(help="Python module to load.")] = "") -> None:
"""Run Django runserver."""
sys.argv.pop(1)
sys.argv = [sys.argv[0]]
django.setup()
from kitchenai.api import api
from kitchenai.core.utils import setup

setup(
api
api,
module=module
)

_run_uvicorn(sys.argv)


@app.command()
def dev(address: str ="0.0.0.0:8000"):
def dev(address: str ="0.0.0.0:8000", module: Annotated[str, typer.Option(help="Python module to load.")] = "" ):
"""
Reads the kitchen config file, reads the application file and runs the KitchenAI server
"""
commands = {"server": "kitchenai runserver"}
if module:
commands["server"] = f"kitchenai runserver --module {module}"
if "django_tailwind_cli" in settings.INSTALLED_APPS:
commands["tailwind"] = "django-admin tailwind watch"
if "tailwind" in settings.INSTALLED_APPS:
Expand All @@ -91,6 +95,7 @@ def manage() -> None:
@app.command()
def setup():
"""Run some project setup tasks"""
django.setup()
from django.core.management import execute_from_command_line
import os

Expand Down
38 changes: 23 additions & 15 deletions kitchenai/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

logger = logging.getLogger("kitchenai.core.utils")

def load_config_from_db():
config = {}
mgmt = KitchenAIManagement.objects.get(name="kitchenai_management")
def load_config_from_db(config: dict):
try:
mgmt = KitchenAIManagement.objects.get(name="kitchenai_management")

app = mgmt.kitchenaimodules_set.filter(is_root=True).first()
config["app"] = yaml.safe_load(app.name)
app = mgmt.kitchenaimodules_set.filter(is_root=True).first()
config["app"] = yaml.safe_load(app.name)
except KitchenAIManagement.DoesNotExist:
pass
return config

def update_installed_apps(self, apps):
Expand Down Expand Up @@ -50,25 +52,31 @@ def import_cookbook(module_path):



def setup(api: "NinjaAPI"):
def setup(api: "NinjaAPI", module: str = ""):
# # Load configuration from the database
config = load_config_from_db()
config = {}
config = load_config_from_db(config)
# Determine the user's project root directory (assumes the command is run from the user's project root)
project_root = os.getcwd()

# Add the user's project root directory to the Python path
if project_root not in sys.path:
sys.path.insert(0, project_root)

if not config:
logger.error('No configuration found. Please run "kitchenai init" first.')
return
logger.error('No configuration found. Checking dynamic module load')
if module:
logger.debug(f"importing module: {module}")
config["app"] = module
else:
logger.error("error not configured correctly. No module found in command or config")
return

# Update INSTALLED_APPS and import modules
# self.update_installed_apps(config.get('installed_apps', []))

# self.import_modules(config.get('module_paths', {}))

# Determine the user's project root directory (assumes the command is run from the user's project root)
project_root = os.getcwd()

# Add the user's project root directory to the Python path
if project_root not in sys.path:
sys.path.insert(0, project_root)

#importing main app
try:
Expand Down
3,047 changes: 3,046 additions & 1 deletion kitchenai/static/css/tailwind.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions kitchenai/templates/pages/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<div
class="grid grid-cols-12 grid-rows-[min-content] gap-y-12 p-4 lg:gap-x-12 lg:p-10">
<section
{% comment %} <section
class="stats stats-vertical col-span-12 w-full shadow-sm xl:stats-horizontal">
<div class="stat">
<div class="stat-title">Total Plugins</div>
Expand All @@ -33,7 +33,7 @@

{% endif %}
</div>
</section>
</section> {% endcomment %}
<!-- card -->
<section class="card col-span-12 col-start-4 col-end-8 overflow-hidden bg-base-200 ">
<h1 class="prose py-4 text-lg"> Under Construction. Adding more to your dashboard section in coming releases</h1>
Expand Down
6 changes: 2 additions & 4 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#
# This file is autogenerated by hatch-pip-compile with Python 3.11
#
Expand Down Expand Up @@ -1203,10 +1204,7 @@ pyzmq==26.2.0
# jupyter-server
questionary==1.10.0
# via bump-my-version
rcssmin==1.1.3
# via
# -c requirements.txt
# django-compressor

referencing==0.35.1
# via
# jsonschema
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,6 @@ pyyaml==6.0.2
# llama-index-core
# slippers
# uvicorn
rcssmin==1.1.3
# via django-compressor
refreshcss==0.5.1
# via hatch.envs.default
Expand Down

0 comments on commit f8ace41

Please sign in to comment.