Displaying all players results

487 views
Skip to first unread message

Jack McLaughlin

unread,
Apr 9, 2021, 11:52:43 AM4/9/21
to oTree help & discussion
Hi,

I'm creating a real effort task in otree 5.0 using live pages, the task works well but I'm having trouble with my results page. I need a table displaying by participant each persons effort (variable reflecting this is called 'donation' stored at a player level), i.e. in results everyone see's what everyone else got for donation. I'm having trouble with creating this table, I only seem to be able to get the info from the current player and not all other players in the subsession as well. Please see below for excerpts from my code.

In the code I'm aware that matrix is only returning the one players values (i have it there to illustrate the method i was trying, i know its wrong - but not how to fix it), i also tried creating a variable based on player.get_others_in_subsession() but all the matrix then returned was [Player(round==1)] or had an error saying that the variable didn't have the attributes i was trying to access.

Thanks in advance for any help you can provide :)

----Code------
class Results(Page):
    form_model = 'player'
        
    def is_displayed(player: Player):
        return player.round_number == 1

    def vars_for_template(player: Player):
        table_rows = []
        matrix = [[player.id_in_group, player.total_payoff, player.donation]]
        print(matrix)
        row = {
            'Player': matrix[0][0],
            'Points': matrix[0][1],
            'Donation': matrix[0][2],
        }
        table_rows.append(row)
        player.participant.vars['t1_results'] = table_rows

        return {
            'table_rows': table_rows,
            'total_payoff':round(player.total_payoff),
            'donation':round(player.donation),
        }
------------------------html page----------------------

{% load staticfiles otree_tags %}

{% block title %}
    Task Summary
{% endblock %}

{% block content %}

    <p class="text-success">
    </p>

    <div class="panel panel-success">

        <div class="panel-heading">
            <h4>The total points your effort resulted in was: {{ player.total_payoff }}</h4><br>
            <h4>The amount of donation that you earned is: {{ player.donation }}</h4><br>
        </div>

        <table class="table">   
            {% for row in table_rows %}
            <tr>
                {% for column in row %}
                    <td>{{column}}</td>   
                {% endfor %}
            </tr>
            {% endfor %}        
            <!-- <tr>
                <th><center>Player</th>
                <th><center>Points</th>
                <th><center>Donation</th>
            </tr>
            {% for row in table_rows %}
            <tr>
                <td align="center">{{ row.Player }}</td>
                <td align="center">{{ row.Points }}</td>
                <td align="center">{{ row.Donation }}</td>
                
            </tr>
            {% endfor %} -->

        </table>

    </div>

    {% next_button %}<br><br><br>

{% endblock %}

Cheers,
Jack

Rok

unread,
Apr 9, 2021, 1:28:36 PM4/9/21
to oTree help & discussion
Hello, 

If you just want to display player fields in a table, you can just to do it all in html.

<table>
{% for p in subsession.get_players %}
<tr>
<td>{{ p.id_in_group }}</td>
<td>{{ p.total_payoff}}</td>
<td>{{ p.donation}}</td>
</tr>
{% endfor %}
</table>

If I understood what you're trying to do, this should do something similar to what you're trying to achieve?

Best Regards,
Rok

Jack McLaughlin

unread,
Apr 9, 2021, 2:24:37 PM4/9/21
to oTree help & discussion
Hi Rok,

Thank you very much, that solution works perfectly. So simple, I feel a fool for not considering doing it all in html.

I appreciate your help, have a great weekend!

Cheers,
Jack

Rok

unread,
Apr 9, 2021, 5:05:07 PM4/9/21
to oTree help & discussion
Haha, don't worry about it. We all sometimes over-engineer stuff that has simple solutions. :)

Good luck with your experiment,
Rok

Reply all
Reply to author
Forward
0 new messages