Skip to main content

oTree chat.

Project description

Chat rooms for oTree so that participants can communicate with each other.

Installation

pip install -U otreechat

In your project root, next to settings.py, create a file routing.py containing this:

from otree.channels.routing import channel_routing
import otreechat.routing
channel_routing += otreechat.routing.channel_routing

In settings.py:

  • Set CHANNEL_ROUTING = 'routing.channel_routing' (this is the dotted path to your channel_routing variable in routing.py).

  • Add 'otreechat' to INSTALLED_APPS, e.g. INSTALLED_APPS = ['otree', 'otreechat']

Then run otree resetdb.

Usage

Add this to the top of your template:

{% load otreechat %}

Then wherever you want a chatbox in the template, use:

{% chat %}

You can pass optional parameters room and/or nickname like this:

{% chat nickname=mynickname room=myroom %}
  • nickname is the nickname that will be displayed for that user in the chat. If omitted, the nickname is Player 1, Player 2, etc. (based on the player’s id_in_group).

  • room is the chat room’s ID or “channel”, meaning that if 2 players have the same room, they can chat with each other. room is not displayed in the user interface; it’s just used internally. If omitted, the room is scoped to the group.

Here’s an example implementation:

class Player(BasePlayer):

    def chat_nickname(self):
        return 'Villain {}'.format(self.id_in_group)

    def chat_room(self):
        '''
        Scope the room to the current session and current group,
        and then can further subdivide into odd/even players
        '''
        return '{}-{}-{}'.format(Constants.name_in_url, self.group.pk, self.id_in_group % 2)

Then in the template:

{% chat nickname=player.chat_nickname room=player.chat_room %}

Styling

To customize the style, just include some CSS after the {% chat %} element, e.g.:

{% chat %}

<style>
    #otreechat .messages {
        height: 400px;
    }
    #otreechat .nickname {
        color: #0000FF;
        font-weight: bold;
    }
</style>

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

otreechat-0.1.1.tar.gz (5.4 kB view hashes)

Uploaded Source

Supported by

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