Skip to content

Commit 223458f

Browse files
[python] rename --type and add --params
1 parent 01962a8 commit 223458f

File tree

9 files changed

+55
-16
lines changed

9 files changed

+55
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 2.8.6
4+
5+
- Renamed `--runtime` to `--type` in `hal9 run`
6+
- Added support for `--params` in `hal9 run`
7+
38
## 2.8.5
49

510
- Fix regression in `ready()` function

python/hal9/cli.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,20 @@ def create(path :str, template :str):
3636
@click.command()
3737
@click.argument('path')
3838
@click.option('--source', default=None, help='Main source file')
39-
@click.option('--runtime', default=None, help='Runtime to use')
39+
@click.option('--type', default=None, help='Type of runtime to use')
4040
@click.option('--port', default="8080", help='Port to use, optional')
41-
def run(path :str, source :str = "app.py", runtime :str = "type", port :str = "8080"):
41+
@click.option('--params', default=None, help='Params to use, optional')
42+
def run(path :str, source :str = "app.py", type :str = "type", port :str = "8080", params :str = None):
4243
"""
4344
Run Project
4445
4546
--path: The path to the project. Required argument.
4647
--source: The main source file to run. Defaults to 'app.py'.
47-
--runtime: The type of content to run. Defaults to 'python'.
48+
--type: The type of content to run. Defaults to 'python'.
4849
--port: The port to use when content requires one. Defaults to '8080'.
50+
--params: An optional JSON string with additional parameters.
4951
"""
50-
api_run(path, source, runtime, port)
52+
api_run(path, source, type, port, params)
5153

5254
@click.command()
5355
@click.argument('path')

python/hal9/run.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"plumber": run_plumber,
1818
}
1919

20-
def run(path :str, source :str = "app.py", runtime :str = "python", port :str = "8080") -> str:
20+
def run(path :str, source :str = "app.py", type :str = "python", port :str = "8080", params :str = None) -> str:
2121
"""Run an application
2222
2323
Parameters
@@ -26,8 +26,10 @@ def run(path :str, source :str = "app.py", runtime :str = "python", port :str =
2626
Path to the application.
2727
source : str
2828
The main file to run. Defaults to 'app.py'.
29-
runtime : str
30-
The runtime to use to run the source. Defaults to 'python'.
29+
type : str
30+
The type of runtime to use to run the source. Defaults to 'python'.
31+
params : str
32+
An optional JSON string with a dictionary of additional parameters.
3133
"""
3234

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

3941
try:
40-
if runtime in runtime_types:
41-
runtime_types[runtime](source_path, port)
42+
if type in runtime_types:
43+
runtime_types[type](source_path, port, params)
4244
else:
43-
print(f"Unsupported runtime: {runtime}")
45+
print(f"Unsupported runtime: {type}")
4446
except Exception as e:
4547
print(f"An error occurred while running {source}: {e}")
4648

python/hal9/runtimes/chainlit.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
11
import subprocess
22
from pathlib import Path
33
import time
4+
import configparser
5+
import json
6+
7+
def handle_params(params :str):
8+
parsed_params = json.loads(params)
9+
10+
try:
11+
result = subprocess.run(["chainlit", "init"], check=False, stderr=subprocess.DEVNULL)
12+
except Exception as e:
13+
print(f"Ignoring error: {e}")
14+
15+
config_file = Path(".chainlit/config.toml")
16+
if config_file.exists():
17+
config = configparser.ConfigParser()
18+
config.read(config_file)
19+
20+
if 'UI' not in config:
21+
config['UI'] = {}
22+
23+
if parsed_params.get("dark") is False:
24+
config['UI']['default_theme'] = '"light"'
25+
26+
with open(config_file, 'w') as configfile:
27+
config.write(configfile)
28+
else:
29+
print(f"Config file {config_file} does not exist. Please create it before running this script.")
30+
31+
def run(source_path: Path, port :str, params :str):
32+
if params:
33+
handle_params(params)
434

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

python/hal9/runtimes/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pathlib import Path
33
import time
44

5-
def run(source_path: Path, port :str):
5+
def run(source_path: Path, port :str, params :str):
66
image_directory = source_path.parent
77
image_name = source_path.name
88

python/hal9/runtimes/plumber.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import subprocess
22
from pathlib import Path
33

4-
def run(source_path :Path, port :str):
4+
def run(source_path :Path, port :str, params :str):
55
code = f"library(plumber);pr_run(pr('{source_path}'), port={port})"
66
command = ['Rscript', '-e', code]
77
with subprocess.Popen(command) as proc:

python/hal9/runtimes/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import subprocess
22
from pathlib import Path
33

4-
def run(source_path :Path, port :str):
4+
def run(source_path :Path, port :str, params :str):
55
command = ['python3', str(source_path)]
66
with subprocess.Popen(command) as proc:
77
proc.wait()

python/hal9/runtimes/r.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import subprocess
22
from pathlib import Path
33

4-
def run(source_path :Path, port :str):
4+
def run(source_path :Path, port :str, params :str):
55
rprofile_content = f"""
66
options(shiny.port = {port})
77
"""

python/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "hal9"
3-
version = "2.8.5"
3+
version = "2.8.6"
44
description = ""
55
authors = ["Javier Luraschi <[email protected]>"]
66
readme = "README.md"
@@ -9,6 +9,7 @@ include = ["templates/openai/app.py", "templates/docker/Dockerfile"]
99
[tool.poetry.dependencies]
1010
python = ">=3.8"
1111
requests = "^2.28.2"
12+
configparser = "7.1.0"
1213

1314
click = "^8.1.7"
1415
[tool.poetry.group.dev.dependencies]

0 commit comments

Comments
 (0)