oTree Forum >

Sending data from live_method to other groups and rounds

#1 by GKarreskog


Thank you for developing this fantastic platform. I have used it extensively for many years with much delight. 

Right now, I am trying to use it to develop a simple platform for classroom experiments. I want it to be flexible with regards to how many students join the experiment. Some might even join late etc. I, therefore, have the teacher manually move the experiment forward using something similar to the example with experimenter_input. This works great for "static" pages or when I want to decide when they can move forward from a wait page. However, I am now in a situation where I'd like to trigger the live_method functionality. 

Since the experimenter is in a different group and on a different page, I can't use the id_in_group functionality to do this. Looking at the source code, I found the live_payload_function from the live.py file. I thought I might be able to use that, but failed. 

In my attempt, I have the ExpInput in the same app as follows

class ExpInput(Page):
    def live_method(player: Player, data):
        if "type" in data.keys():
            if data["type"] == "complete":
                others = player.get_others_in_subsession()
                for o in others:
                    # send message to all other players
                    code = o.participant.code
                    lookup = get_page_lookup(o.participant._session_code, o.participant._index_in_pages)

                    name_to_match = "<class 'double_oral_auction.MyPage'>"
                    page_name = lookup.page_class.__name__
                    print(str(lookup.page_class) == name_to_match) # This is true
                    live_payload_function(code, page_name, {0: data})

And then the MyPage (which the students will be on) 

class MyPage(Page):
    def live_method(player: Player, data):
        print("live method is ran!!!")
        return data

I would be very thankful for any suggestions!

#2 by Chris_oTree

Can you describe what you want to use the live method for?
And can you set PLAYERS_PER_GROUP to None so the experimenter is in the same group?

#3 by GKarreskog

Hi Chris, 

Thank you for your help. 

I currently have PLAYERS_PER_GROUP = None

I am designing a double oral auction where students, acting as buyers and sellers, can place and accept bids on a live page. The challenge arises when I want to introduce a flexible stopping point. I aim to manually send a signal from an experimenter's page, halting further transactions on all student live-pages. The experimenter's page is only shown to the teacher, and despite multiple rounds for students, the experimenter remains on the first round.

With my current understanding, the standard live_method seems limited to sending data to other players on the same page/round. Considering the experimenter is neither on the same page nor in the same round, I'm trying to find a way to communicate from the experimenter's page to student live-pages.

Is there any way I can send data to the live pages of the students from the experimenter_input page even if they are not on the same page/round?

#4 by Chris_oTree

I would suggest using the REST API to set a session field, like session.halted = True: https://otree.readthedocs.io/en/latest/misc/rest_api.html#session-vars-endpoint

In your live_method, check if this session field has been set. If so, then broadcast this to the whole group, using key '0': {0: ...}

though this means that participants won't be notified immediately, it will only happen when one of the pages does a liveSend.

#5 by GKarreskog

Thank you for your help! 

I'm interpreting your answer as there not being any direct way of sending info to live pages from different pages, but I have to go through a session variable. I now realized that it should be easy to write some javascript code that uses the live method to check if there has been a change to the session variable every 10 seconds or so, so then going through the session variable is not a problem. 

Thank you again for developing this wonderful platform!

Write a reply

Set forum username