diff --git a/wandb/errors/term.py b/wandb/errors/term.py index 9d622b3d..e4199fbb 100644 --- a/wandb/errors/term.py +++ b/wandb/errors/term.py @@ -3,6 +3,7 @@ LOG_STRING = click.style('wandb', fg='blue', bold=True) +LOG_STRING_NOCOLOR = 'wandb' ERROR_STRING = click.style('ERROR', bg='red', fg='green') WARN_STRING = click.style('WARNING', fg='yellow') PRINTED_MESSAGES = set() diff --git a/wandb/lib/apikey.py b/wandb/lib/apikey.py index 3cf595fe..f3bd6164 100644 --- a/wandb/lib/apikey.py +++ b/wandb/lib/apikey.py @@ -13,7 +13,7 @@ from six.moves import input import wandb from wandb.apis import InternalApi -from wandb.errors.term import LOG_STRING +from wandb.errors import term from wandb.util import isatty @@ -43,7 +43,7 @@ def _prompt_choice(): return ( int( input( - "%s: Enter your choice: " % LOG_STRING + "%s: Enter your choice: " % term.LOG_STRING ) ) - 1 # noqa: W503 @@ -69,6 +69,7 @@ def prompt_api_key( # noqa: C901 False - if unconfigured (notty) """ input_callback = input_callback or getpass.getpass + log_string = term.LOG_STRING api = api or InternalApi() anon_mode = _fixup_anon_mode(settings.anonymous) jupyter = settings._jupyter or False @@ -84,6 +85,7 @@ def prompt_api_key( # noqa: C901 choices.remove(LOGIN_CHOICE_NEW) if jupyter and 'google.colab' in sys.modules: + log_string = term.LOG_STRING_NOCOLOR key = wandb.jupyter.attempt_colab_login(api.app_url) if key is not None: write_key(settings, key) @@ -110,6 +112,7 @@ def prompt_api_key( # noqa: C901 result = choices[idx] wandb.termlog("You chose '%s'" % result) + api_ask = "%s: Paste an API key from your profile and hit enter: " % log_string if result == LOGIN_CHOICE_ANON: key = api.create_anonymous_api_key() @@ -122,10 +125,7 @@ def prompt_api_key( # noqa: C901 wandb.termlog( "Create an account here: {}/authorize?signup=true".format(app_url) ) - key = input_callback( - "%s: Paste an API key from your profile and hit enter" - % LOG_STRING - ).strip() + key = input_callback(api_ask).strip() write_key(settings, key) return key @@ -138,10 +138,7 @@ def prompt_api_key( # noqa: C901 app_url ) ) - key = input_callback( - "%s: Paste an API key from your profile and hit enter" - % LOG_STRING - ).strip() + key = input_callback(api_ask).strip() write_key(settings, key) return key elif result == LOGIN_CHOICE_NOTTY: diff --git a/wandb/lib/server.py b/wandb/lib/server.py index 1320c69d..7cb6d35d 100644 --- a/wandb/lib/server.py +++ b/wandb/lib/server.py @@ -13,8 +13,8 @@ class ServerError(Exception): class Server(object): - def __init__(self, api=None): - self._api = api or InternalApi() + def __init__(self, api=None, settings=None): + self._api = api or InternalApi(default_settings=settings) self._error_network = None self._viewer = {} self._flags = {} diff --git a/wandb/sdk/wandb_login.py b/wandb/sdk/wandb_login.py index dd57e598..fca70833 100644 --- a/wandb/sdk/wandb_login.py +++ b/wandb/sdk/wandb_login.py @@ -16,6 +16,9 @@ logger = logging.getLogger("wandb") +if wandb.TYPE_CHECKING: # type: ignore + from typing import Dict # noqa: F401 pylint: disable=unused-import + def _validate_anonymous_setting(anon_str): return anon_str in ["must", "allow", "never"] @@ -55,7 +58,7 @@ def _login( wandb.termwarn("Calling wandb.login() after wandb.init() is a no-op.") return True - settings = {} + settings_dict: Dict = {} api = Api() if anonymous is not None: @@ -66,14 +69,20 @@ def _login( "wandb.login(). Can be 'must', 'allow', or 'never'." ) return False - settings.update({"anonymous": anonymous}) + settings_dict.update({"anonymous": anonymous}) + + if key: + settings_dict.update({"api_key": key}) # Note: This won't actually do anything if called from a codepath where # wandb.setup was previously called. If wandb.setup is called further up, # you must make sure the anonymous setting (and any other settings) are # already properly set up there. - wl = wandb.setup() - settings = _settings or wl.settings() + wl = wandb.setup(settings=wandb.Settings(**settings_dict)) + wl_settings = wl.settings() + if _settings: + wl_settings._apply_settings(settings=_settings) + settings = wl_settings if settings._offline: return False diff --git a/wandb/sdk/wandb_setup.py b/wandb/sdk/wandb_setup.py index 1bc34953..fba4edc0 100644 --- a/wandb/sdk/wandb_setup.py +++ b/wandb/sdk/wandb_setup.py @@ -89,7 +89,7 @@ def __init__(self, settings=None, environ=None): _set_logger(self._early_logger) # Have to load viewer before setting up settings. - self._load_viewer() + self._load_viewer(settings=settings) self._settings_setup(settings, self._early_logger) self._settings.freeze() @@ -150,8 +150,8 @@ def _get_entity(self): def _get_user_flags(self): return self._server._flags - def _load_viewer(self): - s = server.Server() + def _load_viewer(self, settings=None): + s = server.Server(settings=settings) s.query_with_timeout() self._server = s # if self.mode != "dryrun" and not self._api.disabled() and self._api.api_key: diff --git a/wandb/sdk_py27/wandb_login.py b/wandb/sdk_py27/wandb_login.py index 541e03c8..fcc0486d 100644 --- a/wandb/sdk_py27/wandb_login.py +++ b/wandb/sdk_py27/wandb_login.py @@ -16,6 +16,9 @@ logger = logging.getLogger("wandb") +if wandb.TYPE_CHECKING: # type: ignore + from typing import Dict # noqa: F401 pylint: disable=unused-import + def _validate_anonymous_setting(anon_str): return anon_str in ["must", "allow", "never"] @@ -55,7 +58,7 @@ def _login( wandb.termwarn("Calling wandb.login() after wandb.init() is a no-op.") return True - settings = {} + settings_dict = {} api = Api() if anonymous is not None: @@ -66,14 +69,20 @@ def _login( "wandb.login(). Can be 'must', 'allow', or 'never'." ) return False - settings.update({"anonymous": anonymous}) + settings_dict.update({"anonymous": anonymous}) + + if key: + settings_dict.update({"api_key": key}) # Note: This won't actually do anything if called from a codepath where # wandb.setup was previously called. If wandb.setup is called further up, # you must make sure the anonymous setting (and any other settings) are # already properly set up there. - wl = wandb.setup() - settings = _settings or wl.settings() + wl = wandb.setup(settings=wandb.Settings(**settings_dict)) + wl_settings = wl.settings() + if _settings: + wl_settings._apply_settings(settings=_settings) + settings = wl_settings if settings._offline: return False diff --git a/wandb/sdk_py27/wandb_setup.py b/wandb/sdk_py27/wandb_setup.py index 5e7456c6..52976ac4 100644 --- a/wandb/sdk_py27/wandb_setup.py +++ b/wandb/sdk_py27/wandb_setup.py @@ -89,7 +89,7 @@ def __init__(self, settings=None, environ=None): _set_logger(self._early_logger) # Have to load viewer before setting up settings. - self._load_viewer() + self._load_viewer(settings=settings) self._settings_setup(settings, self._early_logger) self._settings.freeze() @@ -150,8 +150,8 @@ def _get_entity(self): def _get_user_flags(self): return self._server._flags - def _load_viewer(self): - s = server.Server() + def _load_viewer(self, settings=None): + s = server.Server(settings=settings) s.query_with_timeout() self._server = s # if self.mode != "dryrun" and not self._api.disabled() and self._api.api_key: