Python client for the National Gallery (London) Elasticsearch API
Project description
National Gallery API Wrapper
A small Python wrapper around the National Gallery (London) Elasticsearch search endpoint (https://data.ng.ac.uk/es/public/_search). The library aims to provide:
- A more pythonic interface with the National Gallery API
- Plain-text rendering of records e.g. for use in LLM prompts (entity disambiguation, authority linking, etc.)
National Gallery data is offered for reuse under specific licences.
Setup
# sync only
pip install national-gallery-api
# sync and async
pip install "national-gallery-api[async]"
Quick start
The following entities are available: people, organisations, works, events, exhibitions, places, locations, concepts, publications, archives, media, and packages.
Search records
from national_gallery_api import NationalGallery
with NationalGallery() as ng:
results = ng.people.search("rembrandt", actual="Individual", size=5)
for person in results:
print(person.title, person.pid, person.dates)
Look up a single record by PID
with NationalGallery() as ng:
vincent = ng.people.get("0QCE-0001-0000-0000")
print(vincent.title) # Vincent van Gogh
print(vincent.external_ids) # ULAN, Wikidata, RKD, VIAF, ...
Iterate over all results
iter_all lazily walks an entire result set, handling paging internally:
with NationalGallery() as ng:
for person in ng.people.iter_all(actual="Individual", page_size=100):
...
Caching
Caching is disabled by default. To minimise server load when making frequent repeat requests (e.g. during batch jobs), cache should be enabled:
with NationalGallery(cache=True, ttl=3600, database_path="hishel_cache.db") as ng:
...
Async
AsyncNationalGallery mirrors the sync client.
import asyncio
from national_gallery_api import AsyncNationalGallery
async def main():
async with AsyncNationalGallery() as ng:
results = await ng.works.search("portrait", size=5)
for work in results:
...
async for work in ng.works.iter_all("portrait", page_size=50):
...
asyncio.run(main())
Rendering for LLM context
from national_gallery_api import NationalGallery, to_context, render_candidates
with NationalGallery() as ng:
vincent = ng.people.get("0QCE-0001-0000-0000")
print(to_context(vincent)) # for single entities
candidates = ng.people.search("rembrandt", actual="Individual", size=5)
print(render_candidates(candidates)) # for record sets
Example to_context output:
Person: Vincent van Gogh
PID: 0QCE-0001-0000-0000
Subtype: Individual
Dates: 1853 - 1890
Names: Vincent van Gogh; Gogh, Vincent van
External IDs: http://viaf.org/viaf/9854560; http://vocab.getty.edu/ulan/500115588; https://rkd.nl/artists/32439; https://www.wikidata.org/entity/Q5582
Raw Elasticsearch queries
If a query or field is not covered by the typed API, raw Elasticsearch queries can be made, returning an unparsed response dict:
with NationalGallery() as ng:
payload = ng.search({"query": {"match_all": {}}, "size": 0})
print(payload["hits"]["total"])
Query bodies can also be built with the build_search helper:
from national_gallery_api import build_search, EntityType
body = build_search("van gogh", base=EntityType.AGENT, actual="Individual", size=10)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file national_gallery_api-0.1.1.tar.gz.
File metadata
- Download URL: national_gallery_api-0.1.1.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f67efcdafb63e0f2236e0d378e558c13dba124c8d0a8c38979be3dae069416a1
|
|
| MD5 |
e0c5cdfa6e32ae2caff474b268e3c7b0
|
|
| BLAKE2b-256 |
0e944b25e80c132a13fc99ed4a39e6185d5c9f1e5b3f66e9b2074baf2fd526a7
|
File details
Details for the file national_gallery_api-0.1.1-py3-none-any.whl.
File metadata
- Download URL: national_gallery_api-0.1.1-py3-none-any.whl
- Upload date:
- Size: 20.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3daea66605bb89f3bcaa89600112a3c518a706b7234c591d497f54b9f023b34
|
|
| MD5 |
43d77a57f538dfa978a7a63c3522f061
|
|
| BLAKE2b-256 |
b1bbf0e5de0297ccba9aa6545e1ec495af258082739dbd5721df5a19773073a4
|