Package: otreechat
Chat rooms for oTree so that participants can communicate with each other.Live demo here(open the start links and click through to the “Your Choice” page).This is a beta version and subject to changes/improvements!Installation(Requires otree-core >= 1.2)pip3 install -U otree-core
pip3 install -U otreechatInsettings.py, add'otreechat'toINSTALLED_APPS,
e.g.INSTALLED_APPS = ['otree', 'otreechat']Then runotree resetdb.For installation on your server, yourrequirements_base.txtshould
containotreechatas well asotree-core>=1.2.0(or higher, etc).UsageBasic usageAdd{% load otreechat %}to the top of your template, e.g.:{%loadstaticfilesotree_tags%}{%loadotreechat%}Then wherever you want a chatbox in the template, use:{%chat%}This will make a chat room among players in the same Group,
where each player’s nickname is displayed as
“Player 1”, “Player 2”, etc. (based on the player’sid_in_group).Customizing the nickname and chat room membersYou can pass optional parameterschanneland/ornicknamelike this:{%chatnickname="abc"channel="123"%}Nicknamenicknameis the nickname that will be displayed for that user in the chat.
A typical usage would be{% chat nickname=player.role %}.Channelchannelis the chat room’s ID, meaning that if 2 players
have the samechannel, they can chat with each other.channelis not displayed in the user interface; it’s just used internally.
Its default value isgroup.id, meaning all players in the group can chat together.
You can usechannelto instead scope the chat to the current page
or sub-division of a group, etc. (see examples below).
Regardless of the value of thechannelargument,
the chat will at least be scoped to players in the same session and the same app.Example: chat by roleHere’s an example where instead of communication within a group,
we have communication between groups based on role,
e.g. all buyers can talk with each other,
and all sellers can talk with each other.classPlayer(BasePlayer):defrole(self):ifself.id_in_group==1:return'Seller'else:return'Buyer'defchat_nickname(self):return'Group{}{}'.format(self.group.id_in_subsession,self.role())Then in the template:{%chatnickname=player.chat_nicknamechannel=player.role%}Example: chat across roundsIf you need players to chat with players who are currently in a different round
of the game, you can do:{%chatchannel=group.id_in_subsession%}Example: chat between all groups in all roundsIf you want everyone in the session to freely chat with each other, just do:{%chatchannel=1%}(The number 1 is not significant; all that matters is that it’s the same for everyone.)StylingNoteThe CSS classes for the elements changed in August 2017.
They were renamed to a more consistent BEM style.To customize the style, just include some CSS after the{% chat %}element,
e.g.:{%chat%}<style>.otree-chat__messages{height:400px;}.otree-chat__nickname{color:#0000FF;font-weight:bold;}</style>You can also customize the appearance by putting it inside a<div>and styling that parent<div>. For example, to set the width:<divstyle="width: 400px">{%chat%}</div>Multiple chats on a pageYou can have multiple{% chat %}boxes on each page,
so that a player can be in multiple channels simultaneously.For example, this code enables 1:1 chat with every other player in the group.classPlayer(BasePlayer):defchat_nickname(self):return'Player{}'.format(self.id_in_group)defchat_configs(self):configs=[]forotherinself.get_others_in_group():ifother.id_in_group<self.id_in_group:lower_id,higher_id=other.id_in_group,self.id_in_groupelse:lower_id,higher_id=self.id_in_group,other.id_in_groupconfigs.append({# make a name for the channel that is the same for all# channel members. That's why we order it (lower, higher)'channel':'{}-{}-{}'.format(self.group.id,lower_id,higher_id),'label':'Chat with{}'.format(other.chat_nickname())})returnconfigs{%forconfiginplayer.chat_configs%}<h4>{{config.label}}</h4>{%chatnickname=player.chat_nicknamechannel=config.channel%}{%endfor%}Exporting CSV of chat logsThe chat logs download link will appear on oTree’s regular data export page.Upgradingpip install -U otreechatFeedbackPlease send any feedback/opinions tochris@otree.org,
for example to suggest an improvement to the widget’s appearance.