Skip to content

Config

get_studio_config(dvc_studio_config=None, offline=False, studio_token=None, studio_repo_url=None, studio_url=None)

Get studio config options.


dvc_studio_config (Optional[dict]): Dict returned by dvc.Repo.config["studio"].
offline (bool): Whether offline mode is enabled. Default: false.
studio_token (Optional[str]): Studio access token obtained from the UI.
studio_repo_url (Optional[str]): URL of the Git repository that has been
    imported into Studio UI.
studio_url (Optional[str]): Base URL of Studio UI (if self-hosted).

Dict:
    Config options for posting live metrics.
    Keys match the DVC studio config section.

Example:

        {
            "token": "mytoken",
            "repo_url": "git@github.com:iterative/dvc-studio-client.git",
            "url": "https://studio.dvc.ai",
        }
Source code in dvc_studio_client/config.py
 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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def get_studio_config(
    dvc_studio_config: Optional[dict[str, Any]] = None,
    offline: bool = False,
    studio_token: Optional[str] = None,
    studio_repo_url: Optional[str] = None,
    studio_url: Optional[str] = None,
) -> dict[str, Any]:
    """Get studio config options.

    Args:
    ----
        dvc_studio_config (Optional[dict]): Dict returned by dvc.Repo.config["studio"].
        offline (bool): Whether offline mode is enabled. Default: false.
        studio_token (Optional[str]): Studio access token obtained from the UI.
        studio_repo_url (Optional[str]): URL of the Git repository that has been
            imported into Studio UI.
        studio_url (Optional[str]): Base URL of Studio UI (if self-hosted).

    Returns:
    -------
        Dict:
            Config options for posting live metrics.
            Keys match the DVC studio config section.

    Example:
    -------
                {
                    "token": "mytoken",
                    "repo_url": "git@github.com:iterative/dvc-studio-client.git",
                    "url": "https://studio.dvc.ai",
                }
    """
    config = {}
    if not dvc_studio_config:
        dvc_studio_config = {}

    def to_bool(var):
        if var is None:
            return False
        return bool(re.search("1|y|yes|true", str(var), flags=re.I))

    offline = (
        offline
        or to_bool(getenv(DVC_STUDIO_OFFLINE))
        or to_bool(dvc_studio_config.get("offline"))
    )
    if offline:
        logger.debug("Offline mode enabled. Skipping `post_studio_live_metrics`")
        return {}

    studio_token = (
        studio_token
        or getenv(DVC_STUDIO_TOKEN)
        or getenv(STUDIO_TOKEN)
        or dvc_studio_config.get("token")
    )
    if not studio_token:
        logger.debug(
            f"{DVC_STUDIO_TOKEN} not found. Skipping `post_studio_live_metrics`",
        )
        return {}
    config["token"] = studio_token

    studio_repo_url = (
        studio_repo_url
        or getenv(DVC_STUDIO_REPO_URL)
        or getenv(STUDIO_REPO_URL)
        or dvc_studio_config.get("repo_url")
    )
    if studio_repo_url is None:
        logger.debug(
            f"{DVC_STUDIO_REPO_URL} not found. Trying to automatically find it.",
        )
        studio_repo_url = get_studio_repo_url()
    if studio_repo_url:
        config["repo_url"] = studio_repo_url
    else:
        logger.debug(
            f"{DVC_STUDIO_REPO_URL} not found. Skipping `post_studio_live_metrics`",
        )
        return {}

    studio_url = studio_url or getenv(DVC_STUDIO_URL) or dvc_studio_config.get("url")
    if studio_url:
        config["url"] = studio_url
    else:
        logger.debug(f"{DVC_STUDIO_URL} not found. Using {DEFAULT_STUDIO_URL}.")
        config["url"] = DEFAULT_STUDIO_URL

    return config