Skip to main content

A server to host your online studies on.

Project description

PsyServer

A server to host your online studies on.

License: MIT PyPI Code style: black

PsyServer is a simple, easy to setup server on which to host online studies safely and conveniently. It does not require you to commit to a certain framework, and eliminate the need to put excessive time into setting up backends and servers.

Features

PsyServer uses Filebrowser to enable easy and safe access to the study code, and study data:

Server data root

Every study placed into the studies folder becomes accessible from the internet.

Experiment in Filebrowser Experiment available online

Data from the study is saved in the studydata folder, which is only accessible via the filebrowser and an account.

Both the studies and studydata folder can easily modified and downloaded from the online interface, eliminating the need for putty or ssh.

Filebrowser allows for user management, making collaboration with others easy and convenient.

Setup

To setup a fully functional server to host studies on, you will require following things:

  1. A host machine to run the server on
  2. Installing PsyServer
  3. Get a domain (optional)
  4. Setup HTTPS (optional)
  5. Run the Server

Host machine

A host machine to run the server on will be required. It is highly recommended it is a linux machine. For example, you can set up an aws ec2 instance for little cost.

PsyServer Setup

PsyServer itself comes as a python package and is installed as such. At least python 3.11 is required; it is recommended to use a virtual environment. Here, we use conda (miniconda installation guide). For easy access of files, you need to install [filebrowser] in addition.

# 1. create psyserver folder
mkdir psyserver
cd psyserver

# 2. set up conda environment (optional)
conda create -n psyserver python=3.11
conda activate psyserver

# 3. install package
pip install psyserver

# 4. install filebrowser
curl -fsSL https://raw.githubusercontent.com/filebrowser/get/master/get.sh | bash

# 5. create example config/structure.
psyserver init

# 6. configure server (optional)
# open psyserver.toml with your editor of choice, e.g. vim
vim psyserver.toml

# 7. run server
psyserver run

Running setup

Although you could just run psyserver as background job in your console, that would come at the disadvantage that when the server crashes or restarts, psyserver will not brestart automatically. Thus it is recommended to setup PsyServer as systemd service.

The psyserver init will create the unitfile psyserver.service in your directory. You need to place that unitfile into a systemd subdirectory:

$ sudo mv psyserver.service /etc/systemd/system/
$ sudo chown root:root /etc/systemd/system/psyserver.service
$ sudo chmod 644 /etc/systemd/system/psyserver.service

Now reload systemctl.

$ sudo systemctl daemon-reload

Start the service.

$ sudo systemctl start psyserver

Check on the service (leave with Q).

$ sudo systemctl status psyserver
● psyserver.service - PsyServer Service.
     Loaded: loaded (/home/ubuntu/.config/systemd/user/psyserver.service; disabled; vendor preset: enabled)
     Active: active (running)

Enable the server for autostart.

$ sudo systemctl enable psyserver

For stopping and disabling use:

$ sudo systemctl stop psyserver
$ sudo systemctl disable psyserver

Domain Name

Using a domain name is recommended, but operation is possible without. Domains can be acquired at websites such as namecheap. You will require a domain if you want to use https.

https

https ensure safe communication between participants and your server. It is highly recommend to set up https. Setting up https yourself can be quite tricky, it is thus recommended to use Caddy as Reverse Proxy. A reverse proxy is another application running on your machine which handles the connections from the internet to a server, in this case PsyServer.

  1. Install Caddy
  2. Setup up the Caddy Service
  3. Setup Caddy as reverse Proxy

Use this Caddyfile with the default configuration of PsyServer.

example.com
{
  reverse_proxy :5000
}

admin.example.com
{
  reverse_proxy :5050
}

Replace example.com with your domain.

Reload Caddy sudo systemctl reload caddy.

That's it!

You can also setup uvicorn directly to handle https (uvicorn https documentation). Settings are set in psyserver.toml under [uvicorn] with exactly the same keys as the documentation, without the --.

Configuration

