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

Provide an example of how to use clickify_parameters to create a command line application that can automagically be used as a stimela cab #5

Open
landmanbester opened this issue Dec 4, 2024 · 3 comments

Comments

@landmanbester
Copy link
Contributor

There were also a few requests for this one. Any takers?

@landmanbester
Copy link
Contributor Author

Here is a contrived example of how to use clickify_parameters. You need a main application where you create a command group e.g.

https://github.com/ratt-ru/pfb-imaging/blob/main/pfb/workers/main.py

Then import the cli inside your app like so

https://github.com/ratt-ru/pfb-imaging/blob/v0.0.5-dev/pfb/workers/init.py

You can define the arguments for the function in the function signature or use a separate yaml config in combination with something like the following

https://github.com/ratt-ru/pfb-imaging/blob/v0.0.5-dev/pfb/parser/schemas.py

where your configs are defined in the same folder e.g.

https://github.com/ratt-ru/pfb-imaging/blob/v0.0.5-dev/pfb/parser/init.yaml

But maybe not your simplest example!

@astronomike
Copy link

astronomike commented Dec 4, 2024

Group Nu's take on the 'hello' app using clickify_parameters:

contents of apps/hello.py

#! /usr/bin/python

import click
from pathlib import Path
from scabha.schema_utils import clickify_parameters
from omegaconf import OmegaConf

recipe = Path(__file__).parent.parent / "recipes/hello.yml"

schemas = OmegaConf.load(recipe)

@click.command("hello") 
@clickify_parameters(schemas.cabs.get("hello"))
def main(**kw):
    print(recipe)
    hello(**kw)

def hello(name):
    greeting = f"Hello {name}!"
    click.echo(greeting)
    return greeting

contents of recipes/hello.yml:

opts:
  log:
    dir: logs/log-{config.run.datetime}
    name: log-{info.fqname}
    nest: 2
    symlink: log

cabs:
  hello:  # cab
    info: this runs the clickified hello app 
    command: hello
    inputs:
      name:
        dtype: str
        required: true

and a pytest unit test:

from apps.hello import hello

def test_hello():
    assert hello("test") == f"Hello test!"

We can then just run hello --name Michael for example to get the program to run, using the following line in pyproject.toml:

[tool.poetry.scripts]
hello = "nu.apps.hello:main"

@landmanbester
Copy link
Contributor Author

Very clean and concise. Thanks @astronomike

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants