session variables

884 views
Skip to first unread message

jason....@gmail.com

unread,
Aug 10, 2021, 4:25:20 PM8/10/21
to oTree help & discussion
Hi all,

This is probably a silly question, but one that I haven't been able to figure out by looking at the documentation.

When I was last using oTree, I was able to call a session level variable inside a method using something like "self.session.vars['blah']". However, there is no self and methods are defined over classes.

When I create a method now, there seems to be a problem with calling session.vars. For example

def func(subsession: Subsession):
      session.vars['X'] = a

the session will throw an unresolved reference error.

Again, sorry for the silly question, but how do I call a session variable in a method defined for another class?
Message has been deleted

Chris @ oTree

unread,
Aug 10, 2021, 5:02:34 PM8/10/21
to oTree help & discussion
You can get the current session from any other model, eg 

session=subsession.session

see here: https://otree.readthedocs.io/en/latest/conceptual_overview.html#object-hierarchy

By the way see the new SESSION_FIELDS which is a more convenient access to session vars. 

Itamar Bellaiche

unread,
Aug 11, 2021, 7:05:08 AM8/11/21
to oTree help & discussion
Hi Chris and thank you!

Could you maybe please give an example of how to work with SESSION_FIELDS ?

Where in the code do you assign the specific value of the session?

Thanks,

Itamar

Chris @ oTree

unread,
Aug 11, 2021, 10:15:42 AM8/11/21
to Itamar Bellaiche, oTree help & discussion
In the same place where you would normally assign session.vars. See examples here: https://www.otreehub.com/code/

Sent from my phone

On Aug 11, 2021, at 5:05 AM, Itamar Bellaiche <itamar.b...@mail.huji.ac.il> wrote:

Hi Chris and thank you!
--
You received this message because you are subscribed to a topic in the Google Groups "oTree help & discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/otree/g_63XVCouEM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to otree+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/otree/a542c46d-cb6c-42b1-b1ee-4294a5893baan%40googlegroups.com.

Chris @ oTree

unread,
Aug 11, 2021, 10:16:24 AM8/11/21
to Itamar Bellaiche, oTree help & discussion
For example:
session.matrices
session.past_groups

Etc....

Sent from my phone

On Aug 11, 2021, at 8:15 AM, ch...@otree.org wrote:

In the same place where you would normally assign session.vars. See examples here: https://www.otreehub.com/code/
Reply all
Reply to author
Forward
0 new messages