#1 by adg1
This is possibly quite an embarrassing question, but for some reason I have not found an answer yet. In the documentation itself, it is said that one should repeat pages instead of creating a new one for each iteration. But how does one loop a page in oTree? The only way I have found is to simply do this: page_sequence = [Page1, Page1, Page1, Page2] if I want to repeat Page 1 three times. I assumed there would be a more elegant way where I can just change a number instead of copy and pasting the amount of repetitions into the page sequence. Is there?
#2 by gr0ssmann
You can always do [Page1] * 42 + [RemainingPage1, …] A better approach may be to use rounds.
#3 by adg1
Rounds cannot be used in my setup as it would repeat the whole thing and not just the one page. I thought of just splitting it up into two apps, but then there is no elegant way of repeating the whole experiment (which I can do by using rounds in my setup). Cheers for your input though. If that works then that might be the easiest solution. Still does not look pretty, but it's better than writing 'Page 1' x amount of times.
#4 by qxiao (edited )
It is possible to show only a subset of pages in a round with is_displayed(). So if you just want your Page1 to be repeated (say 3 times), you can set Page2 to be displayed only in the final round (round_number == 3).
#5 by adg1 (edited )
Thanks for this! I need to repeat the one page 10 times before the others can appear once. And the whole experiment should be repeated 5-10 times too. So I would make all other pages appear only every 10 rounds. This is still not very pretty, but I assume this is how it is meant to be done when having nested loops? Or is there a way to add some logic to a page (repeat until n==10) and before_next_page n+=1? This is what I initially tried to do, but failed.
#6 by qxiao
I'd say this is probably the prettiest (maybe also the "standard"). I do not feel comfortable making exact copies of pages/apps, this would make the program hard to maintain.
#7 by adg1
Yes I agree, copying pages/apps is exactly what I want to avoid. Thanks for the help!
#8
by
BonnEconLab
adg1 asked, > Or is there a way to add some logic to a page (repeat until n==10) and before_next_page n+=1? This is what I initially tried to do, but failed. qxiao already mentioned is_displayed: https://otree.readthedocs.io/en/latest/pages.html#is-displayed. This can be used to display the page exactly as often as you desire and even conditionally on earlier events (say, only if participants made a particular choice). gr0ssmann wrote, > You can always do > > [Page1] * 42 + [RemainingPage1, …] I would like to point out an important caveat with this approach: If you have input fields on the page to be repeated, the input values from earlier repetitions will be overwritten with each repetition unless you explicitly store them in a different variable (append them to a separate list). This issue does not arise if you use multiple rounds. > A better approach may be to use rounds. I thus second that!
#9
by
BonnEconLab
adg1 wrote, > Rounds cannot be used in my setup as it would repeat the whole thing and not just the one page. You *can* use rounds, as qxiao already suggested. Here are some code snippets: class C(BaseConstants): ... NUM_ROUNDS = 3 ... class Introduction(Page): ... @staticmethod def is_displayed(player): return player.round_number == 1 class PageToBeRepeated(Page): ... class Results(Page): ... @staticmethod def is_displayed(player): return player.round_number == C.NUM_ROUNDS page_sequence = [ Introduction, PageToBeRepeated, Results, ]
#10 by adg1
Thank you very much for the detailed response! I think for me, it just felt wrong to have the second round of the whole experiment start at the 11th round in oTree. But I see now that it really was just a psychological barrier, and this works perfectly fine.