I would like to incorporate a question in Otree that might or might not be asked depending on a previous question. Here is a very simple example:
Question 1: What is your main occupation: A. Work. B. Student. C. Unemployed
Question 2 (ONLY ASKED IF the answer to "Question 1" is "A. Work"): what industry do you work on? A. Transportation B. Mining C. Other
I have managed to do this when Question 1 and Question 2 are on different pages (see code below). However, I would like to have questions 1 and 2 on the same page. Any insights on how I can do this? (I am a beginner using otree/javascript)
from otree.api import *
doc = """
'other' option
"""
class C(BaseConstants):
NAME_IN_URL = 'option_other'
PLAYERS_PER_GROUP = None
NUM_ROUNDS = 1
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
pass
class Player(BasePlayer):
occupation = models.StringField(label='main occupation?',choices=['Work', 'Student', 'Unemployment'])
industry = models.StringField(label='what industry do you work on?', choices=['transportation','mining','others'])
# PAGES
class MyPage(Page):
form_model = 'player'
form_fields = ['occupation']
class MyPage2(Page):
@staticmethod
def is_displayed(player: Player):
return player.occupation == 'Work'
form_model = 'player'
form_fields = ['industry']
page_sequence = [MyPage, MyPage2]