Skip to content

Commit

Permalink
[python] rename --type and add --params
Browse files Browse the repository at this point in the history
  • Loading branch information
javierluraschi committed Jan 25, 2025
1 parent 01962a8 commit 223458f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.8.6

- Renamed `--runtime` to `--type` in `hal9 run`
- Added support for `--params` in `hal9 run`

## 2.8.5

- Fix regression in `ready()` function
Expand Down
10 changes: 6 additions & 4 deletions python/hal9/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@ def create(path :str, template :str):
@click.command()
@click.argument('path')
@click.option('--source', default=None, help='Main source file')
@click.option('--runtime', default=None, help='Runtime to use')
@click.option('--type', default=None, help='Type of runtime to use')
@click.option('--port', default="8080", help='Port to use, optional')
def run(path :str, source :str = "app.py", runtime :str = "type", port :str = "8080"):
@click.option('--params', default=None, help='Params to use, optional')
def run(path :str, source :str = "app.py", type :str = "type", port :str = "8080", params :str = None):
"""
Run Project
--path: The path to the project. Required argument.
--source: The main source file to run. Defaults to 'app.py'.
--runtime: The type of content to run. Defaults to 'python'.
--type: The type of content to run. Defaults to 'python'.
--port: The port to use when content requires one. Defaults to '8080'.
--params: An optional JSON string with additional parameters.
"""
api_run(path, source, runtime, port)
api_run(path, source, type, port, params)

@click.command()
@click.argument('path')
Expand Down
14 changes: 8 additions & 6 deletions python/hal9/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"plumber": run_plumber,
}

def run(path :str, source :str = "app.py", runtime :str = "python", port :str = "8080") -> str:
def run(path :str, source :str = "app.py", type :str = "python", port :str = "8080", params :str = None) -> str:
"""Run an application
Parameters
Expand All @@ -26,8 +26,10 @@ def run(path :str, source :str = "app.py", runtime :str = "python", port :str =
Path to the application.
source : str
The main file to run. Defaults to 'app.py'.
runtime : str
The runtime to use to run the source. Defaults to 'python'.
type : str
The type of runtime to use to run the source. Defaults to 'python'.
params : str
An optional JSON string with a dictionary of additional parameters.
"""

source_path = Path(path) / source
Expand All @@ -37,10 +39,10 @@ def run(path :str, source :str = "app.py", runtime :str = "python", port :str =
return

try:
if runtime in runtime_types:
runtime_types[runtime](source_path, port)
if type in runtime_types:
runtime_types[type](source_path, port, params)
else:
print(f"Unsupported runtime: {runtime}")
print(f"Unsupported runtime: {type}")
except Exception as e:
print(f"An error occurred while running {source}: {e}")

Expand Down
31 changes: 30 additions & 1 deletion python/hal9/runtimes/chainlit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
import subprocess
from pathlib import Path
import time
import configparser
import json

def handle_params(params :str):
parsed_params = json.loads(params)

try:
result = subprocess.run(["chainlit", "init"], check=False, stderr=subprocess.DEVNULL)
except Exception as e:
print(f"Ignoring error: {e}")

config_file = Path(".chainlit/config.toml")
if config_file.exists():
config = configparser.ConfigParser()
config.read(config_file)

if 'UI' not in config:
config['UI'] = {}

if parsed_params.get("dark") is False:
config['UI']['default_theme'] = '"light"'

with open(config_file, 'w') as configfile:
config.write(configfile)
else:
print(f"Config file {config_file} does not exist. Please create it before running this script.")

def run(source_path: Path, port :str, params :str):
if params:
handle_params(params)

def run(source_path: Path, port :str):
command = ['chainlit', 'run', '-h', '--port', port, source_path]
print(command)

Expand Down
2 changes: 1 addition & 1 deletion python/hal9/runtimes/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
import time

def run(source_path: Path, port :str):
def run(source_path: Path, port :str, params :str):
image_directory = source_path.parent
image_name = source_path.name

Expand Down
2 changes: 1 addition & 1 deletion python/hal9/runtimes/plumber.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess
from pathlib import Path

def run(source_path :Path, port :str):
def run(source_path :Path, port :str, params :str):
code = f"library(plumber);pr_run(pr('{source_path}'), port={port})"
command = ['Rscript', '-e', code]
with subprocess.Popen(command) as proc:
Expand Down
2 changes: 1 addition & 1 deletion python/hal9/runtimes/python.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess
from pathlib import Path

def run(source_path :Path, port :str):
def run(source_path :Path, port :str, params :str):
command = ['python3', str(source_path)]
with subprocess.Popen(command) as proc:
proc.wait()
2 changes: 1 addition & 1 deletion python/hal9/runtimes/r.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess
from pathlib import Path

def run(source_path :Path, port :str):
def run(source_path :Path, port :str, params :str):
rprofile_content = f"""
options(shiny.port = {port})
"""
Expand Down
3 changes: 2 additions & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hal9"
version = "2.8.5"
version = "2.8.6"
description = ""
authors = ["Javier Luraschi <[email protected]>"]
readme = "README.md"
Expand All @@ -9,6 +9,7 @@ include = ["templates/openai/app.py", "templates/docker/Dockerfile"]
[tool.poetry.dependencies]
python = ">=3.8"
requests = "^2.28.2"
configparser = "7.1.0"

click = "^8.1.7"
[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 223458f

Please sign in to comment.