psyserver.toml configures the server. This file has to be in the directory from which psyserver run is called.

The configuration has two groups:

  1. psyserver: all PsyServer configuratons
  2. uvicorn: Uvicorn configurations

psyserver config

[psyserver]
studies_dir = "studies"
data_dir = "data"
redirect_url = "https://www.example.com"
h_captcha_verify_url = "https://api.hcaptcha.com/siteverify"
h_captcha_secret = <YOUR-SECRET>
  • studies_dir: path to directory which contains studies. Any directory inside will be reachable via the url. E.g. a study in <studies_dir>/exp_cute/index.html will have the url <host>:<port>/exp_cute/index.html.
  • data_dir: directory in which study data is saved. E.g. data submissions to the url <host>:<port>/exp_cute/save will be saved in <data_dir>/exp_cute/. Has to be different from studies_dir.
  • redirect_url: Visitors will be redirected to this url when accessing routes that are not found. Without this key, a 404 - Not found html will be displayed.
  • h_captcha_verify_url: URL to the hcaptcha verification server, if different from default.
  • h_captcha_secret: Hcaptcha secret found in the settings/secrets section of your profile.

uvicorn config

[uvicorn]
host = "127.0.0.1"
port = 5000

Here configures the uvicorn instance runnning the server. For example, uou can specify the host, port and https configurations. For all possible options, use the commands in the uvicorn settings documentation without --.

How to save data to psyserver

To save participant data to the server it has to be sent in the json format of a POST request. The POST request can be made to /<study>/save which saves data as a json file. Upon succesful saving, a json object {success: true} is returned. Provide the participantID key such that the saved data will be named with the participantID.

// example data
participant_data = {
  participantID: "debug_1",
  condition: "1",
  experiment1: [2, 59, 121, 256],
  // ...
};
// post data to server
$.ajax({
  url: "/exp_cute/save",
  type: "POST",
  data: JSON.stringify(participant_data),
  contentType: "application/json",
  success: () => {
    console.log("Saving successful");
    // success function
  },
}).fail(() => {
  console.log("ERROR with $.post()");
});

Note that you need to call JSON.stringify on your data. Without this, you will get an unprocessable entity error.

Development

Setup

# 1. set up conda environment
$ conda create -n psyserver python=3.11
$ conda activate psyserver

# 2. clone
$ git clone git@github.com:GabrielKP/psyserver.git
$ cd psyserver

# 3. install in editor mode
$ pip install -e .[dev]

# 4. install & run pre-commit
pre-commit install
pre-commit run --all-files

Testing

# normal test
$ pytest . -v

# coverage
$ coverage run -m pytest
# html report
$ coverage html

Publishing

$ flit publish

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psyserver-0.7.1.tar.gz (13.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psyserver-0.7.1-py3-none-any.whl (12.9 MB view details)

Uploaded Python 3

File details

Details for the file psyserver-0.7.1.tar.gz.

File metadata

  • Download URL: psyserver-0.7.1.tar.gz
  • Upload date:
  • Size: 13.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for psyserver-0.7.1.tar.gz
Algorithm Hash digest
SHA256 4477ad3e650003e81fe38ef8231ee2e607edc8daf59e7949cef0573f5146fdb9
MD5 d855ea8f0ff9249b507a5b1e5429447b
BLAKE2b-256 72ebe21a870c9c99eb1bea926857dfe939cf02d975ad5a4134feec98fc30d24d

See more details on using hashes here.

File details

Details for the file psyserver-0.7.1-py3-none-any.whl.

File metadata

  • Download URL: psyserver-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 12.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.3

File hashes

Hashes for psyserver-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2d66d4361af9b718ef00575e034b921fe6995745ec92d3109f66dadb29d2eb1
MD5 c88d7571d64755c4b0a3e4af270bc240
BLAKE2b-256 5640b26f7cb71e210ed07fef7a3ad8f5dc3c8415b931bde9ef173a6e92818939

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page