====== My tips with oTree ======
===== Override oTree localization =====
**Problem:** I have a session in few days, and my language's localization is not complete. I have already [[https://github.com/oTree-org/otree-core/pull/540|contributed]] new strings, but too late for them being included in time in a new release.
**Solution:** [[https://docs.djangoproject.com/en/3.1/topics/i18n/translation/#how-django-discovers-translations|it is easy to provide additional localization strings in Django]]. Just follow these steps:
- Inside your project root, create a ''locale/LANG/LC_MESSAGES'' folder, replacing ''LANG'' with your language code (e.g. ''it'' for Italian).
- Download the [[https://github.com/oTree-org/otree-core/tree/master/otree/locale|strings file]] (''django.po'') in the desired language to that folder. If your language is not supported, download another one.
- Edit the file with [[https://poedit.net|poedit]] or with a simple text editor. Change/update everything applicable.
- Compile it to a ''.mo'' file (this can be done from inside poedit with ''File'' -> ''Compile to MO''), saved in the same folder.
- Inside your ''settings.py'', add a line with the following content:
''LOCALE_PATHS = ['locale']''
That's it! The strings will be shown next time you start oTree - including in Heroku (remember to ''git add'' and ''git commit'' the ''django.mo'' file).
===== Using labels in rooms =====
When a room is set up for participant labels, I should _not_ distribute the auto-generated links available under ''Participant-specific URLs'' in the session page. These will result in ''player.participant.label'' being ''None'', and in new participants using the provided labels to get an error message when the room is full.
===== Including a figure made with matplotlib =====
from matplotlib import pyplot as plt
from io import BytesIO
from base64 import b64encode
import urllib
class MyPage(Page):
def create_figure(self):
plt.figure(figsize=(4, 3))
plt.plot(range(3), range(3), color='blue', marker='d', markersize=10)
fig = plt.gcf()
buf = BytesIO()
fig.savefig(buf, format='png', bbox_inches='tight')
buf.seek(0)
string = b64encode(buf.read())
return urllib.parse.quote(string)
def vars_for_template(self):
return {'my_img' : self.create_figure()}
... and inside the template:
===== Randomize order of two activities inside a same page =====
Create a Boolean variable ''swap'' that determines whether to reorder, pass it with ''vars_for_template''. Then:
''mypage_base.html'':
[...]
{% block content %}
{% if swap %}
{% block a_1 %}{% endblock %}
{% block b_1 %}{% endblock %}
{% else %}
{% block a_2 %}{% endblock %}
{% block b_2 %}{% endblock %}
{% endif %}
''mypage.html'':
{% extends "appname/mypage_base.html" %}
{% load otree %}
{% block a_1 %}{% block a_2 %}
This is block a.
{% endblock %}{% endblock %}
{% block b_1 %}{% block b_2 %}
This is block b.
{% endblock %}{% endblock %}