def extract_thumbnail_and_title(nb_path):
    title = "No title"
    image = "images/placeholder.png"
    exporter = markdown.MarkdownExporter()
    output, resources = exporter.from_filename(nb_path)
    for line in output.splitlines():
        line = line.strip()
        if line.startswith("#"):
            title = line.strip("#").strip()
            break

    images = sorted(resources["outputs"])
    if images:
        img_file = images[-1]
        thumb_name = os.path.join(
            IMAGE_DIR,
            os.path.splitext(os.path.basename(nb_path))[0]
            + os.path.splitext(img_file)[-1],
        )

        with open(os.path.join("code_gallery", thumb_name), "wb") as thumb:
            thumb.write(resources["outputs"][img_file])
            print(thumb_name)

        image = os.path.relpath(thumb_name, SITE_DIR)
    return title, image
Example #2
0
def extract_thumbnail_and_title(nb_path):
    title = 'No title'
    image = 'images/placeholder.png'
    exporter = markdown.MarkdownExporter()
    output, resources = exporter.from_filename(nb_path)
    for line in output.splitlines():
        line = line.strip()
        if line.startswith('#'):
            title = line.strip('#').strip()
            break

    images = sorted(resources['outputs'])
    if images:
        img_file = images[-1]
        thumb_name = os.path.join(
            IMAGE_DIR,
            os.path.splitext(os.path.basename(nb_path))[0] +
            os.path.splitext(img_file)[-1])

        with open(os.path.join(thumb_name), 'wb') as thumb:
            thumb.write(resources['outputs'][img_file])
            print(thumb_name)

        image = os.path.relpath(thumb_name, SITE_DIR)
    return title, image