Open main menu

Gramps β

Changes

Report-writing tutorial

1,717 bytes removed, 10:38, 2 December 2025
Defining the Report class: A code description is aligned with the new code
===Defining the ReportOptions class===
In this exampletutorial, no special the report has two options . The first, called <code>what_types</code>, allows a user to choose whether the counts of persons whose gender is Male, Female, Unknown or Other are requiredshown in the report. This makes The second is the options class very simple. All standard Gramps option that is necessary is allows the user to define choose the default styleslocale for the report different from UI locale. It is added by calling <precode>from grampsstdoptions.genadd_localization_option()</code> method.plug.report import Reportfrom gramps.gen.plug.report import ReportOptionsfrom gramps.gen.lib import Personfrom gramps.gen.plug import docgenfrom gramps.gen.const import GRAMPS_LOCALE as glocale_ = glocale.translationThese two options are added in the overridden <code>add_menu_options()</code> method.gettext
To define two unique styles for the report: DBS-Title and DBS-Normal the method <code>make_default_style()</code> is overridden too. Both styles are added by calling <code>default_style.add_paragraph_style()</code> method.
class DbSummaryOptions(ReportOptions):  def __init__(self, name, database):  ReportOptions{{RWT_ReportOptions.__init__(self, name, database)  def make_default_style(self, default_style):  # Define the title paragraph, named 'DBS-Title', which uses a # 18 point, bold Sans Serif font with a paragraph that is centered  font = docgen.FontStyle() font.set_size(18) font.set_type_face(docgen.FONT_SANS_SERIF) font.set_bold(True)  para = docgen.ParagraphStyle() para.set_header_level(1) para.set_alignment(docgen.PARA_ALIGN_CENTER) para.set_font(font) para.set_description(_('The style used for the title of the page.'))  default_style.add_paragraph_style('DBS-Title', para)  # Define the normal paragraph, named 'DBS-Normal', which uses a # 12 point, Serif font.  font = docgen.FontStyle() font.set_size(12) font.set_type_face(docgen.FONT_SERIF)  para = docgen.ParagraphStyle() para.set_font(font) para.set_description(_('The style used for normal text'))  default_style.add_paragraph_style('DBS-Normal', para) </pre>py}}
So create now file '''report.py''' and copy there the above code.
===Defining the Report class===
The actual implementation of the <code>DbSummaryReport</code> is rather simple. No additional work needs to be done to To initialize the class, so the parent <code>Report.__init__()</code> routine is called. All and then values of the work is done in options entered by the user are extracted from <code>write_report()options_class</code> method. It uses a [https://www.gramps-project.org/docs/gen/gen_db.html#gramps.gen.db.generic.DbGeneric.iter_people parameter which is of class <code>self.database.iter_people()DbSummaryOptions</code>] to iterate through [https://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.person An instance variable <code>Personself.what_types</code>] objects and gathers some simple statistics. The only thing of any complication is the determination assigned a list of the most common surname. A python dictionary is used to store the number names of times each surname is used. Each time a surname is encountered, the value in the dictionary is incremented. The results are then loaded into a list and sorted, allowing us to find the most common name check boxes selected by looking at the last entry in the listuser.Then <precode>class DbSummaryReport(Report):  def __init__(self, database, options_class, user):  Report.__init__set_locale(self, database, options_class, user def write_report(self):  males = 0 females = 0 total = 0 surname_map = {}  for person in self.database.iter_people(): if person.get_gender() == Person.MALE: males += 1 if person.get_gender() == Person.FEMALE: females += 1 total += 1  surname = person.get_primary_name().get_surname()  if surname in surname_map: surname_map[surname] += 1 else: surname_map[surname] = 1  slist = [] for key in surname_map.keys(): slist.append((surname_map[key], key)) slist.sort()  self.doc.start_paragraph("DBS-Title") self.doc.write_text(_("Database Summary")) self.doc.end_paragraph()  self.doc.start_paragraph('DBS-Normal') self.doc.write_text(_('Number </code> method is called with the value of males : %d') % males) selfthe locale to be used to run the report.doc.end_paragraph()  self.doc.start_paragraph('DBS-Normal') self.doc.write_text(_('Number of females : %d') % females) self.doc.end_paragraph()
selfAll the work is done in the <code>_count()</code> method.docIt uses a [https://www.start_paragraph('DBSgramps-Normal') project.org/docs/gen/gen_db.html#gramps.gen.db.generic.DbGeneric.iter_people <code>self.docdatabase.write_textiter_people(_('Total people )</code>] to iterate through [https: %d') % total) self//www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.person <code>Person</code>] objects and gathers some simple statistics. The only thing of any complication is the determination of the most common surnames. A python dictionary is used to store the number of times each surname is used. Each time a surname is encountered, the value in the dictionary is incremented.docThe results are then loaded into a list and sorted in the reverse order <code>slist.end_paragraphsort(reverse=True)</code>, allowing us to find the most common names by looking at the first entries in the list.
self.doc.start_paragraphFinally <code>write_report('DBS-Normal') self.doc</code> method outputs the data collected in accordance with the options selected by the report user.write_text(_('Number of unique surnames : %d') % lenNote that the strings being used in the report (slistsuch as "Report Options", "Males" etc)) selfhave already been used somewhere else in Gramps and have already been translated into various languages.doc.end_paragraph()Thus the content of our report as well as its parameters are already translated!
self.doc.start_paragraph('DBS-Normal')
self.doc.write_text(_('Most common surname : %s') % (slist[-1][1]))
self.doc.end_paragraph()
</pre>{{RWT_Report.py}}
Append the above code to the '''report.py''' file
64
edits