Trying to build a modular report generator

What I’m trying to do:
I am using Anvil to create a report generator using PDFRenderer, etc. What I’d like to do is use a different form for each page of the report, then in code I can include/exclude pages depending on reporting requirements.

What I’ve tried and what’s not working:
I’ve tried using a ‘base’ form with a column panel, and then in code I .add_component depending on reporting requirements. For some reason, the component forms (panel1, panel2, panel3) are not rendering.

Code Sample:

from ._anvil_designer import main_formTemplate
from anvil import *
from ..panel1 import panel1
from ..panel2 import panel2
from ..panel3 import panel3

class main_form(main_formTemplate):
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.
  def form_show(self, **properties):
    self.main_cp.add_component(panel1())
    self.main_cp.add_component(panel2())
    self.main_cp.add_component(panel3())

Clone link:
https://anvil.works/build#clone:X4LE4AJ56G4AB6VW=A44MRCPK2FZY4ENXTGKKYPAP

Your form isn’t hooking into your form_show function. In the form’s properties panel, scroll down to the bottom and click the arrow next to form_show to hook it up.

image

There is no need to add to the main_cp, you can use a blank panel form and add to it.

Here is how I create a PDF with the description of a truck on the first page followed by one page per crate. The PageBreak is imported from Anvil Extras

from anvil_extras.PageBreak import PageBreak

self.add_component(TruckPDF(truck))
for crate in truck.crates:
    self.add_component(PageBreak())
    self.add_component(CratePDF(crate))

Rookie mistake, thanks!

1 Like