oTree Forum >

HTML display image by round number?

#1 by R800377803

Hi coders, I am hoping to display a different image on my page each round. 
Any ideas how to approach this?

I am guessing that adding if logic to pass the images into vars_for_template might be my best shot, 
for example, some dummy code:

    @staticmethod
    def vars_for_template(player: Player):
        if round_number < 5:
            return dict(
            IMG = "picture_one.png"
                )
           else:
               return dict(
            IMG = "picture_two.png"
                )
  
Is this feasible?

#2 by BonnEconLab (edited )

Yes, this is feasible. The correct variable is player.round_number, however. Here is more parsimonious code:

    @staticmethod
    def vars_for_template(player):
        if player.round_number < 5:
            IMG = "picture_one.png"
        else:
            IMG = "picture_two.png"
        return dict(IMG=IMG)

In you HTML template, you would then have to include

<img src="{{ static IMG }}">

(assuming that the files picture_one.png and picture_two.png are located in your app’s static folder or in the global _static folder, see https://otree.readthedocs.io/en/latest/misc/advanced.html#staticfiles).

#3 by R800377803

It's working great! Thank you!

#4 by R800377803

It's working great! Thank you!

Write a reply

Set forum username