oTree Forum >

Unable to store choice from randomly generated radio button

#1 by AngelVJimenez (edited )

I am designing a multiplayer experiment in which participants at each round choose between two random participants to partner with one of them for a task. Participants are provided with the ID and the reputation of their potential partners. 
Because the potential choices are randomly generated, I created a function to do this and then a radio button in the html template with the random options from the function. 
However, I am unable to store the ID of the chosen partner. 
Does someone know how to do this? 
This is my code: 
def vars_for_template(player):
import random

# Get all players except the target player
all_players = player.subsession.get_players()
other_players = [p for p in all_players if p.id_in_group != player.id_in_group]

# Specify the number of players to select for the pool of candidates
subsample_size = 2

# Ensure that there are enough players to select in the subsample
if len(other_players) >= subsample_size:
    # Select a subsample of other players
    potential_partners = random.sample(other_players, subsample_size)
else:
    # Handle the case when there are not enough other players
    potential_partners = []

# Create a list of dictionaries to store the reputation and cumulative reputation of all potential partners
selected_partners_data = []

for partner in potential_partners:
    partner_data = {'id': partner.id_in_group}
    
    # Calculate the cumulative reputation using sum and list comprehension
    partner_data['cumulative_reputation'] = sum([p.reputation for p in partner.in_all_rounds()])
    
    selected_partners_data.append(partner_data)

# Extract partner IDs
partner_ids = [partner['id'] for partner in selected_partners_data]

# Pass the list of selected partners' data (including cumulative reputation) and their IDs as context to template
return {
    'selected_partners_data': selected_partners_data,
    'partner_ids': partner_ids,
}


And this is my HTML template: 
<p>Your potential partners and their reputations are:</p>

{% for partner_data in selected_partners_data %}
    <label>
        <input type="radio" name="chosen_partner" value="{{ partner_data.id }}">
        Partner ID: {{ partner_data.id }}
        Reputations: {{ partner_data.cumulative_reputation }}
    </label><br>
{% endfor %}




{{ formfields }}

{{ next_button }}


Thank you very much. 

Angel

#2 by ccrabbe

Hi Angel -

To store the partner data,  instead of just storing them in a local variable inside of vars_for_template, you should store them in model fields in the Player class.  Define the player model field, then inside of vars_for_template set the values alongside the code you already have.  All your player model fields are automatically included in the output reports.

I would also like to advise that running randomization inside of vars_for_template isn't recommended, because vars_for_template is executed every time a player refreshes the page.  If you randomize there, subjects can refresh the page until they get values they want.  Instead, I suggest you do the randomization and value assignments inside of creating_session, which is only run once when the session is created.

Thanks,
--Chris

#3 by ccrabbe

Hi Angel -

And to store the results of their choice, you need to define a form_model and form_fields inside your Page class, and then name the inputs like described in the documentation here:  https://otree.readthedocs.io/en/latest/forms.html#raw-html-widgets

Then when they submit the page the decision will be stored in the model field you have in form_fields.

Thanks,
--Chris

Write a reply

Set forum username