from otree.api import * import pandas as pd doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'tree_task_template' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 EX_CHOICE = 8 # which choice will be presented in the example COMP_CHECK_CHOICE = 3 # which choice will be used for the comprehension check CURRENCY_NAME = '£' class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): comp1 = models.FloatField( label=f'How much money in {C.CURRENCY_NAME} do you invest to mitigate climate change? (numeric values only, ' 'without unit sign; "." as decimal separator)' ) comp2 = models.FloatField( label=f'How much money in {C.CURRENCY_NAME} do you keep for yourself? (numeric values only, without unit sign; ' '"." as decimal separator)' ) comp3 = models.IntegerField( label='How many trees are planted with the money you invest to mitigate climate change?' ) comp4 = models.IntegerField( label='How much CO2 do you offset in kg? (numeric values only, without unit sign)' ) choice = models.IntegerField( label='Please select your "Choice" that will be implemented (dropdown menu ranging from "Choice 0 trees" to ' '"Choice 10 trees").', choices=[[i, f'Choice {i} tree' if i == 1 else f'Choice {i} trees'] for i in range(11)], ) email = models.StringField( label='Please insert your email address if you want to receive a confirmation when the trees have been ' 'planted.', blank=True ) effectiveness = models.IntegerField( label='How effective do you consider tree planting to be as a climate protection measure?', widget=widgets.RadioSelect, choices=[ [1, 'very effective'], [2, 'effective'], [3, 'not very effective'], [4, 'not effective at all'] ] ) # FUNCTIONS def creating_session(subsession: Subsession): data = pd.read_excel('tree_task_template/tree-task-table.xlsx') print(data) # save the data from the excel-file as a dataframe, to be able to reference it later subsession.session.tree_task_data = data # PAGES class TaskDescription(Page): @staticmethod def vars_for_template(player: Player): # return the endowment in the currency-format return dict(endowment=cu(player.session.config['endowment'])) class ComprehensionCheck(Page): # test change form_model = 'player' form_fields = [f'comp{num}' for num in range(1, 5)] @staticmethod def vars_for_template(player: Player): session = player.session tree_task_data = session.tree_task_data # get the values of the column that is used for the example on the page ex_values = tree_task_data.iloc[C.EX_CHOICE] # return the matching values to the Page in the correct form (integer / currency) return dict(endowment=cu(session.config['endowment']), investment=cu(ex_values['investment']), rem_balance=cu(ex_values['rem_balance']), planted_trees=int(ex_values['planted_trees']), co2_offset=int(ex_values['co2_offset']), km_offset=int(ex_values['km_offset'])) @staticmethod def error_message(player: Player, values): error_msg = dict() # locate the correct values from the dataframe comp_check_solutions = player.session.tree_task_data.iloc[C.COMP_CHECK_CHOICE] values_to_check = [comp_check_solutions['investment'], comp_check_solutions['rem_balance'], comp_check_solutions['planted_trees'], comp_check_solutions['co2_offset']] # checks for every comprehension check if the given answer is correct, i.e. if it is the same as in the # dataframe. If not all values are correct, return an error-message for i in range(1, 5): if values[f'comp{i}'] != values_to_check[i-1]: error_msg[f'comp{i}'] = 'The question was solved incorrectly' return error_msg class Decision(Page): form_model = 'player' form_fields = ['choice', 'email'] @staticmethod def vars_for_template(player: Player): return dict(endowment=cu(player.session.config['endowment'])) class Effectiveness(Page): form_model = 'player' form_fields = ['effectiveness'] class Results(Page): @staticmethod def vars_for_template(player: Player): # get the values (investment, planted trees etc.) corresponding to the selected choice selected_choice = player.session.tree_task_data.iloc[player.choice] player.payoff = cu(selected_choice['rem_balance']) return dict(investment=cu(selected_choice['investment']), rem_balance=cu(selected_choice['rem_balance']), planted_trees=int(selected_choice['planted_trees']), co2_offset=int(selected_choice['co2_offset'])) page_sequence = [TaskDescription, ComprehensionCheck, Decision, Effectiveness, Results]