main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. # disable token:
  32. '--NotebookApp.token='
  33. ]
  34. nb_server = subprocess.Popen(nb_command, stderr=subprocess.STDOUT,
  35. stdout=subprocess.PIPE)
  36. # wait for notebook server to start up
  37. while 1:
  38. line = nb_server.stdout.readline().decode('utf-8').strip()
  39. if not line:
  40. continue
  41. print(line)
  42. if 'Jupyter Notebook is running at:' in line:
  43. base_url = re.search('(http.*?)$', line).groups()[0]
  44. ws_url = base_url.replace('http', 'ws')
  45. break
  46. while 1:
  47. line = nb_server.stdout.readline().decode('utf-8').strip()
  48. if not line:
  49. continue
  50. print(line)
  51. if 'Control-C' in line:
  52. break
  53. def print_thread():
  54. while 1:
  55. line = nb_server.stdout.readline().decode('utf-8').strip()
  56. if not line:
  57. continue
  58. print(line)
  59. thread = threading.Thread(target=print_thread)
  60. thread.setDaemon(True)
  61. thread.start()
  62. handlers = [
  63. (r"/", MainPageHandler, {'base_url': base_url, 'ws_url': ws_url}),
  64. (r'/lab/(.*)', tornado.web.StaticFileHandler, {'path': 'build/'}),
  65. ]
  66. app = tornado.web.Application(handlers, static_path='build',
  67. template_path='.',
  68. compiled_template_cache=False)
  69. app.listen(PORT, 'localhost')
  70. if sys.platform.startswith('win'):
  71. # add no-op to wake every 5s
  72. # to handle signals that may be ignored by the inner loop
  73. pc = ioloop.PeriodicCallback(lambda: None, 5000)
  74. pc.start()
  75. loop = ioloop.IOLoop.current()
  76. print('Browse to http://localhost:%s' % PORT)
  77. try:
  78. loop.start()
  79. except KeyboardInterrupt:
  80. print(" Shutting down on SIGINT")
  81. finally:
  82. nb_server.kill()
  83. loop.close()
  84. if __name__ == '__main__':
  85. main(sys.argv)