from otree.api import * import csv c = cu doc = 'major description of the project\n' class C(BaseConstants): NAME_IN_URL = 'welcome' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 NUM_GRAPHS = 100 NUM_GRAPHS_TO_ASSIGN = 20 TASKS = ('ambiguity', 'choice') INSTRUCTIONS_TEMPLATE = 'welcome/instructions.html' class Subsession(BaseSubsession): pass def get_records_for_results(subsession: Subsession): with open('_static/records/records_100.csv') as f: all_records = csv.DictReader(f) all_records = [tuple(r.values()) for r in all_records] return all_records def creating_session(subsession: Subsession): from random import sample, choice, shuffle from itertools import cycle tasks = cycle([C.TASKS, C.TASKS[::-1]]) graphs = list(range(1, C.NUM_GRAPHS + 1)) all_records = get_records_for_results(subsession) players = subsession.get_players() shuffle(players) for p in players: participant = p.participant task_order = next(tasks) participant.first_task = task_order[0] participant.second_task = task_order[1] participant.task_to_pay = choice(C.TASKS) # for test C.TASKS[0] participant.graphs = sorted(sample(graphs, k=C.NUM_GRAPHS_TO_ASSIGN)) records = [] for n in participant.graphs: records.append(all_records[n - 1]) participant.vars['records'] = records class Group(BaseGroup): pass class Player(BasePlayer): Your_ID = models.IntegerField(label='Please enter your ID number to start:') agreement = models.BooleanField(choices=[[True, 'Yes'], [False, 'No']], label='I declare being of age and accept of free will, after having read and fully understood the above paragraphs, to participate in the study. ', widget=widgets.RadioSelect) q1 = models.StringField( choices=[['[$91,$111]', '[$91, $111]'], ['[$81,$121]', '[$81,$121]'], ['I do not know', 'I do not know']], widget=widgets.RadioSelect) q2 = models.StringField( choices=[['75%', '75%'], ['60%', '60%'], ['I do not know', 'I do not know']], widget=widgets.RadioSelect) num_errors = models.IntegerField(initial=0) class ID(Page): form_model = 'player' form_fields = ['Your_ID'] class Welcome(Page): form_model = 'player' form_fields = ['agreement'] @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant participant.agreement = player.agreement @staticmethod def app_after_this_page(player: Player, upcoming_apps): if not player.agreement: return upcoming_apps[-1] class Instruction(Page): form_model = 'player' form_fields = ['q1', 'q2'] def error_message(player: Player, values): if values["q1"] != '[$91,$111]' or values["q2"] != "60%": player.num_errors += 1 return "There is at least one mistake in your responses. Please try again." @staticmethod def vars_for_template(player: Player): participant = player.participant return dict( graphs=[f'stock/record_id_{n}.png' for n in participant.graphs] ) # class Tograph(Page): #form_model = 'player' class Graph(Page): #timeout_seconds = 10 @staticmethod def vars_for_template(player: Player): participant = player.participant return dict( graphs=[f'stock/record_id_{n}.png' for n in participant.graphs] ) class Next(Page): form_model = 'player' @staticmethod def app_after_this_page(player: Player, upcoming_apps): participant = player.participant if participant.first_task == C.TASKS[0]: return f'{C.TASKS[0]}_task_1' else: return f'{C.TASKS[1]}_task_1' page_sequence = [Welcome, Instruction, Graph, Next]