PsyNet's software stack
***********************

PsyNet is a Python package built on established open-source tools. You
rarely use these tools directly, but knowing what each one does helps
when you read error messages, logs or other people's experiment code.


Dallinger
=========

PsyNet is built on Dallinger, a framework for running networked
experiments online. Dallinger provides the experiment server, the
database tables for participants, networks, nodes and infos,
integrations with recruitment services such as Prolific, the
experiment dashboard, and the commands that deploy an experiment to a
server. PsyNet adds the timeline, pages, trial makers, assets, bots
and the "psynet" command line on top; "psynet" commands call
Dallinger's where needed.


The web server
==============

* Flask is the web framework: each URL an experiment serves, from
  participant pages to custom routes, is a Flask route.

* Gunicorn runs the Flask app with gevent workers, so one server
  process can serve many participants at once, including over
  WebSockets (Flask-Sock).

  While one request waits on the database, the worker serves other
  requests, and background jobs behave the same way. Module-level
  variables in experiment code can therefore change during any
  database call. Keep per-participant state in the database rather
  than in module-level caches or counters. PostgreSQL "COPY" cannot
  run this way, so wrap it in "psynet.db.blocking_psycopg()".

* Jinja renders the page templates, and dominate builds HTML from
  Python code, for example in prompts.


Data
====

* PostgreSQL stores all experiment data. Each deployed experiment has
  its own database.

* SQLAlchemy maps Python classes such as participants, trials and
  nodes to database tables; see Querying and storing database objects.

* Redis holds short-lived shared state and message queues. RQ uses it
  to run background jobs, such as asynchronous code blocks, in worker
  processes, and APScheduler runs scheduled tasks in a separate clock
  process.

* pandas builds the CSV files in an export.


Participant pages
=================

* Bootstrap provides the page layout and controls, with jQuery for
  older page scripts.

* SurveyJS renders questionnaires built with "SurveyJSControl".

* Tone.js synthesizes sounds in the browser for "JSSynth".

* jsPsych timelines can run inside PsyNet pages.

* gettext catalogs, managed with Babel, hold translations; see
  Internationalization.


Deployment
==========

* Docker packages the experiment and its dependencies into an image,
  and runs PostgreSQL and Redis on your computer during development.

* On a server with classic ingress, Caddy routes each experiment's web
  address to its containers and obtains HTTPS certificates, and Dozzle
  shows the containers' logs in the browser.

* Amazon Web Services can host the server (EC2) and large stimulus
  files (S3); PsyNet talks to AWS with boto3.

How deployment works describes how these fit together.


Development tools
=================

* uv installs Python and the experiment's packages from
  "constraints.txt".

* Click implements the "psynet" command line.

* pytest runs the bot tests behind "psynet test local", and Playwright
  drives a real browser for layout and participant-flow tests.

* Sphinx builds this documentation.
