Skip to content
lucasdavila edited this page Mar 25, 2013 · 21 revisions

Docs

web2py-appreport provides the following helpers (functions which can be used in a controller):

REPORTFORM

This function will return a html form, the inputs of the form are automaticaly generated based on table fields,

usage:

#app_root/controllers/your_controller.py
def report_form():
    form = plugin_appreport.REPORTFORM(table=db.person)
    return dict(form = form)

REPORTPISA

This function will return a PDF document rendered by pisa engine, the document is generated based on html code and can by styled using css,

Expected arguments:

  • filter (or args): dict(form.vars) or {'table_name.field_name':'value', 'table_name.field2_name':'value2'}, default value is {} (empty dict)
  • table: db.your_table, default value is None
  • html: string containing html and css, default value is '' (empty string).

You can pass the html manually or a table and a filter to automatically generate the html (based on table field and filtered using the filter :P ) Note: if you pass the html (aka != '') the report will be generated manually using html ignoring the table and filter

Usage:

#app_root/controllers/your_controller.py
def static_report_pisa():
    static_html = '<html><body><h1>My first report</h1><table><tr><td>Hello world :P </td></tr><tr><td>using <strong>web2py-appreport</strong> with pisa engine</td></tr><table></body></html>'
    return plugin_appreport.REPORTPISA(html=static_html)

or

#app_root/controllers/your_controller.py
def dynamic_report_pisa():
    return plugin_appreport.REPORTPISA(table=db.person, filter = {'person_id' : 1})

or, combining with REPORTFORM:

#app_root/controllers/your_controller.py
def dynamic_report_pisa():
    form = plugin_appreport.REPORTFORM(table = db.person)

    if form.accepts(request.vars, session):
        return plugin_appreport.REPORTPISA(table = person, filter = dict(form.vars))

    return dict(form = form)

or, rendering a web2py view (like mvc):

#app_root/views/person/report_persons.html
<html>
    <head>
        <meta charset="utf-8" />	 
    </head>
    <body>
        <h2>Report of persons</h2>
        <table border="0">
            <tr>
                <th><strong>Name</strong></th>
                <th><strong>Phone</strong></th>
                <th><strong>e-mail</strong></th>
            </tr>                
            {{for p in persons:}}
                <tr>
                    <td>{{=p.name}}</td>
                    <td>{{=p.phone}}</td>
                    <td>{{=p.email}}</td>
                </tr>  
            {{pass}}
        </table>
    </body>
</html>


#app_root/controllers/your_controller.py
def dynamic_report_pisa():
    form = plugin_appreport.REPORTFORM(table = db.person)

    if form.accepts(request.vars, session):
        persons = db(form.prep_filter(filter = dict(form.vars))).select()
        html = response.render('person/report_persons.html', dict(persons = persons))
        return plugin_appreport.REPORTPISA(html = html)

    return dict(form = form)

REPORTPYFPDF

This function will return a PDF document rendered by pyfpdf engine, the document is generated based on html code,

Expected arguments:

  • filter (or args): dict(form.vars) or {'table_name.field_name':'value', 'table_name.field2_name':'value2'}, default value is {} (empty dict)
  • table: db.your_table, default value is None
  • html: string containing html and css, default value is '' (empty string).

You can pass the html manually or a table and a filter to automatically generate the html (based on table field and filtered using the filter :P ) Notes:

  • if you pass the html (aka != '') the report will be generated manually using html ignoring the table and filters
  • css are not supported in pyfpdf, to customize the report see the arguments explanation below:

Other arguments supported by pyfpdf:

  • charset = 'utf-8'
  • title = 'title of report'
  • orientation = 'P' (for portrait (retrato)) or ('L' for landscape (paisagem))
  • unit = 'mm'
  • format = 'A4'

usage:

#app_root/controllers/your_controller.py
def static_report_pyfpdf():
    static_html = '<html><body><h1>My first report</h1><table><tr><td>Hello world :P </td></tr><tr><td>using <strong>web2py-appreport</strong> with pyfpdf engine</td></tr><table></body></html>'
    return plugin_appreport.REPORTPYFPDF(html=static_html, title = 'My report title')

or

#app_root/controllers/your_controller.py
def dynamic_report_pyfpdf():
    return plugin_appreport.REPORTPYFPDF(table=db.person, filter = {'person_id' : 1})

or, combining with REPORTFORM:

#app_root/controllers/your_controller.py
def dynamic_report_pyfpdf():
    form = plugin_appreport.REPORTFORM(table = db.person)

    if form.accepts(request.vars, session):
        return plugin_appreport.REPORTPYFPDF(table = person, filter = dict(form.vars))

    return dict(form = form)

REPORT

This helper are deprecated (maintained for backward compatibility), use REPORTPISA or REPORTPYFPDF to build your reports.

