예제 #1
0
def jupyter_edit(conf: AiscalatorConfig, param=None, param_raw=None):
    """
    Starts a Jupyter Lab environment configured to edit the focused step

    Parameters
    ----------
    conf : AiscalatorConfig
        Configuration object for the step
    param : list
        list of tuples of parameters
    param_raw : list
        list of tuples of raw parameters
    Returns
    -------
    string
        Url of the running jupyter lab
    """
    logger = logging.getLogger(__name__)
    conf.validate_config()
    docker_image = build(conf)
    if docker_image:
        # TODO: shutdown other jupyter lab still running
        notebook, _ = notebook_file(conf.step_field('task.code_path'))
        notebook = os.path.basename(notebook)
        if conf.step_extract_parameters():
            jupyter_run(conf,
                        prepare_only=True,
                        param=param,
                        param_raw=param_raw)
        commands = _prepare_docker_env(
            conf, [docker_image, "start.sh", 'jupyter', 'lab'], "edit")
        return wait_for_jupyter_lab(commands, logger, notebook, 10000,
                                    "work/notebook")
    raise Exception("Failed to build docker image")
예제 #2
0
def jupyter_run(conf: AiscalatorConfig,
                prepare_only=False,
                param=None,
                param_raw=None):
    """
    Executes the step in browserless mode using papermill

    Parameters
    ----------
    conf : AiscalatorConfig
        Configuration object for the step
    prepare_only : bool
        Indicates if papermill should replace the parameters of the
        notebook only or it should execute all the cells too

    Returns
    -------
    string
        the path to the output notebook resulting from the execution
        of this step
    """
    logger = logging.getLogger(__name__)
    conf.validate_config()
    docker_image = build(conf)
    if not docker_image:
        raise Exception("Failed to build docker image")
    notebook, _ = notebook_file(conf.step_file_path('task.code_path'))
    notebook = os.path.join("/home/jovyan/work/notebook/",
                            os.path.basename(notebook))
    notebook_output = conf.step_notebook_output_path(notebook)
    commands = _prepare_docker_env(
        conf,
        [
            docker_image, "bash", "start-papermill.sh", "papermill", notebook,
            notebook_output
        ],
        "run_" + conf.step_name() + "_"
        # add timestamp to name to handle multiple concurrent runs
        + datetime.datetime.now().strftime('%Y%m%d_%H%M%S'))
    if prepare_only:
        commands.append("--prepare-only")
    parameters = conf.step_extract_parameters()
    if parameters:
        commands += parameters
    if param:
        for parameter in param:
            commands += ["-p", parameter[0], parameter[1]]
    if param_raw:
        for raw_parameter in param_raw:
            commands += ["-r", raw_parameter[0], raw_parameter[1]]
    log = LogRegexAnalyzer()
    logger.info("Running...: %s", " ".join(commands))
    returncode = subprocess_run(commands, log_function=log.grep_logs)
    if returncode:
        logger.error("Run was not successful, returned status code is: " +
                     str(returncode))
        sys.exit(returncode)
    return os.path.join(conf.step_file_path('task.execution_dir_path'),
                        os.path.basename(notebook_output))