main.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """
  2. An example demonstrating a stand-alone "notebook".
  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 'notebook' example.
  36. - Start the Tornado main event loop for the Jupyter notebook server
  37. - Set up the main page event handler for the 'notebook' example
  38. """
  39. nb_command = [sys.executable, '-m', 'notebook', '--no-browser',
  40. '--debug',
  41. # FIXME: allow-origin=* only required for notebook < 4.3
  42. '--NotebookApp.allow_origin="*"',
  43. # disable user password:
  44. '--NotebookApp.password=',
  45. ]
  46. nb_server = subprocess.Popen(nb_command, stderr=subprocess.STDOUT,
  47. stdout=subprocess.PIPE)
  48. # Wait for Jupyter notebook server to complete start up
  49. while 1:
  50. line = nb_server.stdout.readline().decode('utf-8').strip()
  51. if not line:
  52. continue
  53. print(line)
  54. if 'Jupyter Notebook is running at:' in line:
  55. base_url = re.search(r'(http[^\?]+)', line).groups()[0]
  56. break
  57. while 1:
  58. line = nb_server.stdout.readline().decode('utf-8').strip()
  59. if not line:
  60. continue
  61. print(line)
  62. if 'Control-C' in line:
  63. break
  64. def print_thread():
  65. while 1:
  66. line = nb_server.stdout.readline().decode('utf-8').strip()
  67. if not line:
  68. continue
  69. print(line)
  70. thread = threading.Thread(target=print_thread)
  71. thread.setDaemon(True)
  72. thread.start()
  73. handlers = [
  74. (r"/", MainPageHandler, {'base_url': base_url}),
  75. (r'/(.*)', tornado.web.StaticFileHandler, {'path': '.'}),
  76. ]
  77. app = tornado.web.Application(handlers, static_path='build',
  78. template_path='.',
  79. compiled_template_cache=False)
  80. app.listen(PORT, 'localhost')
  81. # For Windows, add no-op to wake every 5 seconds (5000 ms) to handle
  82. # signals that may be ignored by the Tornado main event loop
  83. if sys.platform.startswith('win'):
  84. pc = ioloop.PeriodicCallback(lambda: None, 5000)
  85. pc.start()
  86. loop = ioloop.IOLoop.current()
  87. print('Browse to http://localhost:%s' % PORT)
  88. try:
  89. # Start the Tornado main event loop
  90. loop.start()
  91. except KeyboardInterrupt:
  92. print(" Shutting down on SIGINT")
  93. finally:
  94. nb_server.kill()
  95. loop.close()
  96. if __name__ == '__main__':
  97. main(sys.argv)