View Single Post
Old 08-19-2024, 07:02 AM   #17
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,489
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by foosion View Post
Is there documentation which shows these functions (these two and the ones in the function you posted)? Or do I need to browse the source code?

It would be good to learn how to write things myself.
Some of the API is documented in https://manual.calibre-ebook.com/dev...rts-of-calibre.

However, for the kinds of things you are trying to do it is usually best to look at existing code that does something similar.

Quote:
In the meanwhile, whichever is easier.
Here is code to do both. The bit you want replaces the two "view_action" lines.
Code:
		# select the random row in the booklist
		view = gui.current_view()
		view.select_rows(set((random_id,)))

		# display a dialog
		mi = db.new_api.get_proxy_metadata(random_id)
		from calibre.gui2 import info_dialog
		data_to_show = (
			f"Series: {mi.format_field('series')[1]}",
			f"Tags: {mi.format_field('tags')[1]}",
			f"My Bool: {mi.format_field('#mybool')[1]}"
		)
		msg = '<br>'.join(data_to_show)
		info_dialog(gui, f"Random book: {mi.get('title')}", msg, show=True)
You can use the template tester in python mode to play, which is easier and faster than creating template actions or altering calibre source. For example, here is the code from above usable as a python template. It is only the rist few lines that are different.

Code:
python:
def evaluate(book, context):
	db = context.db
	from calibre.gui2.ui import get_gui
	gui = get_gui()
	db = gui.current_db
	ids = list(db.new_api.search('#mybool:true and formats:epub'))
	if ids:
		import random
		random_id = random.choice(ids)
#		view_action = gui.iactions['View']
#		view_action.view_format_by_id(random_id, 'EPUB', open_at=None)

		# select the random row.
		view = gui.current_view()
		view.select_rows(set((random_id,)))

		# display a dialog
		mi = db.new_api.get_proxy_metadata(random_id)
		from calibre.gui2 import info_dialog
		data_to_show = (
			f"Series: {mi.format_field('series')[1]}",
			f"Tags: {mi.format_field('tags')[1]}",
			f"My Book: {mi.format_field('#mybool')[1]}"
		)
		msg = '<br>'.join(data_to_show)
		info_dialog(gui, f"Random book: {mi.get('title')} ({str(random_id)})", msg, show=True)		
	return 'a string'

Last edited by chaley; 08-19-2024 at 07:13 AM. Reason: Typo
chaley is offline   Reply With Quote