View Single Post
Old 10-09-2022, 12:09 PM   #29
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,476
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Another example python template, this time a bit more complicated and more useful. It produces a list of all the authors for a series. The list is stored in a composite column (#comp2) that shows in the book details "on separate lines", which requires the list be a comma-separated. To make that work the template converts commas in author names to semicolons then builds the comma-separated list. The authors are then sorted, which is why the template uses author_sort.

The template:
Code:
python:
def evaluate(book, db, globals, arguments, **kwargs):
	if book.series is None:
		return ''
	ans = set()
	for id_ in db.search_getting_ids(f'series:"={book.series}"', ''):
		ans.update([v.strip() for v in db.new_api.field_for('author_sort', id_).split('&')])
	return ', '.join(v.replace(',', ';') for v in sorted(ans))
The result in book details showing all authors for the series of the current book:
Attached Thumbnails
Click image for larger version

Name:	Clipboard04.jpg
Views:	913
Size:	29.4 KB
ID:	197070  

Last edited by chaley; 10-09-2022 at 12:14 PM. Reason: Corrected the template
chaley is offline   Reply With Quote