Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the 'kfp experiment' commands #3705

Merged
merged 9 commits into from
May 20, 2020
2 changes: 2 additions & 0 deletions sdk/python/kfp/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from .run import run
from .pipeline import pipeline
from .diagnose_me_cli import diagnose_me
from .experiment import experiment

@click.group()
@click.option('--endpoint', help='Endpoint of the KFP API service to connect.')
Expand All @@ -40,6 +41,7 @@ def main():
cli.add_command(run)
cli.add_command(pipeline)
cli.add_command(diagnose_me,'diagnose_me')
cli.add_command(experiment)
try:
cli(obj={}, auto_envvar_prefix='KFP')
except Exception as e:
Expand Down
89 changes: 89 additions & 0 deletions sdk/python/kfp/cli/experiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import click
import logging
from tabulate import tabulate


@click.group()
def experiment():
"""Manage experiment resources"""
pass


@experiment.command()
@click.option(
'--description',
help="Description of the experiment."
)
@click.argument("name")
@click.pass_context
def create(ctx, description, name):
"""Create an experiment"""
client = ctx.obj["client"]

response = client.create_experiment(name, description=description)
logging.info("Experiment {} has been submitted\n".format(name))
_display_experiment(response)


@experiment.command()
@click.option(
'--max-size',
default=100,
help="Max size of the listed experiments."
)
@click.pass_context
def list(ctx, max_size):
"""List experiments"""
client = ctx.obj['client']

response = client.list_experiments(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client.list_experiments() method used here is actually a wrapper around our backend api method client.experimetns.list_experiment(). I personally prefer to use the backend api method directly, since the wrapper method client.list_experments() in _client.py might not get timely updates/maintenance as backend api method.

E.g., in your case, could you try if the following works?
client = ctx.obj['client']
client.experiments.list_experiment(page_size=max_size, sort_by='created_at desc')

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. That makes sense. I'll modify code to use the api methods directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed code in 5b16b1a.

page_size=max_size,
sort_by="created_at desc"
)
if response.experiments:
_display_experiments(response.experiments)
else:
logging.info("No experiments found")


@experiment.command()
@click.argument("experiment-id")
@click.pass_context
def get(ctx, experiment_id):
"""Get detailed information about an experiment"""
client = ctx.obj["client"]

response = client.get_experiment(experiment_id)
_display_experiment(response)


@experiment.command()
@click.argument("experiment-id")
@click.pass_context
def delete(ctx, experiment_id):
"""Delete an experiment"""
client = ctx.obj["client"]

client.experiments.delete_experiment(id=experiment_id)
print("{} is deleted.".format(experiment_id))


def _display_experiments(experiments):
headers = ["Experiment ID", "Name", "Created at"]
data = [[
exp.id,
exp.name,
exp.created_at.isoformat()
] for exp in experiments]
print(tabulate(data, headers=headers, tablefmt="grid"))


def _display_experiment(exp):
print(tabulate([], headers=["Experiment Details"]))
table = [
["ID", exp.id],
["Name", exp.name],
["Description", exp.description],
["Created at", exp.created_at.isoformat()],
]
print(tabulate(table, tablefmt="plain"))