main.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """
  2. An example demonstrating a stand-alone "console".
  3. Copyright (c) Jupyter Development Team.
  4. Distributed under the terms of the Modified BSD License.
  5. Example
  6. -------
  7. To run the example, see the instructions in the README to build it. Then
  8. run ``python main.py`` and navigate your browser to ``localhost:8765``.
  9. Note
  10. ----
  11. This file provides the Python code for interacting with the Jupyter notebook
  12. server using ``ZMQ`` and the ``tornado`` web server.
  13. """
  14. import re
  15. import subprocess
  16. import sys
  17. import threading
  18. import tornado.web
  19. # Install the pyzmq ioloop. Must be done after importing tornado.web and
  20. # before importing any additional tornado modules
  21. from zmq.eventloop import ioloop
  22. ioloop.install()
  23. PORT = 8765
  24. """int: Port number of web application"""
  25. class MainPageHandler(tornado.web.RequestHandler):
  26. """Handle requests between the main app page and notebook server."""
  27. def initialize(self, base_url):
  28. """Intitialize the base URL of the handler."""
  29. self.base_url = base_url
  30. def get(self):
  31. """Get the main page for the application's interface."""
  32. return self.render("index.html", static=self.static_url,
  33. base_url=self.base_url)
  34. def main(argv):
  35. """Start the 'console' example.
  36. - Start the Tornado main event loop for the Jupyter notebook server
  37. - Set up the main page event handler for the 'console' example
  38. """
  39. url = "http://localhost:%s" % PORT
  40. nb_command = [sys.executable, '-m', 'notebook', '--no-browser', '--debug',
  41. '--NotebookApp.allow_origin="%s"' % url]
  42. nb_server = subprocess.Popen(nb_command, stderr=subprocess.STDOUT,
  43. stdout=subprocess.PIPE)
  44. # Wait for Jupyter notebook server to complete start up
  45. while 1:
  46. line = nb_server.stdout.readline().decode('utf-8').strip()
  47. if not line:
  48. continue
  49. print(line)
  50. if 'Jupyter Notebook is running at:' in line:
  51. base_url = re.search('(http.*?)$', line).groups()[0]
  52. break
  53. while 1:
  54. line = nb_server.stdout.readline().decode('utf-8').strip()
  55. if not line:
  56. continue
  57. print(line)
  58. if 'Control-C' in line:
  59. break
  60. def print_thread():
  61. while 1:
  62. line = nb_server.stdout.readline().decode('utf-8').strip()
  63. if not line:
  64. continue
  65. print(line)
  66. thread = threading.Thread(target=print_thread)
  67. thread.setDaemon(True)
  68. thread.start()
  69. handlers = [
  70. (r"/", MainPageHandler, {'base_url': base_url}),
  71. (r'/(.*)', tornado.web.StaticFileHandler, {'path': '.'}),
  72. ]
  73. app = tornado.web.Application(handlers, static_path='build',
  74. template_path='.',
  75. compiled_template_cache=False)
  76. app.listen(PORT, 'localhost')
  77. # For Windows, add no-op to wake every 5 seconds (5000 ms) to handle
  78. # signals that may be ignored by the Tornado main event loop
  79. if sys.platform.startswith('win'):
  80. pc = ioloop.PeriodicCallback(lambda: None, 5000)
  81. pc.start()
  82. loop = ioloop.IOLoop.current()
  83. print('Browse to http://localhost:%s' % PORT)
  84. try:
  85. # Start the Tornado main event loop
  86. loop.start()
  87. except KeyboardInterrupt:
  88. print(" Shutting down on SIGINT")
  89. finally:
  90. nb_server.kill()
  91. loop.close()
  92. if __name__ == '__main__':
  93. main(sys.argv)