oTree Forum >

Live Page Not Working

#1 by PatrickT

Good afternoon, I am in the process of coding a Gift Exchange Experiment and cannot figure out why my live page is not working. Here are the functions I am testing:

On the page I have:
<table id="bid_history" class="table">
<tr>
  <th>Bid Number</th>
  <th>Wage</th>
</tr>
</table>
...
    <div id="wage_offer" style="display: block">
        Please make a wage offer. <br>
        <input id="wage_offer_input" type="number">
        <button type="button" onclick="wage_offer()">Make Wage Offer</button>        
    </div>
...
<script>
    var firm_id;
    var wage_offer_made;
    var bid_number;
    var firm_offer_id;
    let wage_offer_input = document.getElementById('wage_offer_input');
    let bid_history = document.getElementById('bid_history');
    let transaction_history = document.getElementById('transaction_history');
    function wage_offer(){
        wage_offer_made=parseInt(wage_offer_input.value);
        firm_id={{player.id_in_group}};
        liveSend({"information_type": "offer", "offer":wage_offer_made, "id_in_group":firm_id});
    }
</script>
...
<script>
    function liveRecv(data){
        if (data["information_type"]=="offer"){
            wage_offer_amount=data["offer"];
            bid_number=data["bid_number"];
            firm_offer_id=data["firm_offer_id"];
            bid_history.innerHTML += '<tr><td>' + data.bid_number + '</td><td>' + data.offer + '</td></tr>';
</script>

And for my live method I have:

if data["information_type"]=="offer": #and data["offer"]>=group.highest_bid and data["offer"] % 5==1 and data["id_in_group"]==player.id_in_group
    group.highest_bid=data["offer"]
    group.bid_number=group.bid_number+1
    player.wage=data["offer"]
    player.bid_number_list=group.bid_number
    return {0: {"information_type": "offer", "offer":data["offer"], "bid_number":player.bid_number_list, "firm_offer_id":data["id_in_group"]}}
    
 The chart, button, and inputbox load correctly, but when I try and make an offer, nothing happens. I am testing using Otree zipserver. Any input would be greatly appreciated!

#2 by Trontatuma

whats your error?

#3 by PatrickT

Hi, I do not get an error. I input a number in the input box and then click “Make Wage Offer” but nothing happens. Would there be an error popping up in windows power shell?

#4 by Trontatuma

First step should be to add some print statements in Python, e.g. print(data) in live_method and console.log(data_to_send) in JS. This will help you understand where the problem is. Is the data not arriving or not being sent back?

#5 by PatrickT

Hi, thank you for the advice, I tried it, and could never get console.log() to send anything to the console. So, I tried to make a basic test and see if anything would send:

<input id="test">
<button type="button" onclick="test()">Send Test</button>

<script>
function test() {
    console.log("test");
  }
</script>

and still, nothing pops up in my console. Is there perhaps something wrong with my computer?

#6 by nicolas

basic test could be:

.html file:

<input type="button" onclick="handleClick()"/>
<script>
    function handleClick() {
        liveSend("hello from client");
    }
</script>

__init__.py:

class MyTest(Page):
    @staticmethod
    def live_method(player, data):
        print("received a message:", data)

#7 by PatrickT

Thanks Nicolas, I tried using this code and did not get any messages in the console. Any ideas what could be happening?

#8 by nicolas

Hi PatrickT, sorry, the content of the print function:
"received a message:", data
Would be printed on the console where you are running otree
for example if you use PyCharm or Visual Studio Code, you would see it there when you run for example "otree devserver"

This is code which receives messages from the client (the browsers) to the server (where otree is running):

class MyTest(Page):
    @staticmethod
    def live_method(player, data):
        print("received a message:", data)

#9 by PatrickT

Hi Nicolas, I’m using windows power shell and the otree zipserver command. I have a player function elsewhere which does successfully send a message in the power shell console. However, when I use the button and function you suggested, I get don’t get any message in the powershell.

#10 by PatrickT

Hi again Nicolas, I finally figured it out, thanks to your comments, I was double checking everything and I figured out that I forgot to enable the live_method on the page (I am using Otree Studio). Thank you so much.

Write a reply

Set forum username