from otree.api import models, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Page doc = 'consent and balancing treatments for dropouts' n_treatment_groups = 8 class C(BaseConstants): NAME_IN_URL = 'consentapp' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 TREATMENTS = list(range(n_treatment_groups)) class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): session = subsession.session session.completions_by_treatment = {x: 0 for x in C.TREATMENTS} class Group(BaseGroup): pass class Player(BasePlayer): export_media_order_id = models.IntegerField() class Intro(Page): @staticmethod def before_next_page(player: Player, timeout_happened): session = player.session # get treatment group id with the least completions player.export_media_order_id = min( C.TREATMENTS, key=lambda idx: session.completions_by_treatment[idx], ) # copy media order id to "global" participant to carry data over to actual study app participant = player.participant participant.media_order_id = player.export_media_order_id page_sequence = [Intro]