examples

Remote reports

In a controler ex: app_root/controllers/default.py define the following actions:

def remote_report():
    session.forget()
    return service()

import xmlrpclib

@service.xmlrpc
def pdf(html, **kargs):
    if not isinstance(html, str):
        raise Exception('html arg must be string')
    elif html.strip() == '':
        raise Exception('html arg can not be empty') 
    report = xmlrpclib.Binary(plugin_appreport.REPORTPISA(html = html))
    return dict(report = report)

Now connect a xml-rpc client, to build remote reports, ex (python client):

import xmlrpclib
server = xmlrpclib.ServerProxy('http://localhost:8000/plugin-appreport/person/remote_report/xmlrpc')
report = server.pdf(html = 'your html to build the report...')['report']

report_file = open('/home/your_user_name/remote_report.pdf', 'w')
report_file.write(report.data)
report_file.close()

#now check your report on '/home/your_user_name/remote_report.pdf

Helpers REPORTFORM and REPORTPISA (or REPORTPYFPDF) for simple reports

  1. Download the plugin appreport

  2. Create a new app called reports

  3. Import the plugin in your app reports

  4. Set the following table in models/db.py:

     person = db.define_table('person',
         Field('name'),
         Field('phone'),
         Field('email'))
    
     person.name.requires = IS_NOT_EMPTY()
     person.email.requires = IS_NULL_OR(IS_EMAIL())
    
    
     favorite_music = db.define_table('favorite_music',
         Field('person', person),
         Field('title'),
         Field('artist'))
    
     favorite_music.person.requires = IS_IN_DB(db, person.id, '%(name)s')
     favorite_music.title.requires = IS_NOT_EMPTY()
    
  5. Define the following action in controllers / default.py:

     def report():
         form = plugin_appreport.REPORTFORM(person)
    
         if form.accepts(request.vars, session):
             return plugin_appreport.REPORTPISA(table = person, title = 'List of persons', filter = dict(form.vars))
    
         return dict(form = form)
    
  6. Visit the link http://127.0.0.1:8000/reports/default/report

Helper REPORTPISA (or REPORTPYFPDF) for complex report, using simple html code

  1. Repeat steps 1, 2, 3 and 4 of the above example.

  2. Define the following action in controllers / default.py:

     def custom_report():
         html = """
             <html>
                 <body>
                     <table>
                         <thead>
                             <tr>
                                 <th>Author</th>
                                 <th>Email</th>
                                 <th>Twitter</th>
                             </tr>
                         </thead>
                         <tbody>
                             <tr>
                                 <td>Lucas Davila</td>
                                 <td>lucassdvl@gmail.com</td>
                                 <td>@lucadavila</td>                                
                             </tr>
                         </tbody>
                     </table>
                 </body>
             </html>"""
    
         return plugin_appreport.REPORTPYFPDF(html = html, title = 'my custom report using the plugin appreport')
    
  3. Visit the link http://127.0.0.1:8000/reports/default/custom_report

Helper REPORTPISA (or REPORTPYFPDF) for complex report, using web2py views

  1. Repeat steps 1, 2, 3 and 4 of the first example.

  2. Define the following view in views / default / report_persons.html:

     <html>
     <head>
         <meta charset="utf-8" />	 
     </head>
     <body>
         <h2>Report of persons</h2>
    
         <table border="0">
             <thead>
                 <tr>
                     <td><strong>Name</strong></td>
                     <td><strong>Phone</strong></td>
                     <td><strong>e-mail</strong></td>
                 </tr>
                 <tr>
                     <td></td>
                     <td><strong>Favorite music</strong></td>
                     <td><strong>Artist</strong></td>
                 </tr>                
     
             </thead>
             <tbody>
                 {{for p in persons:}} <!-- using python -->
                     <tr>
                         <td>{{=p.name}}</td>
                         <td>{{=p.phone}}</td>
                         <td>{{=p.email}}</td>
                     </tr>  
                         {{for m in p.favorite_music.select():}} <!-- using python -->
                             <tr>
    
                                 <td></td>
                                 <td>{{=m.title}}</td>
                                 <td>{{=m.artist}}</td>
    
                             </tr>                      
                     {{pass}}
                 {{pass}}
             </tbody>
         </table>
     </body>
    </html>
    
  3. Define the following action in controllers / default.py:

    def complex_report():

     form = plugin_appreport.REPORTFORM(table=person)
    
     if form.accepts(request.vars, session):
         persons = db(form.prep_filter(filter = dict(form.vars))).select()
         html = response.render('default/report_persons.html', dict(persons = persons))
         return plugin_appreport.REPORTPISA(html = html)
    
     return dict(form = form)
    
  4. Visit the link http://127.0.0.1:8000/reports/default/complex_report