NFDI4Culture / CPS-Demo

Computational Publishing Services Demo - 2024
https://nfdi4culture.github.io/CPS-Demo/
MIT License
0 stars 5 forks source link

images displayed at the end of file #20

Open calnfynn opened 4 months ago

calnfynn commented 4 months ago

possibly caused by PIL. markdown syntax might be better

calnfynn commented 4 months ago

The PIL library the code uses works for displaying images in Jupyter Notebook, but I think Quarto doesn't understand that it is dealing with images if they're in that format. Without that information it can't convert them like it should and puts them at the very end of the PDF without doing anything to them.

The simplest solution is to put the images into Markdown format instead so that Quarto recognises them correctly:


def get_img(partOfItem_id):

    q = ""
    if partOfItem_id:
        q = query_img.replace("<placeholder>", "?partOfStatement cpsps:P6 cps:"+partOfItem_id+".")
    else:
        q = query_img.replace("<placeholder>","")

    results_img = run_query(endpoint_url, q)

    for item in results_img["results"]["bindings"]:    

        title = item['itemLabel']['value']
        description = html.unescape(item['itemDescr']['value'])

        print('\nWikibase link: ' + '[' + item['imgItem']['value'] + ']' + '(' + item['imgItem']['value'] + ')' + '\n')
        print('Title: ' + title + '\n')
        print('Year: ' + item['publishDate']['value'] + '\n')
        print('Description: ' + description + '\n')

        # get image from image URL
        image_url=item['imgUrl']['value']

        # display image with title + alt text (in markdown)
        print('!['+ title +']('+image_url+'){fig-alt="'+description+'"}\n\n')

The above snippet replaces the get_img part in section.yml . This changes the image format to Markdown and also allows the Title from Wikibase to be used as the caption in the PDF, and the Description as the alt text.

Since this doesn't use the PIL library anymore, this:

from PIL import Image
from datetime import datetime
import time

and the fetch_image_by_url & get_delay functions can be deleted from the file.

mrchristian commented 4 months ago

FYI - checking on local rendering and PDF image rendering at end of doc - I now find an inconsistency with images now working in PDF. We will still carry out our fix as we want to be sure things work. The repo is https://github.com/TIBHannover/section-demo - This is just to log the observation - https://github.com/NFDI4Culture/CPS-Demo/issues/23