Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rapida/cli/connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async def connectivity(ctx, bbox:tuple[float, float, float, float]=None, travel_
disjoint:bool=False, smooth:bool=True
):
logger.info(f'Running connectivity analysis')
progress = ctx.obj.get('progress')
progress = ctx.obj.get('progress') if ctx.obj else None
with progress:
return await run_connectivity_analysis(
bbox=bbox, dst_dir=dst_dir, travel_mode=travel_mode, time_intervals=time_intervals,
Expand Down
41 changes: 40 additions & 1 deletion rapida/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import click
from rapida.cli import cli

def run_cmd(rapida_command_name:str = None, command_string: str = None):
def run_cmd_jin(rapida_command_name:str = None, command_string: str = None):
"""
Invoke a Click entry‐point programmatically using a shell‐style string.

Expand All @@ -29,3 +29,42 @@ def run_cmd(rapida_command_name:str = None, command_string: str = None):
raise click.ClickException(
f"Command `{command_string}` exited with status {e.code}."
)


def run_cmd(rapida_command_name: str = None, command_string: str = None):
"""
Invoke a Click entry-point programmatically in Jupyter.
Handles no-arg help triggers, parameter errors, and async execution safely.
"""
if not rapida_command_name:
raise ValueError("A command name must be provided (e.g., run_cmd('connectivity')).")

# Guard against None for command_string
args = shlex.split(command_string) if command_string else []
rapida_command_names = list(cli.list_commands(None))

if "auth" in rapida_command_names:
rapida_command_names.remove("auth")

assert rapida_command_name in rapida_command_names, (
f"Invalid command '{rapida_command_name}'. Valid options are: {', '.join(rapida_command_names)}"
)

full_args = [rapida_command_name] + args

try:
# Run through root group so context (ctx.obj['progress']) is initialized
return cli.main(args=full_args, standalone_mode=False)

except SystemExit as e:
# Exit code 0 indicates normal termination (e.g., --help or no_args_is_help)
if e.code != 0:
raise click.ClickException(
f"Command `{rapida_command_name} {command_string or ''}` exited with status {e.code}."
)
return None

except click.ClickException as e:
# Formats Click usage/validation errors (e.g., missing required options) cleanly
e.show()
return None
Loading