Mf5F
Mf5F

Reputation: 21

Jinja in Python: How to make IF statements work?

I am trying to automate some document generation with Jinja in a Python environment. I customised the jinja environment in order not to get conflicts with default character meanings in my IDE. The line_statement_prefix is set to #.

    latex_jinja_env = jinja2.Environment(
    block_start_string = '\BLOCK{',
    block_end_string = '}',
    variable_start_string = '\VAR{',
    variable_end_string = '}',
    comment_start_string = '%',
    comment_end_string = '}',
    line_statement_prefix = '#',
    line_comment_prefix = '%#',
    trim_blocks = True,
    autoescape = False,
    loader = jinja2.FileSystemLoader(os.path.abspath('.')))

I am trying to conditionally activate rows of a table, if a particular cell content is not 'nan'.

{# if \VAR{row['Climate (where PUE is located)']} not 'nan'}
{{Climate & \VAR{row['Climate (where PUE is located)']}\\}}
{# endif}

So far, any possible variation of the if statement did not yield the desired outcome. I'd be very grateful for any suggestions. Thank you.

Upvotes: 0

Views: 159

Answers (1)

Mf5F
Mf5F

Reputation: 21

Figured out that the content of \VAR cannot be accessed for evaluation, it is just possible to "print" it statically to the document that's being generated.

I solved the issue by generating a .tex document containing all vallues (including nan), then let a little script run over said .tex file in order to remove all rows with nan-value.

Upvotes: 2

Related Questions