Changes

Jump to: navigation, search

Report-writing tutorial

903 bytes added, 12:08, 12 September 2021
m
Typo in code variable name : "fist" instead of "first"
{{languages|Report-writing tutorial}}
{{stub}}
This tutorial covers the basics of writing a simple report using the Gramps report infrastructure. It covers the process of handling options, building a document and creating the report.
* The most common surname
As of {{man tip|From Gramps version 3.2, there is also a |A [[Simple Access API|simple access]] database [https://www.gramps-project.org/docs/ API] available, with accompanying [[Quick Views]], [[Gramplets]] and [[Addons development|Addons]].}}
==Overview==
Before going into details, it is useful to note that the report should have two basic parts. As This is explained on the [[Addons_Development|Addonsdevelopment]] page, these go that the source code goes into two different files: # the source code Gramps Plugin Registration (''*.gpr.py'') file (e.g.: <code>report.gpr.py) </code># and a Gramps Plugin Registration the main source code file (''*.py'') e.g.: <code>report.py</code> ===report.gpr.py=== ;Registration statement : This initializes the report by a single call to the <tt>[https://gramps-project.org/docs/gen/gen_plug.html#module-gramps.gen.plug._pluginreg register()]</tt> function. It is trivial, but without it your report will not become available to Gramps, even if it is otherwise perfectly written. A report can potentially be generated as a standalone report, as a Gramps Book item, and as a command line report.The registration determines which modes are enabled for a given report. The report class does not have to know anything about the mode. The options class is there to provide options interface for all available modes. * [[Report-writing_tutorial#Registering_the_report]]
===report.py===
;Report class : This is the code that takes data from the Gramps database and organizes it into the document structure. This structure can later be printed, viewed, or written into a file in a variety of formats. This class uses the [https://gramps-project.org/docs/gen/gen_plug.html#module-gen.plug._docgenplugin docgen] interface to abstract away the output format details.
;Options class : This is the code that provides means to obtain options necessary for the report using a variety of available mechanisms.
 
===report.gpr.py===
 
;Registration statement : This initializes the report by a single call to the <tt>register()</tt> function. It is trivial, but without it your report will not become available to Gramps, even if it is otherwise perfectly written.
 
A report can potentially be generated as a standalone report, as a Gramps Book item, and as a command line report. The registration determines which modes are enabled for a given report. The report class does not have to know anything about the mode. The options class is there to provide options interface for all available modes.
==Document interface==
The [[Report Generation]] article provides an overview of the 'docgen' interfaces that are available for outputting documents.
The [[Report API]] article provides more details about the interfaces.
The developer [https://gramps-project.org/docs/gen/gen_plug.html#module-gen.plug._docgenplugin docgen] api documentation provides a detailed specification of the interfaces.
Gramps attempts to abstract the output document format away from the report. By coding to the [https://gramps-project.org/docs/gen/gen_plug.html#module-gen.plug._docgenplugin docgen] class, the report can generate its output in the format desired by the end user. The document passed to the report (<tt>self.doc</tt>) could represent an HTML, OpenDocument, PDF or any of the other formats supported by the user. The report does not have to concern itself with the output format details, since all details are handled by the document object.
A document is composed of paragraphs, tables, and graphics objects. Tables and graphics objects will not be covered in this tutorial.
The report defines a set of paragraph and font styles, along with their default implementation. The user can override the definition of each style, allowing the user to [[Gramps_4.2_Wiki_Manual_Gramps_{{man version}}_Wiki_Manual_-_Settings#Customize_report_output_formats|customize the report]]. Each paragraph style must be named uniquely, to prevent collisions when printed in a book format. It is recommended to prefix each paragraph style with a three letter code unique to the report.
Paragraph and font styles are defined in the <tt>make_default_style()</tt> function of the options class. The paragraphs are grouped into a <tt>StyleSheet</tt>, which the <tt>make_default_style()</tt> function defines. For the example report (<tt>DbSummary</tt>), the paragraph styles are defined as below:
{{stub}}<!-- need to check code is correct for Gramps 5.1.x -->
compare to <code>gramps\plugins\textreport\summary.py</code> starting at [https://github.com/gramps-project/gramps/blob/master/gramps/plugins/textreport/summary.py#L297 line 297]
<pre>
def make_default_style(self, default_style):
# Options specific for this report
self.options_dict = {
'my_fist_optionmy_first_option' : 0,
'my_second_option' : '',
}
self.options_help = {
'my_fist_optionmy_first_option' : ("=num","Number of something",
[ "First value", "Second value" ],
True),
==Implementation==
===Defining the Report Options class===
 
In this example, no special options are required. This makes the options class very simple. All that is necessary is to define the default styles.
<pre>
** translated name (the one to display in menus)
** modes that should be enabled for the report (standalone, book item, command line)
* If the report requires an active person to run, then <code>require_active </code> should be set to <code>True</code>.
* Finally, both report class and options class should be passed to registration.
description = _("Produces a ..."),
version = '1.0',
gramps_target_version = '35.21',
status = STABLE,
fname = 'filename.py',
See an existing book report for more details.
 
== Manually installing your report ==
Install in the plugins directory of your Gramps user directory.
 
== Example output ==
Start Gramps and your tutorial report will be available from the reports menu as ....
 
Run the report and this is what the output looks like ....
==See also==
* [[Report_API|Report API]]
* [[Report_Generation|Report Generation]] (describing output format options)* [[Gramps_Data_Model|Gramps Data Model]]
[[Category:Addons]]
[[Category:Developers/General]]
[[Category:Developers/Tutorials]]
[[Category: Plugins]]
[[Category:Reports]]
3,060
edits

Navigation menu