oTree Forum >

Conditional display of messages

#1 by js8773420

Hi everyone, 

I am trying to conditionally display a message on a page only if a variable in my code cumulA is >= 1. I am relatively new to oTree so could be missing something obvious, but does anyone know how I can do it?

This is the code I have been trying to use in the html page:
{{ if player.cumulA >= 1 }}
{{ endif }} 

And this is the part of my code where cumulA is referenced:
class Nudge(Page):
    def is_displayed(player):
        return player.participant.nudge == 1

    def vars_for_template(player):
        current_t = player.round_number
        max_t = C.NUM_ROUNDS
        # get the affectation of the player:
        actifs = (player.actifs).split(",")
        idxs_prix = []
        for i in range(1, (current_t + 1) + C.history):
        prices_to_show = get_prices(player)
        # in order to compute the positions in the portfolio:
        qtes_pfeuille = []
        cumulA = sum([p.quantityA * p.decisionA for p in player.in_all_rounds()])
        cumulB = sum([p.quantityB * p.decisionB for p in player.in_all_rounds()])
        cumulC = sum([p.quantityC * p.decisionC for p in player.in_all_rounds()])
        cumulD = sum([p.quantityD * p.decisionD for p in player.in_all_rounds()])
        cumulE = sum([p.quantityE * p.decisionE for p in player.in_all_rounds()])
        cumulF = sum([p.quantityF * p.decisionF for p in player.in_all_rounds()])
        qtes_pfeuille = [cumulA, cumulB, cumulC, cumulD, cumulE, cumulF]
When I try to run the code, the page comes back with an error saying "Error evaluating the condition in the 'if' tag" but runs fine if I remove the conditional display code from the html.

#2 by js8773420 (edited )

Sorry everyone, I have figured out how to do this with a bit of trial and error + another look at my code so all good. Instead I am trying to conditionally display a message based on if at least two of the cumulX variables are greater than 0 (e.g. cumulA > 0 and cumulB > 0). I have tried to do it via introducing a new set of variables as heldX as you can see in the code below and just crudely use the int function to see if each cumulX variable is > 0 but I am running into the same issue with my page saying: "Cannot resolve the variable 'held_stocks' (line 141, in "held_stocks.0")"

I added this into the def vars_for_template(player) section: 
        held_stocks = []
        heldA = int(cumulA / 90000000)
        heldB = int(cumulB / 90000000)
        heldC = int(cumulC / 90000000)
        heldD = int(cumulD / 90000000)
        heldE = int(cumulE / 90000000)
        heldF = int(cumulF / 90000000)
        held_stocks = [heldA, heldB, heldC, heldD, heldE, heldF]

And this to the html page: 
 <p> {{ held_stocks.0 }} </p>
 This is what worked for my previous issue so I am unsure why it is not working here. Is it maybe because of the crude way I used to try to see if cumulX > 0 ? If anyone is able to help I think I am truely stuck this time so I would greatly appreciate it. 

#3 by js8773420

Please ignore all the above. Has now been sorted. Sorry again everyone. If anyone knows how to delete a thread, please let me know.

Write a reply

Set forum username