Skip to content

Getting started: using iiifprezi3 without helper methods

Note

iiif-prezi3 offers useful helper methods for adding IIIF resources, it is preferable to use them when possible.

For special needs, not addressed by the helper methods, users may take advantage directly of the Python classes generated by datamodel-code-generator. These classes can be used for generating instances of IIIF objects described in the presentation API v.3.

In the following lines of code, we will create a manifest and two canvases using directly the pydantic classes:

import iiif_prezi3
amanifest = iiif_prezi3.Manifest(id='http://iiif.example.org/prezi/Manifest/0', label={'en': ['default label']})
acanvas = iiif_prezi3.Canvas(id='http://iiif.example.org/prezi/Manifest/0/canvas/01', label={'en': ['default label']})
asecondcanvas = iiif_prezi3.Canvas(id='http://iiif.example.org/prezi/Manifest/0/canvas/02', label={'en': ['second label']})

For constructing nested objects we have to assign to the items property a list containing the valid IIIF objects accepted by the class:

amanifest.items = [acanvas,asecondcanvas]

If we try to set the items property of the manifest items to a string pydantic will rise a ValidationError:

amanifest.items = ["this shouldn't be here"]

Warning

Be aware that appending to the items list after that the items property has been assigned :

amanifest.items.append("this shouldn't be here")

will not raise any error regarding the input.

print(amanifest.json(exclude_unset=True,indent=2))
{
  "id": "http://iiif.example.org/prezi/Manifest/0",
  "type": "Manifest",
  "label": {},
  "items": [
    {
      "id": "http://iiif.example.org/prezi/Manifest/0/canvas/01",
      "type": "Canvas",
      "label": {}
    },
    {
      "id": "http://iiif.example.org/prezi/Manifest/0/canvas/02",
      "type": "Canvas",
      "label": {}
    },
    "this shouldn't be here"
  ]
}

Of course, in most of the use cases, canvases will be created dynamically so a possible approach for constructing the manifest can be the following:

canvases = []
for i in range(3):
    canvasid = f'http://iiif.example.org/prezi/CanvasID/{i}'
    aservice = {
                "id": "https://example.org/iiif/book1/page2",
                "type": "ImageService3",
                "profile": "level2"
                      }
    anannotation = iiif_prezi3.Annotation(type='Annotation',id=canvasid+'/annotation/0',target=canvasid)
    anannotation.motivation = 'painting'
    # aresource = Resource(service=[aservice],type='Resource')
    anannotation.body = iiif_prezi3.ResourceItem(service=[aservice],id=canvasid+'/annotation/resource',type='Image')
    anannotationpage = iiif_prezi3.AnnotationPage(type='AnnotationPage')
    anannotationpage.items = [anannotation]
    acanvas.items = [anannotationpage]
    canvases.append(acanvas)

amanifest.items = canvases

Notice that the canvases list is created at the beginning but is assigned to the items property of the manifest only once at the end, in this way pydantic will validate the list.

Warning

Keep in mind that objects contained in the canvases list are references in the manifest items, hence any modification on the original object will affect the object inside the manifest. This holds true for any object added to the model.