oTree Forum >

Retrieve session vars (and possibly more) ex post, in desperation mode

#1 by BattPi

I thought I would share a very dirty trick that allowed me to retrieve some experimental data I had in session.vars of an experiment running on Heroku, but NOT transferred in any model (e.g. player) field.*

Notice that when I did this the experiment was over (since two days), all participants were on the "No more pages left" message, and hence modifying the "vars_for_template" of some page (and possibly enabling debugging, or also editing the template) was of no use. What's more, I noticed that if I increased the number of rounds, the already run(ning) session would not actually run them - when participant are said it's over, it's over.

What worked instead was simply to include the following lines into my code (e.g. to __init__.py):

###########################################################################

from otree.views.participant import OutOfRangeNotification, db, Participant

orig_get = OutOfRangeNotification.get

def myget(self, request):
    code = request.path_params['code']
    participant = db.get_or_404(Participant, code=code)
    print(participant.session.vars)
    return orig_get(self, request)

OutOfRangeNotification.get = myget

###########################################################################

I then went to one of the participants' "No more pages left" page (using a link from the management page), and reloaded the page in the browser. The result is that the session vars got printed in the terminal. (Notice that the line that prints the session vars in the code above can be easily changed to do other stuff, including but not limited to printing participant vars)

In my particular case, since the session was on Heroku, I had then to do "heroku logs --tail" and got my variables I could copypaste to some text file. (I then had to spend a few minutes cleaning the data from the log timestamps).

That's it, and I hope you will actually never need this.

Pietro

* The complete story is that the code that transferred the data to the player field raised an error in the session which had never appeared during testing, so I disabled it in real time to chase the error away and continue the session.

#2 by Chris_oTree

Did you know about custom_export?

Also, with the regular data export, session vars get exported as long as they are in SESSION_FIELDS in your settings.

Write a reply

Set forum username