Simple generic experiment template using FastAPI, SQLModel, and jspsych.
- Clone or download this repo.
- Set up a Python environment:
python -m venv env
- Activate that environment:
source env/bin/activate
- Install required packages:
pip install -r requirements.txt
- Initialize/reset the local database:
python cli.py reset_db
- Install frontend packages:
python cli.py install_packages
OR from withinfrontend/
:npm i
This should be done locally in a .env
file. If using a deployment on Railway, can be done online via their GUI.
Note that you will need to change the Settings
object within config.py
to look for those variables of interest, and then point to those within ExperimentConfiguration
in main.py
.
- In one terminal, start the frontend server:
python cli.py debug
OR from withinfrontend/
:npm run dev
- In another, separate terminal, start the backend server:
python cli.py run
- Once both terminals are running, you can see the experiment locally at: http://localhost:8000/exp?workerId=XXX&assignmentId=XXX&hitId=XXX
- It won't work without the query parameters specifying
workerId
and so on. This is for your convenience when you run the experiment, so you can automatically get that information from participants without their needing to enter it. - You can always modify the frontend to make these parameters optional! I prefer when they are mandatory, so I don't forget to make them mandatory once again at deploy time (though of course in future iterations I may supply dummy credentials while in development mode, if I get around to it).
- It won't work without the query parameters specifying
- Create a Railway account: https://railway.app
- From the Railway website, select your repo for deployment.
- In the app's settings, you will need to select the appropriate branch (master/main would be the default)
- Attach a Postgres database instance
- Set up your environment variables (including a reference to the Postgres instance at
DATABASE_URL
)
- Install the Railway CLI if you don't already have it:
npm i -g @railway/cli
railway login
(login to the service)railway link
(choose the relevant project)railway shell
(gives you access to the environment variables you set on Railway when performingrailway run
commands)- Reset the database online:
railway run python cli.py reset_db
railway run python cli.py export
- The current setup has a one-to-one relationship between participants and data. However, if a participant should run the experiment multiple times, their data will still be saved -- it just means that the participant table will only be associated with the latest version of the data. This is a design choice that can be changed by modifying the
Participant
andData
models inmodels.py
.