예제 #1
0
def run_report_http(report_name):
    """
    The "Run Report" interface is generated by this method.

    :param report_name: The parameter here should be a "/"-delimited string which mirrors the directory structure of \
        the notebook templates.

    :returns: An HTML template which is the Run Report interface.
    """
    report_name = convert_report_name_url_to_path(report_name)
    json_params = request.args.get("json_params")
    initial_python_parameters = json_to_python(json_params) or ""
    try:
        path = generate_ipynb_from_py(get_template_dir(), report_name)
    except FileNotFoundError as e:
        logger.exception(e)
        return "", 404
    nb = nbformat.read(path, as_version=nbformat.v4.nbformat)
    metadata_idx = _get_parameters_cell_idx(nb)
    parameters_as_html = ""
    has_prefix = has_suffix = False
    if metadata_idx is not None:
        metadata = nb["cells"][metadata_idx]
        parameters_as_html = metadata["source"].strip()
        has_prefix, has_suffix = (bool(nb["cells"][:metadata_idx]), bool(nb["cells"][metadata_idx + 1 :]))
    logger.info("initial_python_parameters = {}".format(initial_python_parameters))
    return render_template(
        "run_report.html",
        parameters_as_html=parameters_as_html,
        has_prefix=has_prefix,
        has_suffix=has_suffix,
        report_name=report_name,
        all_reports=get_all_possible_templates(),
        initialPythonParameters=initial_python_parameters,
    )
예제 #2
0
def sanity_check(template_dir):
    logger.info("Starting sanity check")
    os.environ["PY_TEMPLATE_DIR"] = template_dir
    try:
        for template_name in templates._all_templates():
            logger.info(
                "========================[ Sanity checking {} ]========================"
                .format(template_name))
            # Test conversion to ipynb - this will throw if stuff goes wrong
            generate_ipynb_from_py(filesystem.get_template_dir(),
                                   template_name,
                                   warn_on_local=False)

            # Test that each template has parameters as expected
            nb = templates.template_name_to_notebook_node(template_name,
                                                          warn_on_local=False)
            param_idx = templates._get_parameters_cell_idx(nb)
            if param_idx is None:
                logger.warning(
                    'Template {} does not have a "parameters"-tagged cell.'.
                    format(template_name))

            # Test that we can generate a preview from the template
            preview = templates._get_preview(template_name,
                                             warn_on_local=False)
            # Previews in HTML are gigantic since they include all jupyter css and js.
            assert len(
                preview
            ) > 1000, "Preview was not properly generated for {}".format(
                template_name)
            logger.info(
                "========================[ {} PASSED ]========================"
                .format(template_name))
    finally:
        filesystem._cleanup_dirs()
예제 #3
0
def test_template_has_parameters(template_name):
    generate_ipynb_from_py(get_template_dir(),
                           template_name,
                           warn_on_local=False)
    nb = template_name_to_notebook_node(template_name, warn_on_local=False)
    metadata_idx = _get_parameters_cell_idx(nb)
    assert metadata_idx is not None, 'Template {} does not have a "parameters"-tagged cell.'.format(
        template_name)
예제 #4
0
def test_template_has_parameters(template_name, template_dir, flask_app):
    flask_app.config["PY_TEMPLATE_DIR"] = ""
    with flask_app.app_context():
        generate_ipynb_from_py(template_dir,
                               template_name,
                               notebooker_disable_git=True,
                               py_template_dir="",
                               warn_on_local=False)
        nb = template_name_to_notebook_node(template_name,
                                            notebooker_disable_git=True,
                                            py_template_dir="",
                                            warn_on_local=False)
        metadata_idx = _get_parameters_cell_idx(nb)
        assert metadata_idx is not None, 'Template {} does not have a "parameters"-tagged cell.'.format(
            template_name)