oTree Forum >

Creating a simple profile

#1 by rionny

Hi, I was wondering if any simple profile templates exist. 

I wanted to ask general survey questions to participants (age, name, gender etc) and display all players answers on a screen at the same time. Displaying the participants own answers is simple enough, but I'm really struggling to display the other players answers so I would really appreciate some help with this.

-Z

#2 by aseyq

If they are in the same group, you can loop over them `group.get_players()` function to create a list like this:

```
class Results(Page):
    def vars_for_template(player):
        group_answers = []
        for p in player.group.get_players():
            group_answers.append(dict(name=p.name, age=p.age))

        return dict(group_answers=group_answers)
```

And in your template file, you can loop over your generated answers:

```
{{ for p in group_answers }}
        I am {{ p.name }} and I am {{ p.age }} years old. </br>
    {{ endfor }}
```
    
It will look like: 

I am Anna and I am 55 years old.
I am Bob and I am 59 years old.
I am Carol and I am 18 years old.

If the players are not grouped and you want to list the answers of everyone in the session, you can do the same thing but with the subsession level equivalent of the same function:

    def vars_for_template(player):
        group_answers = []
        for p in player.subsession.get_players():
            group_answers.append(dict(name=p.name, age=p.age))

        return dict(group_answers=group_answers)
        
There are also handy player functions to get a list of others excluding yourself: player.get_others_in_subsession() player.get_others_in_group()

Here are some overview of the functions: https://otree.readthedocs.io/en/latest/multiplayer/groups.html#groups

Write a reply

Set forum username