Introductory Workshop on Programming Experiments with oTree

Gregory Chernov 2025

Workshop Overview

  • Duration: 16 Academic Hours
  • No programming experience required
  • Recommended: Basic Python knowledge and oTree tutorials

๐Ÿ•น๏ธ Example Code

๐Ÿงช Demos

๐Ÿ”ง oTree Tools and Projects

๐Ÿ“š Guides and Tutorials

Contents

Programming Experiments Overview

Popular Platforms for Experimentation

Platform Launch Core Components Languages Key Features
oTree 2013 Django, Starlette Python, JS N-players, real-time, custom UI
zTree 1998 Client-server Pascal-like Own simple exp language
Qualtrics 2002 Survey engine, js-friendly No coding Advanced surveys, branching logic, self-hosting
PsychoPy/jsPsych 2003/2012 Python/JS /assets/html/export/libs Python/JavaScript Precise timing, visual stimuli, online/offline experiments
G.Forms 2008 Google Cloud No coding Basic surveys, easy integration

Some others: Lioness; Sofi(PHP), etc.

Core Concepts of Coding Experiments

Any experiment consists of two basic actions:

1. Show something (stimulus) 2. Collect reaction (response)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ STIMULUS โ”‚ โ”‚ RESPONSE โ”‚ โ”‚ Show info โ”‚ --> โ”‚ Collect โ”‚ โ”‚ Display UI โ”‚ โ”‚ reaction โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ–ฒ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ Next iteration

E.G. show kitten image and ask happiness level

Implementation methods:

  • Pen and paper (+engagement, -UI)
  • Web server (stimulus) โ†” Browser (response)
  • oTree = web framework for this cycle

Web Application Architecture

Web Application Architecture

oTree overview (MVC Framework)

  • Models: Core logic and database (Starlette, SQLite/PostgreSQL)
  • Controller: Page logic, function call order, and event sequence (Starlette, called pages in Django).
  • Views/Templates: Front-end structure
    • HTML
    • CSS (Bootstrap)
    • JavaScript

Platform Comparison

Feature oTree zTree Qualtrics jsPsych g.forms
Games โœ“ โœ“ โœ— ! โœ—
N-players โœ“ โœ“ โœ— โœ— โœ—
Online โœ“ ! โœ“ โœ“ โœ“
Code Req. โœ“ โœ“ ! โœ“ โœ—
Custom UI โœ“ ! ! โœ“ โœ—
Free โœ“ ! โœ— โœ“ โœ“
Surveys โœ“ ! โœ“ ! โœ“
Mobile โœ“ โœ— โœ“ โœ“ โœ“
Self-host ! โœ— โœ“ โœ“ โœ“
Active community โœ“ โœ“ โœ— โœ— โœ—
Legend: โœ“ Full ! Limited โœ— None

oTree: Conceptual Overview

oTree Admin Panel

oTree Admin Panel

  • Session Monitor: Real-time participant progress tracking
  • Data: Live data viewing and CSV/Excel export
  • Links: Participant access URLs (demo/study modes)
  • Payments: Payment calculations and reports
  • Sessions: Create and manage experiment sessions

Demo: https://otree-more-demos.herokuapp.com/demo

oTree: webapp (server) and template

About Features & Options
Platform โ€ข Standard web server (like Instagram)
โ€ข Multiplayer strategy games
โ€ข Can integrate what other web can
Development โ€ข Standard: Python + text editor
โ€ข No-code: oTree Studio
โ€ข Rich documentation & demos
Technology โ€ข Python, Django/Starlette backend
โ€ข JS/Bootstrap frontend
โ€ข SQLite/PostgreSQL
Deployment โ€ข Local server for lab
โ€ข Cloud hosting (Heroku)
โ€ข oTree Hub integration

Sample experiment: Guessing Game

Simple game demonstrating core oTree concepts (models, pages, templates)

Repository structure:

otree_workshop/
  โ””โ”€โ”€ guessing_game/            # Example game
      โ”œโ”€โ”€ _static/              # Static files
      โ”œโ”€โ”€ _templates/           # Global templates
      โ”œโ”€โ”€ guessing_app/         # Game application
      โ”‚   โ”œโ”€โ”€ __init__.py      # Game logic
      โ”‚   โ”œโ”€โ”€ Instructions.html # Game pages
      โ”‚   โ”œโ”€โ”€ MyPage.html
      โ”‚   โ””โ”€โ”€ Results.html
      โ””โ”€โ”€ settings.py           # Project settings
  

Code: github.com/gregory-ch/otree_workshop

Guess Game: Description

  • A player has an endowment
  • Random number is generated within certain limits
  • A player has to guess. The closer is the guess to a real number the larger is the profit
  • Two fields:
    • what is guessed
    • a player's decision
  • Three screens:
    • Intro: instructions
    • Decision with instructions
    • Results

Guessing Game: Instructions Page

Guessing Game: Decision Page

Main Game Page

Guessing Game: Results Page:

Results Page

Introduction to the Model-View-Controller (MVC) framework

oTree 3 vs oTree 5 (Lite)

  • oTree 5:
    • Starlette-based (no-self)
    • Faster, simpler (No redis, no second dynos)
    • Better performance

oTree: Object Hierarchy

Session = Event where participants play games/tasks

  • Example: "Lab session with public goods game + questionnaire"
  • Includes payments, configurations, participant management



Session and Subsession Structure

Structure of oTree App

  1. Pages (Display Logic)

    • Page classes (Instructions, Game, Results)
    • page_sequence (order of pages)
    • Templates (HTML files)
  2. Models (Data Processing)

    • Constants: game parameters
    • Player (individual); Group (interaction);
      Subsession (session-level data & methods)
    • Functions (calculation of payoffs
      and other properties based on input)
  3. Configuration

oTree: Initialization Sequence
  1. Session Creation (by Admin)

    • Admin creates session (room/demo/single-use link)
    • Session configs applied
    • Session variables initialized
    • Participants slots created
    • creating_session() runs:
      • Can initialize participant.vars
      • Can set participant.label
      • Can set initial participant.payoff
    • Subsessions, Groups, Players created
  2. Waiting for Participants

    • Links generated and distributed
    • Participants slots with pre-initialized vars wait
  1. Participant Joins

    • Participant opens link
    • Gets assigned to pre-created slot
    • Accesses their pre-initialized variables
    • Starts first page in page_sequence
  2. Page Flow

    • Pages execute in order from page_sequence

    • For each page:

      • BEFORE page is shown:
      is_displayed() # (should return 
        # True if page is to be shown)
        vars_for_template()
        
      • AFTER page is shown:
      before_next_page()
        app_after_this_page()
        

oTree: Models Object Hierarchy

Session
      โ””โ”€โ”€ Subsession
          โ””โ”€โ”€ Group
              โ””โ”€โ”€ Player
  

Access pattern (bottom-up):

player.participant  # Access participant from player
  player.group       # Access group from player
  player.subsession  # Access subsession from player
  group.subsession   # Access subsession from group
  

oTree: Players vs Participants

Session and Subsession Structure

Participant = Person playing the experiment - Stays constant throughout session - Has unique participant code - Tracks progress through all games

Player = Role in specific subsession

  • New player instance for each subsession
  • Can have different roles/groups
  • Stores game-specific data