Skip to content

Commit

Permalink
Merge pull request #23 from usegalaxy-au/local-render
Browse files Browse the repository at this point in the history
Local render Lab pages
  • Loading branch information
neoformit authored Jan 4, 2025
2 parents 7b1c6d2 + da79ade commit 61a57aa
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 24 deletions.
20 changes: 17 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"configurations": [
{
"name": "Django runserver",
"type": "python",
"type": "debugpy",
"purpose": ["debug-test"],
"request": "launch",
"program": "${workspaceFolder}/app/manage.py",
Expand All @@ -20,7 +20,7 @@
},
{
"name": "Django shell",
"type": "python",
"type": "debugpy",
"purpose": ["debug-test"],
"request": "launch",
"program": "${workspaceFolder}/app/manage.py",
Expand All @@ -34,7 +34,7 @@
},
{
"name": "Django test",
"type": "python",
"type": "debugpy",
"purpose": ["debug-test"],
"request": "launch",
"program": "${workspaceFolder}/app/manage.py",
Expand All @@ -46,5 +46,19 @@
"django": true,
"justMyCode": true,
},
{
"name": "Lab serve",
"type": "debugpy",
"purpose": ["debug-test"],
"request": "launch",
"program": "${workspaceFolder}/app/app/cli.py",
"cwd": "/home/cameron/dev/galaxy/galaxy_codex/communities/genome/lab",
"env": {
"GITHUB_API_TOKEN": "",
},
"args": ["serve"],
"django": true,
"justMyCode": true,
},
]
}
46 changes: 46 additions & 0 deletions app/app/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Command line interface for launching a development server.
This is intended to be used for rendering Lab pages in development.
"""

import os
import sys
from pathlib import Path


def main():
"""CLI entry point for running the development server."""
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Make sure it's installed and "
"available on your PYTHONPATH environment variable."
) from exc

os.environ["DJANGO_SETTINGS_MODULE"] = "app.settings.cli"
set_required_env_vars()

if sys.argv[1] == "serve":
BASE_DIR = Path(__file__).resolve().parent.parent
os.chdir(BASE_DIR)
sys.path.insert(0, str(BASE_DIR))
if len(sys.argv) > 2:
os.environ["LAB_CONTENT_ENTRYPOINT"] = sys.argv[2]
execute_from_command_line(["manage.py", "runserver"])
else:
print(f"Unknown command: {sys.argv[1]}")


def set_required_env_vars():
"""Set required environment variables for the CLI."""
os.environ.setdefault("LAB_CONTENT_ROOT", os.getcwd())
os.environ.setdefault("HOSTNAME", "localhost:8000")
os.environ.setdefault("MAIL_HOSTNAME", "localhost")
os.environ.setdefault("MAIL_SMTP_PORT", "22")
os.environ.setdefault("MAIL_FROM_ADDRESS", "[email protected]")
os.environ.setdefault("MAIL_TO_ADDRESS", "[email protected]")


if __name__ == "__main__":
main()
28 changes: 9 additions & 19 deletions app/app/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
"""
Django settings for app project.
Generated by 'django-admin startproject' using Django 3.2.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
*These settings are the base for all other settings. Variables declared here
may be overidden by the importing file.*
"""
"""Django settings for Labs Engine project."""

# flake8: noqa

import os
from pathlib import Path
from dotenv import load_dotenv
from utils.paths import ensure_dir

load_dotenv('../.env', override=True)
if os.getenv('DJANGO_SETTINGS_MODULE') != "app.settings.cli":
from dotenv import load_dotenv
load_dotenv('../.env', override=True)

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
CLI_DEV = False
AUTH_USER_MODEL = 'labs.User'
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY') or "secretkey"
SILENCED_SYSTEM_CHECKS = ['django_recaptcha.recaptcha_test_key_error']
Expand Down Expand Up @@ -193,9 +182,10 @@

GITHUB_API_TOKEN = os.getenv('GITHUB_API_TOKEN')
if not GITHUB_API_TOKEN:
print("Warning: GITHUB_API_TOKEN not set. Requests to api.github.com will"
" be rate-limited at 60 requests per hour which may result in"
" errors.")
print("\n\033[33mWarning: env variable GITHUB_API_TOKEN not set. Requests"
" to api.github.com will be rate-limited at 60 requests per hour"
" which may result in errors (this is used for fetching CONTRIBUTORS"
" information).\n\033[0m")

CACHE_TIMEOUT = 60 * 60 # 1 hour

Expand Down
32 changes: 32 additions & 0 deletions app/app/settings/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Local Lab development settings.
To be used when pip-installed and running as `labs-engine serve`.
"""

# flake8: noqa

import os

from .base import *

DEBUG = True
CLI_DEV = True
LAB_CONTENT_ROOT = os.environ.get('LAB_CONTENT_ROOT')
if not LAB_CONTENT_ROOT:
raise EnvironmentError('Env variable LAB_CONTENT_ROOT not set')
LAB_CONTENT_ENTRYPOINT = os.environ.get('LAB_CONTENT_ENTRYPOINT', 'base.yml')

INTERNAL_IPS = [
"127.0.0.1",
]

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

STATICFILES_DIRS = [
("local", LAB_CONTENT_ROOT),
]

DEFAULT_EXPORTED_LAB_CONTENT_ROOT = (
f"http://{HOSTNAME}/static/local/{LAB_CONTENT_ENTRYPOINT}")

INSTALLED_APPS.remove('django_light')
5 changes: 3 additions & 2 deletions app/labs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ def export_lab(request):
repo with a YAML file root which is specified as a GET parameter.
"""

if response := LabCache.get(request):
return response
if not settings.CLI_DEV:
if response := LabCache.get(request):
return response

template = 'labs/exported.html'

Expand Down
25 changes: 25 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from setuptools import setup, find_packages

setup(
name="galaxy-labs-engine",
version="1.0.0",
packages=find_packages(where='app'),
package_dir={'': 'app'},
include_package_data=True,
install_requires=[
"django>=4.0", # Add any dependencies here
"django>=5.0",
"pyyaml>=6.0",
"pydantic>=2.0",
"markdown2>=2.0",
"beautifulsoup4>=4.0",
"requests>=2.0",
"django-crispy-forms>=2.0",
"crispy-bootstrap5>=2024.0",
],
entry_points={
"console_scripts": [
"labs-engine=app.cli:main",
],
},
)

0 comments on commit 61a57aa

Please sign in to comment.