#1 by dimdub34
Hello oTree community, I would like to display a link to the current room on the last page of my application for later review of results. Since I will have several rooms, I need to programmatically obtain the room name in which the session is running. Could you advise on how to achieve this within the `__init__.py` file? Thank you for your assistance. Best regards, Dimitri
#2
by
BonnEconLab
(edited )
It seems, according to some testing that I just did, that you can access the room name from within a method with the player argument via player.session.otree_RoomToSession[0].room_name Please note that this works only if you are actually running your experiment within a room. Otherwise the otree_RoomToSession attribute of player.session is an empty list. Hence, to make your code robust you will need to check first whether player.session.otree_RoomToSession[0].room_name actually exists. That is, include something like try: room_name = player.session.otree_RoomToSession[0].room_name except: room_name = "I am not running in a room." or if player.session.otree_RoomToSession == []: room_name = "I am not running in a room." else: room_name = player.session.otree_RoomToSession[0].room_name Complete example code: class MyPage(Page): @staticmethod def vars_for_template(player): try: room_name = player.session.otree_RoomToSession[0].room_name except: room_name = "I am not running in a room." return dict( room_name = room_name, )
#3 by dimdub34
Thank you, it seems to work. It's strange that we cannot access room_name from subsession.session (for example, in the creating_session method). I even tried: def creating_session(subsession=: first_player = subsession.get_players()[0] room_name = first_player ... but it's empty. Anyway, thank you very much—maybe something to consider for a future version. Best, Dimitri
#4
by
Chris_oTree
Hi, note that the above described solution uses unofficial/undocumented attributes. I cannot guarantee that it will continue to work in future oTree versions. (I say this because some novice users who find this post might not be familiar with the difference between stable and unstable APIs, and end up shocked when their code doesn't work after an upgrade.)
#5
by
BonnEconLab
> It's strange that we cannot access room_name from subsession.session (for example, in the creating_session method). It seems that creating_session is run too early. (I also just tried this.) If you put retrieval of the room name on the first page of your experiment, it works. > Hi, note that the above described solution uses unofficial/undocumented attributes. I cannot guarantee that it will continue to work in future oTree versions. I should have mentioned that I used oTree 5.11.1.