Skip to content

Commit 7011a54

Browse files
authored
React frontend (facebookresearch#1281)
* Loading react code in the worker frontend * first working implementation for react frontend * feature parity * Abstracting socket handler out from the main app * Fixing mturk submit, infra for custom views * Add demo task for react server * Simplify package logic * bugfixes pre PR * needing package for legacy * Manifest and copyrights * addressing comments * fixing test cases to refer to legacy mock
1 parent fd21119 commit 7011a54

35 files changed

+9315
-46
lines changed

MANIFEST.in

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
include parlai/mturk/core/server/html/*
2-
include parlai/mturk/core/server/server.js
3-
include parlai/mturk/core/server/package.json
1+
include parlai/mturk/core/server_legacy/html/*
2+
include parlai/mturk/core/server_legacy/server.js
3+
include parlai/mturk/core/server_legacy/package.json
4+
include parlai/mturk/core/react_server/*

parlai/mturk/core/agents.py

+12
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,15 @@ def shutdown(self, timeout=None, direct_submit=False):
648648
self.db_logger.log_submit_assignment(
649649
self.worker_id, self.assignment_id)
650650
return did_complete
651+
652+
def update_agent_id(self, agent_id):
653+
"""Workaround used to force an update to an agent_id on the front-end
654+
to render the correct react components for onboarding and waiting
655+
worlds. Only really used in special circumstances where different
656+
agents need different onboarding worlds.
657+
"""
658+
self.mturk_manager.worker_manager.change_agent_conversation(
659+
agent=self,
660+
conversation_id=self.conversation_id,
661+
new_agent_id=agent_id,
662+
)

parlai/mturk/core/mturk_data_handler.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ def log_worker_note(self, worker_id, assignment_id, note):
545545
except Exception as e:
546546
print(repr(e))
547547

548-
def get_all_worker_data(self, start=0, count=30):
548+
def get_all_worker_data(self, start=0, count=100):
549549
"""get all the worker data for all worker_ids."""
550550
with self.table_access_condition:
551551
conn = self._get_connection()
@@ -600,7 +600,7 @@ def get_worker_assignment_pairing(self, worker_id, assignment_id):
600600
results = c.fetchone()
601601
return results
602602

603-
def get_all_run_data(self, start=0, count=30):
603+
def get_all_run_data(self, start=0, count=100):
604604
"""get all the run data for all task_group_ids."""
605605
with self.table_access_condition:
606606
conn = self._get_connection()

parlai/mturk/core/mturk_manager.py

+74-33
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,68 @@ def is_task_world(self, conversation_id):
764764

765765
# Manager Lifecycle Functions #
766766

767+
def populate_legacy_task_files(self, task_directory_path):
768+
# Poplulate files to copy over to the server
769+
if not self.task_files_to_copy:
770+
self.task_files_to_copy = []
771+
if not task_directory_path:
772+
task_directory_path = os.path.join(
773+
self.opt['parlai_home'],
774+
'parlai',
775+
'mturk',
776+
'tasks',
777+
self.opt['task']
778+
)
779+
self.task_files_to_copy.append(
780+
os.path.join(task_directory_path, 'html', 'cover_page.html'))
781+
try:
782+
for file_name in os.listdir(os.path.join(
783+
task_directory_path, 'html')):
784+
self.task_files_to_copy.append(os.path.join(
785+
task_directory_path, 'html', file_name
786+
))
787+
except FileNotFoundError: # noqa F821 we don't support python2
788+
# No html dir exists
789+
pass
790+
for mturk_agent_id in self.mturk_agent_ids + ['onboarding']:
791+
self.task_files_to_copy.append(os.path.join(
792+
task_directory_path,
793+
'html',
794+
'{}_index.html'.format(mturk_agent_id)
795+
))
796+
797+
def populate_task_files(self, task_directory_path):
798+
# Poplulate files to copy over to the server
799+
if not self.task_files_to_copy:
800+
self.task_files_to_copy = {
801+
'static': [],
802+
'components': [],
803+
'css': [],
804+
}
805+
if not task_directory_path:
806+
task_directory_path = os.path.join(
807+
self.opt['parlai_home'],
808+
'parlai',
809+
'mturk',
810+
'tasks',
811+
self.opt['task']
812+
)
813+
self.task_files_to_copy['static'].append(os.path.join(
814+
task_directory_path, 'frontend', 'static', 'cover_page.html'))
815+
try:
816+
frontend_contents = os.listdir(
817+
os.path.join(task_directory_path, 'frontend'))
818+
for dir in frontend_contents:
819+
if dir in self.task_files_to_copy:
820+
for file_name in os.listdir(os.path.join(
821+
task_directory_path, 'frontend', dir)):
822+
self.task_files_to_copy[dir].append(os.path.join(
823+
task_directory_path, 'frontend', dir, file_name
824+
))
825+
except FileNotFoundError: # noqa F821 we don't support python2
826+
# No frontend dir exists
827+
pass
828+
767829
def setup_server(self, task_directory_path=None):
768830
"""Prepare the MTurk server for the new HIT we would like to submit"""
769831
assert self.task_state >= self.STATE_CREATED
@@ -884,34 +946,6 @@ def setup_server(self, task_directory_path=None):
884946
unique_worker=self.is_unique,
885947
is_sandbox=self.opt['is_sandbox']
886948
)
887-
# Poplulate files to copy over to the server
888-
if not self.task_files_to_copy:
889-
self.task_files_to_copy = []
890-
if not task_directory_path:
891-
task_directory_path = os.path.join(
892-
self.opt['parlai_home'],
893-
'parlai',
894-
'mturk',
895-
'tasks',
896-
self.opt['task']
897-
)
898-
self.task_files_to_copy.append(
899-
os.path.join(task_directory_path, 'html', 'cover_page.html'))
900-
try:
901-
for file_name in os.listdir(os.path.join(
902-
task_directory_path, 'html')):
903-
self.task_files_to_copy.append(os.path.join(
904-
task_directory_path, 'html', file_name
905-
))
906-
except FileNotFoundError: # noqa F821 we don't support python2
907-
# No html dir exists
908-
pass
909-
for mturk_agent_id in self.mturk_agent_ids + ['onboarding']:
910-
self.task_files_to_copy.append(os.path.join(
911-
task_directory_path,
912-
'html',
913-
'{}_index.html'.format(mturk_agent_id)
914-
))
915949

916950
# Setup the server with a likely-unique app-name
917951
task_name = '{}-{}'.format(str(uuid.uuid4())[:8], self.opt['task'])
@@ -921,11 +955,18 @@ def setup_server(self, task_directory_path=None):
921955
heroku_team = self.opt['heroku_team']
922956
else:
923957
heroku_team = None
924-
self.server_url = server_utils.setup_server(self.server_task_name,
925-
self.task_files_to_copy,
926-
self.opt['local'],
927-
heroku_team,
928-
self.opt['hobby'])
958+
959+
if self.opt.get('frontend_version', 0) < 1:
960+
self.populate_legacy_task_files(task_directory_path)
961+
self.server_url = server_utils.setup_legacy_server(
962+
self.server_task_name, self.task_files_to_copy,
963+
self.opt['local'], heroku_team, self.opt['hobby'])
964+
else:
965+
self.populate_task_files(task_directory_path)
966+
self.server_url = server_utils.setup_server(
967+
self.server_task_name, self.task_files_to_copy,
968+
self.opt['local'], heroku_team, self.opt['hobby'])
969+
929970
shared_utils.print_and_log(logging.INFO, self.server_url)
930971

931972
shared_utils.print_and_log(logging.INFO, "MTurk server setup done.\n",
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@babel/env", "@babel/preset-react"],
3+
"plugins": ["@babel/plugin-proposal-class-properties"]
4+
}

0 commit comments

Comments
 (0)