Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions dvc/command/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ class CmdExperimentsInit(CmdBase):
DEFAULT_NAME = "default"

@post_processing(dict)
def init_interactive(self, defaults=None):
def init_interactive(self, defaults=None, show_heading: bool = False):
defaults = defaults or {}
prompts = {
"cmd": "[b]Command[/b] to execute",
Expand All @@ -856,10 +856,14 @@ def init_interactive(self, defaults=None):
"This command will guide you to set up your first stage in "
"[green]dvc.yaml[/green].\n"
)
ui.error_write(message, styled=True)
if show_heading:
ui.error_write(message, styled=True)

for key, prompt in prompts.items():
prompt_cls = RequiredPrompt if key == "cmd" else SkippablePrompt
if key == "cmd":
prompt_cls = RequiredPrompt
else:
prompt_cls = SkippablePrompt
kwargs = {"default": defaults[key]} if key in defaults else {}
value = prompt_cls.ask(prompt, console=ui.error_console, **kwargs)
yield key, value
Expand All @@ -873,6 +877,8 @@ def run(self):
if self.args.template:
raise NotImplementedError("template is not supported yet.")

from dvc.dvcfile import make_dvcfile

global_defaults = {
"code": self.CODE,
"data": self.DATA,
Expand All @@ -882,14 +888,32 @@ def run(self):
"plots": self.PLOTS,
}

dvcfile = make_dvcfile(self.repo, "dvc.yaml")
name = self.args.name or self.DEFAULT_NAME

dvcfile_exists = dvcfile.exists()
if not self.args.force and dvcfile_exists and name in dvcfile.stages:
from dvc.stage.exceptions import DuplicateStageName

hint = "Use '--force' to overwrite."
raise DuplicateStageName(
f"Stage '{name}' already exists in 'dvc.yaml'. {hint}"
)

context = ChainMap()
if not self.args.explicit:
config = {} # TODO
context.maps.extend([config, global_defaults])

if self.args.interactive:
defaults = context.new_child({"live": self.DVCLIVE})
context = self.init_interactive(defaults=defaults)
try:
context = self.init_interactive(
defaults=defaults, show_heading=not dvcfile_exists
)
except (KeyboardInterrupt, EOFError):
ui.error_write()
raise
else:
d = compact(
{
Expand Down Expand Up @@ -922,7 +946,6 @@ def run(self):
_, ext = os.path.splitext(path)
params_kv = [{path: list(LOADERS[ext](path))}]

name = self.args.name or self.DEFAULT_NAME
stage = self.repo.stage.add(
name=name,
cmd=command,
Expand Down