File size: 2,419 Bytes
fc0f7bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | ---
id: compose_api
title: Compose API
sidebar_label: Compose API
---
The compose API can compose a configuration similar to `@hydra.main()` anywhere in the code.
Prior to calling compose(), you have to initialize Hydra: This can be done by using the standard `@hydra.main()`
or by calling one of the initialization methods listed below.
Compose is useful when `@hydra.main()` is not applicable.
### Examples
- [Jupyter notebook with compose](https://github.com/facebookresearch/hydra/tree/master/examples/notebook) (hydra_notebook_example.ipynb)
- [Unit testing with compose](https://github.com/facebookresearch/hydra/tree/master/examples/advanced/hydra_app_example/tests/test_hydra_app.py).
### Code example
```python
from hydra.experimental import compose, initialize
if __name__ == "__main__":
initialize(config_path="conf")
cfg = compose(config_name="config", overrides=["db=mysql", "db.user=me"])
print(cfg.pretty())
```
### API Documentation
```python
def compose(config_file=None, overrides=[]):
"""
:param config_file: optional config file to load
:param overrides: list of overrides for config file
:return: the composed config
"""
...
def initialize(
config_path: Optional[str] = None,
strict: Optional[bool] = None,
caller_stack_depth: int = 1,
) -> None:
"""
Initialize automatically detect the calling file or module.
config_path is relative to the detected calling for or module.
:param config_path: A directory relative to the declaring python file or module
:param strict: (Deprecated), will be removed in the next major version
:param caller_stack_depth: stack depth of module the config_path is relative to
"""
def initialize_with_file(
file: Optional[str], config_path: Optional[str] = None
) -> None:
"""
Initialize Hydra and add the config_path to the search path.
The config path is relative to the calling_file.
:param file : The file to make the config_path relative to
:param config_path : The config path
"""
...
def initialize_with_module(
module: Optional[str], config_path: Optional[str] = None
) -> None:
"""
Initialize Hydra and add the config_path to the search path.
The config path is relative to the calling_module.
:param module : The module to make the config_path relative to
:param config_path : The config path
"""
...
```
|