main.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """
  2. An example demonstrating a stand-alone "terminal".
  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``.
  9. """
  10. import os
  11. from jinja2 import FileSystemLoader
  12. from notebook.base.handlers import IPythonHandler, FileFindHandler
  13. from notebook.notebookapp import NotebookApp
  14. from traitlets import Unicode
  15. HERE = os.path.dirname(__file__)
  16. class ExampleHandler(IPythonHandler):
  17. """Handle requests between the main app page and notebook server."""
  18. def get(self):
  19. """Get the main page for the application's interface."""
  20. available = self.settings['terminals_available']
  21. return self.write(self.render_template('index.html',
  22. static=self.static_url,
  23. base_url=self.base_url,
  24. token=self.settings['token'],
  25. terminals_available=available))
  26. def get_template(self, name):
  27. loader = FileSystemLoader(HERE)
  28. return loader.load(self.settings['jinja2_env'], name)
  29. class ExampleApp(NotebookApp):
  30. default_url = Unicode('/example')
  31. def init_webapp(self):
  32. """initialize tornado webapp and httpserver.
  33. """
  34. super(ExampleApp, self).init_webapp()
  35. default_handlers = [
  36. (r'/example/?', ExampleHandler),
  37. (r"/example/(.*)", FileFindHandler,
  38. {'path': os.path.join(HERE, 'build')})
  39. ]
  40. self.web_app.add_handlers('.*$', default_handlers)
  41. if __name__ == '__main__':
  42. ExampleApp.launch_instance()