Generating under new ProcessΒΆ
This is exactly the same report we made for “without-Django” test. But this is going to be ran under an independent Process, from multiprocessing library.
Is important to keep aware on:
multiprocessing works on Python 2.3 or higher - and is a builtin package on 2.6
you must use ‘generate_under_process_by’ instead of ‘generate_by’
you can do it without ‘generate_under_process_by’, using geraldo.utils.run_under_process decorator, if you prefer do it manually
the method ‘’generate_under_process_by’ is not the best solution ever. You must keep aware on what kind of report generation you are doing, and how server is configure, but most of cases will work well
the decorator ‘run_under_process’ will work only if geraldo.utils.DISABLE_MULTIPROCESSING is False
>>> import os >>> cur_dir = os.path.dirname(os.path.abspath(__file__))
>>> from geraldo.utils import A4, cm, TA_CENTER, TA_RIGHT
>>> from geraldo import Report, ReportBand, Label, ObjectValue, SystemField,\ ... FIELD_ACTION_COUNT, BAND_WIDTH
Report class
>>> class SimpleListReport(Report):
... title = 'Demonstration without Django'
...
... class band_page_header(ReportBand):
... height = 1.3*cm
... elements = [
... SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
... style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
... Label(text="ID", top=0.8*cm, left=0),
... Label(text="Name", top=0.8*cm, left=3*cm),
... ]
... borders = {'bottom': True}
...
... class band_page_footer(ReportBand):
... height = 0.5*cm
... elements = [
... Label(text='Created with Geraldo Reports', top=0.1*cm, left=0),
... SystemField(expression='Page # %(page_number)d of %(page_count)d', top=0.1*cm,
... width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
... ]
... borders = {'top': True}
...
... class band_detail(ReportBand):
... height = 0.5*cm
... elements = [
... ObjectValue(attribute_name='id', top=0, left=0),
... ObjectValue(attribute_name='name', top=0, left=3*cm),
... ]
>>> class MyObject(object):
... def __init__(self, **kwargs):
... for k,v in kwargs.items():
... setattr(self, k, v)
>>> objects_list = [
... MyObject(id=1, name='Rio de Janeiro'),
... MyObject(id=2, name='New York'),
... MyObject(id=3, name='Paris'),
... MyObject(id=4, name='London'),
... MyObject(id=5, name='Tokyo'),
... MyObject(id=6, name='Moscow'),
... MyObject(id=7, name='Beijing'),
... MyObject(id=8, name='Hamburg'),
... MyObject(id=9, name='New Delhi'),
... MyObject(id=10, name='Jakarta'),
... ]
>>> report = SimpleListReport(queryset=objects_list)
PDF generation
>>> from geraldo.generators import PDFGenerator
>>> report.generate_under_process_by(PDFGenerator, filename=os.path.join(cur_dir, 'output/generated-in-multiprocessing.pdf'))