main.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. PORT = 8765
  11. class MainPageHandler(tornado.web.RequestHandler):
  12. def initialize(self, base_url):
  13. self.base_url = base_url
  14. def get(self):
  15. return self.render("index.html", static=self.static_url,
  16. base_url=self.base_url)
  17. def main(argv):
  18. url = "http://localhost:%s" % PORT
  19. nb_command = [sys.executable, '-m', 'notebook', '--no-browser', '--debug',
  20. '--NotebookApp.allow_origin="%s"' % url]
  21. nb_server = subprocess.Popen(nb_command, stderr=subprocess.STDOUT,
  22. stdout=subprocess.PIPE)
  23. # wait for notebook server to start up
  24. while 1:
  25. line = nb_server.stdout.readline().decode('utf-8').strip()
  26. if not line:
  27. continue
  28. print(line)
  29. if 'Jupyter Notebook is running at:' in line:
  30. base_url = re.search('(http.*?)$', line).groups()[0]
  31. break
  32. while 1:
  33. line = nb_server.stdout.readline().decode('utf-8').strip()
  34. if not line:
  35. continue
  36. print(line)
  37. if 'Control-C' in line:
  38. break
  39. def print_thread():
  40. while 1:
  41. line = nb_server.stdout.readline().decode('utf-8').strip()
  42. if not line:
  43. continue
  44. print(line)
  45. thread = threading.Thread(target=print_thread)
  46. thread.setDaemon(True)
  47. thread.start()
  48. handlers = [
  49. (r"/", MainPageHandler, {'base_url': base_url}),
  50. (r'/(.*)', tornado.web.StaticFileHandler, {'path': '.'}),
  51. ]
  52. app = tornado.web.Application(handlers, static_path='build',
  53. template_path='.',
  54. compiled_template_cache=False)
  55. app.listen(PORT, 'localhost')
  56. loop = tornado.ioloop.IOLoop.instance()
  57. print('Browse to http://localhost:%s' % PORT)
  58. try:
  59. loop.start()
  60. except KeyboardInterrupt:
  61. print(" Shutting down on SIGINT")
  62. finally:
  63. nb_server.kill()
  64. loop.close()
  65. if __name__ == '__main__':
  66. main(sys.argv)