main.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """
  2. Copyright (c) Jupyter Development Team.
  3. Distributed under the terms of the Modified BSD License.
  4. """
  5. import re
  6. import subprocess
  7. import sys
  8. import threading
  9. import tornado.web
  10. # Install the pyzmq ioloop. This has to be done before anything else from
  11. # tornado is imported.
  12. from zmq.eventloop import ioloop
  13. ioloop.install()
  14. PORT = 8765
  15. class MainPageHandler(tornado.web.RequestHandler):
  16. def initialize(self, base_url, ws_url):
  17. self.base_url = base_url
  18. self.ws_url = ws_url
  19. def get(self):
  20. return self.render("index.html", static=self.static_url,
  21. terminals_available=sys.platform != 'win32',
  22. base_url=self.base_url, ws_url=self.ws_url,)
  23. def main(argv):
  24. # Start a notebook server with cross-origin access.
  25. nb_command = [sys.executable, '-m', 'notebook', '--no-browser',
  26. '--debug',
  27. # FIXME: allow-origin=* only required for notebook < 4.3
  28. '--NotebookApp.allow_origin="*"',
  29. # disable user password:
  30. '--NotebookApp.password=',
  31. ]
  32. nb_server = subprocess.Popen(nb_command, stderr=subprocess.STDOUT,
  33. stdout=subprocess.PIPE)
  34. # wait for notebook server to start up
  35. while 1:
  36. line = nb_server.stdout.readline().decode('utf-8').strip()
  37. if not line:
  38. continue
  39. print(line)
  40. if 'Jupyter Notebook is running at:' in line:
  41. base_url = re.search('(http.*?)$', line).groups()[0]
  42. ws_url = base_url.replace('http', 'ws')
  43. break
  44. while 1:
  45. line = nb_server.stdout.readline().decode('utf-8').strip()
  46. if not line:
  47. continue
  48. print(line)
  49. if 'Control-C' in line:
  50. break
  51. def print_thread():
  52. while 1:
  53. line = nb_server.stdout.readline().decode('utf-8').strip()
  54. if not line:
  55. continue
  56. print(line)
  57. thread = threading.Thread(target=print_thread)
  58. thread.setDaemon(True)
  59. thread.start()
  60. handlers = [
  61. (r"/", MainPageHandler, {'base_url': base_url, 'ws_url': ws_url}),
  62. (r'/lab/(.*)', tornado.web.StaticFileHandler, {'path': 'build/'}),
  63. ]
  64. app = tornado.web.Application(handlers, static_path='build',
  65. template_path='.',
  66. compiled_template_cache=False)
  67. app.listen(PORT, 'localhost')
  68. if sys.platform.startswith('win'):
  69. # add no-op to wake every 5s
  70. # to handle signals that may be ignored by the inner loop
  71. pc = ioloop.PeriodicCallback(lambda: None, 5000)
  72. pc.start()
  73. loop = ioloop.IOLoop.current()
  74. print('Browse to http://localhost:%s' % PORT)
  75. try:
  76. loop.start()
  77. except KeyboardInterrupt:
  78. print(" Shutting down on SIGINT")
  79. finally:
  80. nb_server.kill()
  81. loop.close()
  82. if __name__ == '__main__':
  83. main(sys.argv)