oTree Forum >

Passing data across apps

#1 by mona2876

Hello all,

I have a general question about passing data between apps that I was hoping someone can help me with. In app 1, I ask participants about their income and race. Later on, in app 3, I will conduct a longer survey and some of the questions will only be asked based on their answer from app 1, something like this for instance: 

 def get_form_fields(player):
        if participant.income== 'Middle Class':
         return ['partyid_imp']
          return ['partyid_imp', 'zipcode']

My question is, what is the best approach to define variables if you want to use survey information across apps? I have tried to follow the link below, but I still can't figure out how I need to define my income variable (for instance) in app 1 in order to use it again in app 3 etc. The error message I get is either "name 'participant' is not defined" or "'Player' object has no attribute 'vars'" if I try to grab it using player object. Any help would be much appreciated. 

Thank you!! 


#2 by ccrabbe

Hi mona2876 -

I can help explain the probable reasons behind the error messages you mention:

* name 'participant' is not defined
This would likely result from the code you posted.  get_form_fields takes a Player object as an argument, but you are referencing participant.income - where is that Participant object coming from?  You need to get the Participant associated with the passed Player by using player.participant

* 'Player' object has no attribute 'vars'
This is a similar error - you're trying to access a Participant field ('vars') from a variable which is a Player object (which doesn't have a 'vars' field).  If your player object is called p, then you can access its associated Participant object with p.participant.  So you'd say p.participant.vars instead of p.vars.

For how to pass the data, the simplest way is to store the data in participant.vars in the first app.  I usually do this in the before_next_page function of the Page I collect the data on:

def before_next_page(player: Player):
  player.participant.vars['income'] = player.income
And then in the later app, you can access player.participant.vars anywhere that you have a player object.

Note:  I tend to manage oTree servers with a couple dozen apps installed, so using PARTICIPANT_FIELDS is less convenient for me than just using participant.vars directly - PARTICIPANT_FIELDS would bloat up pretty quickly, and I'd have to be careful to always pick a unique variable name for new apps.  So you can use PARTICIPANT_FIELDS instead of participant.vars if you find it useful.


Write a reply

Set forum username