oTree for experiments in social sciences

An Introduction to oTree

Matteo Ploner

Computerized experiments

Advantages of computerized experiments

  • Computerized experiments present several advantages over paper&pencil experiments
    • Live interaction
    • Dynamic interfaces
    • Codified data
    • Wide audience
      • Web-based experiments
  • Specialized softwares are available
    • z-Tree (Fischbacher 2007) is the de-facto standard for lab experiments
    • oTree (Chen, Schonger, and Wickens 2016) is an emerging software
      • Platform-independent, web-based
  • Here, we focus on oTree

About oTree

  • oTree is a framework based on Python to run controlled experiments
    • Games
      • e.g., Public Goods Games (PGG)
    • Individual decision making
      • e.g., Risk elicitation tasks
    • Surveys and tests
      • e.g., Raven test
  • Support by the community
    • Forum
    • Code developed by others
      • e.g. Holzmeister (2017)
  • oTree is open-source,
    • Licensed under an adaptation of the MIT license.
      • Cite it in the paper when you use it


  • Programming language of oTree is Python
    • Popular object-oriented programming language
    • Developed in early 90’s by Guido Van Rossum
  • OTree’s user interface is based on HTML5
    • Supported by modern browser and rich in functionalities
  • OTree is based on Django web application framework
    • OTree applications are web applications
  • All components of oTree are free and open-source


  • The basic setup consists in
    1. An app (experiment) written within oTree
    2. A server computer
    • Cloud server, local PC …
    1. Subjects’ devices with a browser
    • PC, Laptop, Tablet, Mobile Phone …
  • oTree creates a session on the server and generates links for all participants
  • Participants click on the links and are sent to a personal page
    • They submit their answers, which are collected by the server
    • The experimenter can check the progress on the server


oTree app

Conceptual overview

Subsession Subsession
Page Page Page Page Page Page
  • Sessions
    • Participants take part in a series of tasks
  • Subsessions
    • Sections of a session
      • EXAMPLE: a PGG is subsession 1 and a questionnaire is subsession 2
      • Repetitions of the same task are performed over distinct subsessions (periods)
  • Page
    • Sections of a subsession
      • EXAMPLE: the PGG is made of 4 pages (instructions, …) and the questionnaire of 2 pages
  • Groups
    • Each subsession can be divided into groups of players
      • Groups can be shuffled between subsessions

Object hierarchy

  • oTree’s entities are organized with the following hyerarchy
  • Session
    • Subsession
      • Group
        • Player
          • Page
  • A session is a series of subsessions
    • A subsession contains multiple groups
      • A group contains multiple players
        • Each player proceeds through multiple pages

Your first app

  • To create an app named “my_first_app” move to the oTree folder
  • and create the app
  • Move to the folder my_first_app
    • You will find the following files
      • models.py
      • pages.py
      • tests.py
    • And a subfolder
      • templates/my_first_app
        • MyPage.html, Results.html


  • These are the pages that are displayed to participants
    • html files that contain informations and forms
      • forms are used to collect data
    • A default MyPage.html is created
      • {% formfields %} will display the forms of the page
        • see pages.py
      • {% next_button %} will display a button to continue
  • The HTML can contain “fancy” stuff
    • Javascript
    • Bootstrap framework


  • Pages that the participants see are defined in pages.py
    • Logic for how to display the HTML templates
      • when, how, and what to display
  • page_sequence gives the order of pages
    • if there are multiple rounds the sequence is repeated


Basics: Classes

  • Classes are main object-oriented-programming (OOP) in Python
    • Class objects provide default behavior
      • The class statement creates a class object and assigns it a name
      • Assignments inside class statements make class attributes
      • Class attributes export object state and behavior
        • def statements inside class generate a method
    • Instance objects are generated from classes
      • Calling a class object makes a new instance object
      • Each instance object inherits class attributes and gets its own namespace
      • Assignment to self in methods make per-instance attributes
        • self refers to the instance being processed

Classes: An Example

  • Pokemon is a class with some properties
    • Height
    • Weight
    • Category
  • Two instances of the class


Height: 2’ 00";

Weight 18.7 lbs;

Category: Lizard

Thank you


To contact me just write me an email


or write me on the forum of the course


Software required during the course

  • Install the following software on a machine you have access to
    • In Lab 1 the software is installed on each machine
  • All the software is open source and freely downloadable
    • You find below the links to OS WIN installations, installations for other OS are available online
  • oTree

  • Python

  • Without this software you will not be able to follow next lectures

Create an instance of class Pokemon

  • Consider the class Pokemon we illustrated above
    1. Create a new instance of the class
    • Charizard: Height: 5’ 07"; Weight: 199.5 lbs; Category: Flame
    1. Check whether Charizard is taller than Bulbasaur


Ascher, David, and Mark Lutz. 1999. Learning Python. O’Reilly.

Chen, Daniel L, Martin Schonger, and Chris Wickens. 2016. “OTree—an Open-Source Platform for Laboratory, Online, and Field Experiments.” Journal of Behavioral and Experimental Finance 9. Elsevier: 88–97.

Fischbacher, Urs. 2007. “Z-Tree: Zurich Toolbox for Ready-Made Economic Experiments.” Experimental Economics 10 (2). Springer: 171–78.

Holzmeister, Felix. 2017. “OTree: Ready-Made Apps for Risk Preference Elicitation Methods.” Journal of Behavioral and Experimental Finance 16. Elsevier: 33–38.