hydraclick.core#

Attributes#

Functions#

wrap_kwargs_and_config(function[, as_kwargs, ...])

Wrap a function to run as a hydra command.

build_hydra_args(hydra_help, version, show_config, ...)

Compose the arguments for the hydra command.

get_default_dir()

Get the default directory for the hydra config.

run_hydra(function, hydra_args[, config_path, ...])

Run a function as a Hydra app.

command_api(function[, config_path, config_name, ...])

Integrate Hydra's configuration management capabilities with a Click-based CLI.

hydra_command([config_path, config_name, ...])

Integrate Hydra's configuration management capabilities with a Click-based CLI.

Module Contents#

hydraclick.core._logger#
hydraclick.core.FLOGGING_AVAILABLE = True#
hydraclick.core.wrap_kwargs_and_config(function, as_kwargs=False, print_config=True, preprocess_config=None, resolve=True)[source]#

Wrap a function to run as a hydra command.

Parameters:
  • function (Callable)

  • as_kwargs (bool)

  • print_config (bool)

  • preprocess_config (Callable[[omegaconf.DictConfig], omegaconf.DictConfig] | None)

  • resolve (bool)

hydraclick.core.build_hydra_args(hydra_help, version, show_config, resolve, package, info, run, multirun, config_path, config_name, config_dir, shell_completion, hydra_args=None)[source]#

Compose the arguments for the hydra command.

Parameters:
  • hydra_help (bool)

  • version (bool)

  • show_config (str)

  • resolve (bool)

  • package (str)

  • info (bool)

  • run (bool)

  • multirun (bool)

  • config_path (str)

  • config_name (str)

  • config_dir (str)

  • shell_completion (bool)

  • hydra_args (tuple[str, Ellipsis] | None)

Return type:

tuple[str, Ellipsis]

hydraclick.core.get_default_dir()[source]#

Get the default directory for the hydra config.

Return type:

str

hydraclick.core.run_hydra(function, hydra_args, config_path=None, config_name='config', version_base=None, use_flogging=True, **flogging_kwargs)[source]#

Run a function as a Hydra app.

Parameters:
  • function (Callable) – The function to be executed as a Hydra command. This function should accept a DictConfig object as its argument.

  • hydra_args (tuple[str, ...]) – The arguments to pass to the Hydra command.

  • config_path (str | None, optional) – The path to the configuration directory. If not specified, the default directory is used, which is the current working directory/config. Defaults to None.

  • config_name (str | None, optional) – The name of the configuration file (without the .yaml or .yml extension). Defaults to “config”.

  • version_base (str | None, optional) – The base version of the configuration. Defaults to None.

  • use_flogging (bool, optional) – Whether to use the flogging library for structured logging. Defaults to True.

  • **flogging_kwargs (Any, optional) – Additional keyword arguments to pass to the flogging.setup function.

Returns:

Then return value of the function.

Return type:

Any

hydraclick.core.command_api(function, config_path=None, config_name='config', version_base=None, as_kwargs=False, preprocess_config=None, print_config=True, resolve=True, use_flogging=True, terminal_effect=omegaconf.MISSING, **flogging_kwargs)[source]#

Integrate Hydra’s configuration management capabilities with a Click-based CLI.

Parameters:
  • function (Callable[[DictConfig], Any]) – The function to be executed as a Hydra command. This function should accept a DictConfig object as its argument.

  • config_path (str | Path | None, optional) – The path to the configuration directory. If not specified, the default directory is used.

  • config_name (str | None, optional) – The name of the configuration file (without the .yaml or .yml extension). Defaults to “config”.

  • version_base (str | None, optional) – The base version of the configuration. Defaults to None.

  • as_kwargs (bool, optional) – The mode in which to run the function. If True, the function is run with the configuration as keyword arguments. In this case the configuration is converted to a dictionary before passing it to the function. Defaults to False.

  • preprocess_config (Callable[[DictConfig], DictConfig] | None, optional) – A function to preprocess the configuration before passing it to the main function. Defaults to None.

  • print_config (bool, optional) – Whether to print the configuration before running the function. Defaults to True.

  • resolve (bool, optional) – Whether to resolve the configuration before running the function. Defaults to True.

  • use_flogging (bool, optional) – Whether to use the flogging library for structured logging. Defaults to True.

  • terminal_effect (Callable | None, optional) – The terminal effect function to use when rendering the command help.

  • **flogging_kwargs (Any, optional) – Additional keyword arguments to pass to the flogging.setup function.

Returns:

A Click-compatible command function that can be used as a CLI command.

Return type:

Callable

Example

```python from omegaconf import DictConfig

def my_function(config: DictConfig):

print(config.pretty())

click_command = command_api(

function=my_function, config_path=”path/to/config”, config_name=”my_config”, version_base=”1.0”, run_mode=”config”, preprocess_config=None, print_config=True, resolve=True, use_flogging=True, allow_trailing_dot=True

)#

In this example, my_function is wrapped by command_api to create a Click-compatible command. The configuration is loaded from the specified config_path and config_name, and the function is executed with the resolved configuration.

Notes

  • The command_api function uses several Hydra and Click decorators to provide a rich CLI experience.

  • If use_flogging is enabled but the flogging library is not available, a warning is logged, and flogging is disabled.

  • The preprocess_config function, if provided, allows for custom preprocessing of the configuration before it is passed to the main function.

hydraclick.core.hydra_command(config_path=None, config_name='config', version_base=None, as_kwargs=False, preprocess_config=None, print_config=True, resolve=True, use_flogging=True, terminal_effect=omegaconf.MISSING, **flogging_kwargs)[source]#

Integrate Hydra’s configuration management capabilities with a Click-based CLI.

Parameters:
  • config_path (str | Path | None, optional) – The path to the configuration directory. If not specified, the default directory is used.

  • config_name (str | None, optional) – The name of the configuration file (without the .yaml or .yml extension). Defaults to “config”.

  • version_base (str | None, optional) – The base version of the configuration. Defaults to None.

  • as_kwargs (bool, optional) – The mode in which to run the function. If True, the function is run with the configuration as keyword arguments. In this case the configuration is converted to a dictionary before passing it to the function. Defaults to False.

  • preprocess_config (Callable[[DictConfig], DictConfig] | None, optional) – A function to preprocess the configuration before passing it to the main function. Defaults to None.

  • print_config (bool, optional) – Whether to print the configuration before running the function. Defaults to True.

  • resolve (bool, optional) – Whether to resolve the configuration before running the function. Defaults to True.

  • use_flogging (bool, optional) – Whether to use the flogging library for structured logging. Defaults to True.

  • terminal_effect (Callable | None, optional) – The terminal effect function to use when rendering the command help.

  • **flogging_kwargs (Any, optional) – Additional keyword arguments to pass to the flogging.setup function.

Returns:

A Click-compatible command function that can be used as a CLI command.

Return type:

Callable

Notes

  • The command_api function uses several Hydra and Click decorators to provide a rich CLI experience.

  • If use_flogging is enabled but the flogging library is not available, a warning is logged, and flogging is disabled.

  • The preprocess_config function, if provided, allows for custom preprocessing of the configuration before it is passed to the main function.