Strumenti Utente

Strumenti Sito


otree_mytips

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.

Link a questa pagina di confronto

Entrambe le parti precedenti la revisioneRevisione precedente
Prossima revisione
Revisione precedente
otree_mytips [2020/09/10 11:53] – titles pietrootree_mytips [2020/09/12 09:49] (versione attuale) – randomize order pietro
Linea 18: Linea 18:
  
 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. 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 =====
 +
 +<code>
 +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()}
 +</code>
 +
 +... and inside the template:
 +
 +<code>
 +<img width="100%" src="data:image/png;base64,{{ my_img }}">
 +</code>
 +
 +===== 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'':
 +
 +<code>
 +[...]
 +
 +{% block content %}
 +{% if swap %}
 +    {% block a_1 %}{% endblock %}
 +    {% block b_1 %}{% endblock %}
 +{% else %}
 +    {% block a_2 %}{% endblock %}
 +    {% block b_2 %}{% endblock %}
 +{% endif %}
 +</code>
 +
 +''mypage.html'':
 +
 +<code>
 +{% 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 %}
 +</code>
otree_mytips.1599731587.txt.gz · Ultima modifica: 2020/09/10 11:53 da pietro