Beispiel #1
0
def run_notebook(filename):
    c = Config()
    c.TagRemovePreprocessor.remove_cell_tags = ("remove_cell", "uses-hardware")
    c.TagRemovePreprocessor.enabled = True
    c.preprocessors = ['TagRemovePreprocessor']
    execution_failed = False

    with open(filename) as f:
        nb = nbformat.read(f, as_version=4)

    for cell in nb.cells.copy():
        if "uses-hardware" in cell.metadata.get("tags", []):
            nb.cells.remove(cell)
    try:
        ep = ExecutePreprocessor(timeout=None, kernel_name='python3')
        ep.preprocess(nb, {'metadata': {'path': './'}})
    except Exception as e:
        print(
            "[" + datetime.now().time().strftime('%H:%M') + "] " +
            "Error in file '", filename, "': ",
            str(e).split('\n')[-2])
        execution_failed = True

    if not execution_failed:
        return 1
    return 0
async def generate_report(athlete, activity_id):

    input_path = Path(NOTEBOOK_TEMPLATES_PATH,
                      f'{NOTEBOOK_TEMPLATE_NAME}.ipynb')
    output_dir = Path(REPORT_OUTPUT_DIR, str(athlete.id))
    output_dir.mkdir(parents=True, exist_ok=True)
    notebook_filename = f'{activity_id}.ipynb'
    html_filename = f'{activity_id}.html'
    notebook_path = Path(output_dir, notebook_filename)
    html_path = Path(output_dir, html_filename)

    try:
        papermill.execute_notebook(input_path=input_path.as_posix(),
                                   output_path=notebook_path.as_posix(),
                                   parameters=dict(
                                       access_token=athlete.access_token,
                                       activity_id=activity_id))
    except papermill.exceptions.PapermillExecutionError:
        pass

    nb = sb.read_notebook(notebook_path.as_posix())
    start_date_local = nb.scraps['activity_detail'].data['start_date_local']
    dt = datetime.fromisoformat(start_date_local)
    title = nb.scraps['activity_detail'].data['name']

    await Report.objects.create(activity_id=activity_id,
                                strava_athlete=athlete,
                                title=title,
                                datetime=dt,
                                notebook_filename=notebook_filename,
                                html_filename=html_filename)

    with notebook_path.open('r') as f:
        notebook = nbformat.reads(f.read(), as_version=4)

    c = Config()
    c.TagRemovePreprocessor.enabled = True
    c.TagRemovePreprocessor.remove_cell_tags = ("remove_cell", "parameters",
                                                "injected-parameters")
    c.TagRemovePreprocessor.remove_all_outputs_tags = ('remove_output', )
    c.TagRemovePreprocessor.remove_input_tags = ('remove_input', )
    c.preprocessors = ["TagRemovePreprocessor"]

    html_exporter = HTMLExporter(config=c)
    html_exporter.template_file = 'full'
    body, _ = html_exporter.from_notebook_node(notebook)

    with html_path.open('w') as f:
        f.write(body)
Beispiel #3
0
    out = ep.preprocess(snb, {'metadata': {'path': './'}})
except CellExecutionError:
    out = None
    msg = 'Error executing the notebook "%s".\n\n' % template_path
    msg += 'See notebook "%s" for the traceback.' % template_path
    print(msg)
    raise
finally:
    # Save notebook
    with open(template_path, mode='w', encoding='utf-8') as f:
        nbformat.write(snb, f)

# Export as HTML (PDF is too much hassle)
print("All good. Building report.")
c = Config()
c.TagRemovePreprocessor.enabled = True
c.TagRemovePreprocessor.remove_input_tags = set(["hide_input"])
c.preprocessors = ["TagRemovePreprocessor"]

html_exporter = HTMLExporter(config=c)
html_data, resources = html_exporter.from_notebook_node(snb)
html_data = html_data.replace(
    '</head>',
    '<style>pre{font-family: "Times New Roman", Times, serif;}</style></head>')

with open(submission_path, "wb") as f:
    f.write(html_data.encode('utf8'))
    f.close()

print("Done in {} seconds".format(time.time() - start))