Skip to content

Reporting with SQLAlchemy

Werner Bruhin edited this page Jun 5, 2013 · 1 revision

Dabo's ReportDesigner and Writer used with SQLAlchemy

Not using the Dabo Framework but still wanting to use the reporting tools and feeding data to it from SQLAlchemy.

Code sample for the ReportWriter

result = session.query(db.Cellarbook.currentvalue,
                   db.Drinkinfo.name,
                   db.Vintage.vintage,
                   db.Vintage.notes)
result = result.join(db.Drinkinfo)
result = result.join(db.Vintage)

repFile = r'C:\dev\twcbv4\twcbsrc\reports\reportdefs\daboTest.rfxml'
outFile = r'C:\dev\twcbv4\data\generatedReports\daboTest.pdf'

from dabo.lib.reportWriter import ReportWriter
from dabo.lib import reportUtils

print "Instantiating the report writer..."
rw = ReportWriter()

# Set some required properties:
rw.ReportFormFile = repFile
rw.OutputFile = outFile
rw.Cursor = result.all()

print "Writing %s..." % outFile
rw.write()

As you can see from the above it is quit simple to do.

Create a report definition file

  1. Create a report definition using the Dabo ReportDesigner
  2. With Dabo you would use self.Record['columnName'], with SA you need to do self.Record.__dict__['columnName']
  3. Save the report definition

Run the report against a query

  1. Create a SQLAlchemy session
  2. Create the query to get the columns you need for the report
  3. "rw.Cursor = result.all()" hands the data over to the report writer
  4. You got a PDF!

Clone this wiki locally