from otree.api import * import pandas as pd import numpy as np doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'staircase' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 class Subsession(BaseSubsession): def df(self): return pd.DataFrame() def date(delf): return str() class Group(BaseGroup): pass class Player(BasePlayer): csv_input = models.LongStringField( label="請上傳您的 CSV 檔案:" ) user_id = models.StringField( label="請輸入您的學號(英文字母請輸入小寫):" ) switching_point_1 = models.StringField() switching_point_2 = models.StringField() # PAGES class Login(Page): form_model = 'player' form_fields = ['user_id'] # @staticmethod # def error_message(player, values): # if values['user_id'] != 'admin': # if Subsession.df.empty: # return '請稍後再試!' # elif values['user_id'] not in Subsession.df['user_id'].values: # return '請輸入正確的學號!' @staticmethod def before_next_page(player, timeout_happened): player.participant.user_id = player.user_id class Admin_input(Page): form_model = 'player' form_fields = ['csv_input'] @staticmethod def before_next_page(player, timeout_happened): # 輸入 CSV 檔案 csv_data = player.csv_input.split('\r\n') header = csv_data[0].split(',') # 假设标题在 CSV 数据的第一行 data_rows = [row.split(',') for row in csv_data[1:]] # 数据从第二行开始 Subsession.df = pd.DataFrame(data_rows, columns=header) # 使用指定的标题创建 DataFrame @staticmethod def is_displayed(player): return (player.participant.user_id == 'admin') class Instruction(Page): pass class Switch_1(Page): form_model = 'player' form_fields = ['switching_point_1'] class Switch_2(Page): form_model = 'player' form_fields = ['switching_point_2'] class End(Page): pass page_sequence = [Login, Admin_input, Instruction, Switch_1, Switch_2, End]