|
1 | | -"""Command Line Interface for `openml` to configure its settings.""" |
| 1 | +"""Command Line Interface for `openml` to configure its settings and browse resources.""" |
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
|
9 | 9 | from pathlib import Path |
10 | 10 | from urllib.parse import urlparse |
11 | 11 |
|
| 12 | +import openml |
12 | 13 | from openml import config |
13 | 14 | from openml.__version__ import __version__ |
14 | 15 |
|
@@ -300,6 +301,40 @@ def configure_field( # noqa: PLR0913 |
300 | 301 | verbose_set(field, value) |
301 | 302 |
|
302 | 303 |
|
| 304 | +def list_flows_cli(args: argparse.Namespace) -> None: |
| 305 | + """List OpenML flows with optional filtering.""" |
| 306 | + df = openml.flows.list_flows( |
| 307 | + offset=args.offset, |
| 308 | + size=args.size, |
| 309 | + tag=args.tag, |
| 310 | + uploader=args.uploader, |
| 311 | + ) |
| 312 | + if df.empty: |
| 313 | + print("No flows found matching the given criteria.") |
| 314 | + else: |
| 315 | + print(df.to_string()) |
| 316 | + |
| 317 | + |
| 318 | +def info_flow_cli(args: argparse.Namespace) -> None: |
| 319 | + """Display detailed information about a specific OpenML flow.""" |
| 320 | + flow = openml.flows.get_flow(args.flow_id) |
| 321 | + print(flow) |
| 322 | + |
| 323 | + |
| 324 | +def handle_flows(args: argparse.Namespace) -> None: |
| 325 | + """Dispatch flows subcommands.""" |
| 326 | + actions = { |
| 327 | + "list": list_flows_cli, |
| 328 | + "info": info_flow_cli, |
| 329 | + } |
| 330 | + action = getattr(args, "flows_action", None) |
| 331 | + if action is None: |
| 332 | + # Print help when no subcommand is given |
| 333 | + args._parser_flows.print_help() |
| 334 | + else: |
| 335 | + actions[action](args) |
| 336 | + |
| 337 | + |
303 | 338 | def configure(args: argparse.Namespace) -> None: |
304 | 339 | """Calls the right submenu(s) to edit `args.field` in the configuration file.""" |
305 | 340 | set_functions = { |
@@ -329,7 +364,7 @@ def not_supported_yet(_: str) -> None: |
329 | 364 |
|
330 | 365 |
|
331 | 366 | def main() -> None: |
332 | | - subroutines = {"configure": configure} |
| 367 | + subroutines = {"configure": configure, "flows": handle_flows} |
333 | 368 |
|
334 | 369 | parser = argparse.ArgumentParser() |
335 | 370 | # Add a global --version flag to display installed version and exit |
@@ -368,7 +403,55 @@ def main() -> None: |
368 | 403 | help="The value to set the FIELD to.", |
369 | 404 | ) |
370 | 405 |
|
| 406 | + # --- flows subcommand --- |
| 407 | + parser_flows = subparsers.add_parser( |
| 408 | + "flows", |
| 409 | + description="Browse and search OpenML flows (models).", |
| 410 | + ) |
| 411 | + flows_subparsers = parser_flows.add_subparsers(dest="flows_action") |
| 412 | + |
| 413 | + parser_flows_list = flows_subparsers.add_parser( |
| 414 | + "list", |
| 415 | + description="List OpenML flows with optional filtering.", |
| 416 | + ) |
| 417 | + parser_flows_list.add_argument( |
| 418 | + "--size", |
| 419 | + type=int, |
| 420 | + default=10, |
| 421 | + help="Maximum number of flows to return (default: 10).", |
| 422 | + ) |
| 423 | + parser_flows_list.add_argument( |
| 424 | + "--offset", |
| 425 | + type=int, |
| 426 | + default=None, |
| 427 | + help="Number of flows to skip, for pagination.", |
| 428 | + ) |
| 429 | + parser_flows_list.add_argument( |
| 430 | + "--tag", |
| 431 | + type=str, |
| 432 | + default=None, |
| 433 | + help="Only list flows with this tag.", |
| 434 | + ) |
| 435 | + parser_flows_list.add_argument( |
| 436 | + "--uploader", |
| 437 | + type=str, |
| 438 | + default=None, |
| 439 | + help="Only list flows uploaded by this user.", |
| 440 | + ) |
| 441 | + |
| 442 | + parser_flows_info = flows_subparsers.add_parser( |
| 443 | + "info", |
| 444 | + description="Display detailed information about a specific flow.", |
| 445 | + ) |
| 446 | + parser_flows_info.add_argument( |
| 447 | + "flow_id", |
| 448 | + type=int, |
| 449 | + help="The ID of the flow to display.", |
| 450 | + ) |
| 451 | + |
371 | 452 | args = parser.parse_args() |
| 453 | + # Attach parser_flows so handle_flows can print help when no action is given |
| 454 | + args._parser_flows = parser_flows |
372 | 455 | subroutines.get(args.subroutine, lambda _: parser.print_help())(args) |
373 | 456 |
|
374 | 457 |
|
|
0 commit comments