Skip to content

Commit

Permalink
Fix port printing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-singhal committed Feb 15, 2025
1 parent 9756966 commit 5ed0584
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
10 changes: 8 additions & 2 deletions preswald/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from preswald.deploy import deploy as deploy_app
from preswald.deploy import stop as stop_app
from preswald.main import start_server
from preswald.utils import configure_logging, read_template
from preswald.utils import configure_logging, read_port_from_config, read_template


# Create a temporary directory for IPC
Expand Down Expand Up @@ -99,6 +99,7 @@ def run(script, port, log_level, disable_new_tab):

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)

url = f"http://localhost:{port}"
click.echo(f"Running '{script}' on {url} with log level {log_level} 🎉!")
Expand All @@ -112,6 +113,7 @@ def run(script, port, log_level, disable_new_tab):
except Exception as e:
click.echo(f"Error: {e}")


@cli.command()
@click.argument("script", default="app.py")
@click.option(
Expand Down Expand Up @@ -156,11 +158,14 @@ def deploy(script, target, port, log_level, github, api_key):

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)

if target == "structured":
click.echo("Starting production deployment... 🚀")
try:
for status_update in deploy_app(script, target, port=port, github_username=github, api_key=api_key):
for status_update in deploy_app(
script, target, port=port, github_username=github, api_key=api_key
):
status = status_update.get("status", "")
message = status_update.get("message", "")

Expand Down Expand Up @@ -340,6 +345,7 @@ def tutorial(ctx):
This command runs the tutorial app located in the package's tutorial directory.
"""
import preswald

package_dir = os.path.dirname(preswald.__file__)
tutorial_script = os.path.join(package_dir, "tutorial", "hello.py")

Expand Down
16 changes: 2 additions & 14 deletions preswald/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

logger = logging.getLogger(__name__)


def create_app(script_path: Optional[str] = None) -> FastAPI:
"""Create and configure the FastAPI application"""
app = FastAPI()
Expand Down Expand Up @@ -82,6 +83,7 @@ async def serve_spa(path: str):
"""Serve the SPA for any other routes"""
return await serve_index()


def _register_websocket_routes(app: FastAPI):
"""Register WebSocket routes"""

Expand Down Expand Up @@ -115,20 +117,6 @@ def start_server(script: Optional[str] = None, port: int = 8501):
"""Start the FastAPI server"""
app = create_app(script)

# Load port from config if available
if script:
try:
script_dir = os.path.dirname(script)
config_path = os.path.join(script_dir, "preswald.toml")
if os.path.exists(config_path):
import toml

config = toml.load(config_path)
if "project" in config and "port" in config["project"]:
port = config["project"]["port"]
except Exception as e:
logger.error(f"Error loading config: {e}")

config = uvicorn.Config(app, host="0.0.0.0", port=port, loop="asyncio")
server = uvicorn.Server(config)

Expand Down
19 changes: 15 additions & 4 deletions preswald/utils.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import logging
import toml
import os
import pkg_resources
from typing import Optional
import logging

import pkg_resources
import toml


def read_template(template_name):
"""Read content from a template file."""
template_path = pkg_resources.resource_filename(
"preswald", f"templates/{template_name}.template"
)
with open(template_path, "r") as f:
with open(template_path) as f:
return f.read()


def read_port_from_config(config_path: str, port: int):
try:
if os.path.exists(config_path):
config = toml.load(config_path)
if "project" in config and "port" in config["project"]:
port = config["project"]["port"]
return port
except Exception as e:
print(f"Warning: Could not load port config from {config_path}: {e}")


def configure_logging(config_path: Optional[str] = None, level: Optional[str] = None):
"""
Configure logging globally for the application.
Expand Down

0 comments on commit 5ed0584

Please sign in to comment.