SESSION_CONFIG to Constants

752 views
Skip to first unread message

Dustin T

unread,
Sep 15, 2021, 12:16:03 AM9/15/21
to oTree help & discussion
Hello all,

In otree lite I'd like to allow the number of rounds to be set in the session configs, and then read it into the constants.  I tried: 

class Constants(BaseConstants):
     num_rounds = session.config["num_rounds"]

I'd appreciate any help as to how to do this without "self".

Thanks,
Dustin 

Chris @ oTree

unread,
Sep 15, 2021, 12:28:44 AM9/15/21
to oTree help & discussion
See the random_num_rounds apps here: https://www.otreehub.com/projects/otree-snippets/

Bhagya Gunawardena

unread,
Sep 29, 2021, 8:42:45 AM9/29/21
to oTree help & discussion
Hello Chris, 

I have the same question and checked the above link. If I'm not mistaken, in "random_num_rounds" app, you set num_rounds to a larger number and display pages conditionally (based on random numbers). Is there a way where the experimenter set a fixed number of rounds (or set a value of any other variable) in session config and use it as a constant? For example, can I assign the value of "num_seq" and "num_rounds_per_seq" in the following code in the session config, so I can change the number of rounds when starting a session without changing the original code? (I'm using self format) 


class Constants(BaseConstants):
    name_in_url = 'New_Stages'
    players_per_group = None
    num_seq = 3
    num_rounds_per_seq = 10
    num_rounds = num_seq * num_rounds_per_seq

Thank you,

Bhagya 

Corey Scheinfeld

unread,
Dec 22, 2021, 10:09:25 PM12/22/21
to oTree help & discussion
Were you able to find a solution for this?

jareck...@gmail.com

unread,
May 17, 2022, 7:54:58 AM5/17/22
to oTree help & discussion
Hey, did anybody find a way to pass session.configs to Constants?

Chris @ oTree

unread,
May 18, 2022, 5:11:11 AM5/18/22
to oTree help & discussion
passing session config to constants is not supported.

Pei-Hsun

unread,
May 24, 2022, 4:35:38 PM5/24/22
to oTree help & discussion
Hi Chris,

I was wondering if I can pass session config to Player class for the labels in forms? If that is not supported, is there any alternative?

Thanks,
Pei-Hsun

Chris @ oTree 在 2022年5月18日 星期三上午5:11:11 [UTC-4] 的信中寫道:

Chris Crabbe

unread,
May 24, 2022, 6:40:07 PM5/24/22
to Pei-Hsun, oTree help & discussion
Hi Pei-Hsun -

Yes, you can access session.config from anywhere in your __init_py__ that has a oTree model variable (an instance of Player, Group, Subsession, Page, WaitPage, etc).  You can also access session.config directly from your template (by using {{ session.config.keyname }} to have it print out the value of session.config['keyname']

Thanks,
--Chris

--
You received this message because you are subscribed to the Google Groups "oTree help & discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/65cd49e7-ec9b-41e9-918d-60b77201d79en%40googlegroups.com.

Pei-Hsun

unread,
May 25, 2022, 3:19:06 PM5/25/22
to oTree help & discussion
Hi Chris,

I was still confused about how to access session.config in Player class. Should I make a function inside the Player class as in Page class?

Thanks,
Pei-Hsun

ccr...@gmail.com 在 2022年5月24日 星期二下午6:40:07 [UTC-4] 的信中寫道:

Chris Crabbe

unread,
May 25, 2022, 6:24:39 PM5/25/22
to Pei-Hsun, oTree help & discussion
Hi Pei-Hsun -

You can access the 'session' associated with any oTree object.  If you have a player instance named "p" you just use p.session.  If you have a Group object named "grp" you just use group.session.  And so on for any participant or subsession instance.

It's explained a little bit here:  https://otree.readthedocs.io/en/latest/conceptual_overview.html#object-hierarchy - you may benefit from re-reading the whole "Conceptual Overview" and "Models" sections in the documentation.

Good luck,
--Chris

Pei-Hsun

unread,
May 25, 2022, 6:46:30 PM5/25/22
to oTree help & discussion
Hi Chris,

I have tried to use player.session.config in class Player(BasePlayer), but it did not work out. I am sorry for the stupid question, but could you tell more about how to make a player instance in class Player(BasePlayer)?

Thanks

ccr...@gmail.com 在 2022年5月25日 星期三下午6:24:39 [UTC-4] 的信中寫道:

Chris Crabbe

unread,
May 25, 2022, 7:04:41 PM5/25/22
to Pei-Hsun, oTree help & discussion
Hi Pei=Hsun-

I see.  Model field definitions in class Player can't access session instances or player instances because they're read/executed by oTree when oTree starts up - not when you're running a session -  so session.config doesn't mean anything then, without an associated session.

Instead of assigning a value to a Player field from session.config inside of class Player, do it inside of creating_session.  That runs when you create a session, and it takes subsession: Subsession as an argument, so you can get the subsession's Player and Session objects from there.

Thanks,
--Chris

Pei-Hsun

unread,
May 25, 2022, 10:09:00 PM5/25/22
to oTree help & discussion
Hi Chris,

Thank you for your explanation. If I want to use that value in a form's label in class Player, is the only way to do so to assign that value in Constant class? It works, but since several apps share the same constant, so I think that it would be better if I can put that in session.config.

Thanks!


ccr...@gmail.com 在 2022年5月25日 星期三下午7:04:41 [UTC-4] 的信中寫道:
Reply all
Reply to author
Forward
0 new messages