Skip to content

Commit

Permalink
feat: wip - cli
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanth-kumar-morem committed Feb 18, 2025
1 parent fa3949d commit 3a2f322
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
31 changes: 24 additions & 7 deletions preswald/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def init(name):


@cli.command()
@click.argument("script", default="hello.py")
@click.option("--port", default=8501, help="Port to run the server on.")
@click.option(
"--log-level",
Expand All @@ -96,17 +95,35 @@ def init(name):
default=False,
help="Disable automatically opening a new browser tab",
)
def run(script, port, log_level, disable_new_tab):
def run(port, log_level, disable_new_tab):
"""
Run a Preswald app.
Run a Preswald app from the current directory.
By default, it runs the `hello.py` script on localhost:8501.
Looks for preswald.toml in the current directory and runs the script specified in the entrypoint.
"""
config_path = "preswald.toml"
if not os.path.exists(config_path):
click.echo("Error: preswald.toml not found in current directory. ❌")
click.echo("Make sure you're in a Preswald project directory.")
return

import tomli
try:
with open(config_path, "rb") as f:
config = tomli.load(f)
except Exception as e:
click.echo(f"Error reading preswald.toml: {e} ❌")
return

if "project" not in config or "entrypoint" not in config["project"]:
click.echo("Error: entrypoint not defined in preswald.toml under [project] section. ❌")
return

script = config["project"]["entrypoint"]
if not os.path.exists(script):
click.echo(f"Error: Script '{script}' not found. ❌")
click.echo(f"Error: Entrypoint script '{script}' not found. ❌")
return

config_path = os.path.join(os.path.dirname(script), "preswald.toml")
log_level = configure_logging(config_path=config_path, level=log_level)
port = read_port_from_config(config_path=config_path, port=port)

Expand Down Expand Up @@ -375,7 +392,7 @@ def tutorial(ctx):
click.echo("🚀 Launching the Preswald tutorial app! 🎉")

# Invoke the 'run' command with the tutorial script path
ctx.invoke(run, script=tutorial_script)
ctx.invoke(run, port=8501)


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions preswald/templates/preswald.toml.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title = "Preswald Project"
version = "0.1.0"
port = 8501
slug = "preswald-project" # Required: Unique identifier for your project
entrypoint = "hello.py" # Required: Main script to run when executing preswald run

[branding]
name = "Preswald Project"
Expand Down

0 comments on commit 3a2f322

Please sign in to comment.