main.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. from __future__ import print_function, absolute_import
  4. import atexit
  5. import json
  6. import shutil
  7. import subprocess
  8. import sys
  9. import tempfile
  10. from multiprocessing.pool import ThreadPool
  11. from tornado import ioloop
  12. from notebook.notebookapp import NotebookApp
  13. from traitlets import Bool, Unicode
  14. root_dir = tempfile.mkdtemp(prefix='mock_contents')
  15. atexit.register(lambda: shutil.rmtree(root_dir, True))
  16. def run_task(func, args=(), kwds={}):
  17. """Run a task in a thread and exit with the return code."""
  18. loop = ioloop.IOLoop.instance()
  19. worker = ThreadPool(1)
  20. def callback(result):
  21. loop.add_callback(lambda: sys.exit(result))
  22. def start():
  23. worker.apply_async(func, args, kwds, callback)
  24. loop.call_later(1, start)
  25. class TestApp(NotebookApp):
  26. open_browser = Bool(False)
  27. notebook_dir = Unicode(root_dir)
  28. def start(self):
  29. run_task(run_node, args=(self.connection_url, self.token))
  30. super(TestApp, self).start()
  31. def run_node(base_url, token):
  32. # Run the node script with command arguments.
  33. node_command = ['node', 'index.js', '--jupyter-config-data=./config.json']
  34. config = dict(baseUrl=base_url)
  35. if token:
  36. config['token'] = token
  37. with open('config.json', 'w') as fid:
  38. json.dump(config, fid)
  39. print('*' * 60)
  40. print(' '.join(node_command))
  41. return subprocess.check_call(node_command)
  42. if __name__ == '__main__':
  43. TestApp.launch_instance()