main.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. class ExampleHandler(IPythonHandler):
  16. """Handle requests between the main app page and notebook server."""
  17. def get(self):
  18. """Get the main page for the application's interface."""
  19. return self.write(self.render_template("index.html",
  20. static=self.static_url, base_url=self.base_url,
  21. token=self.settings['token'],
  22. terminals_available=self.settings['terminals_available']))
  23. def get_template(self, name):
  24. loader = FileSystemLoader(os.getcwd())
  25. return loader.load(self.settings['jinja2_env'], name)
  26. class ExampleApp(NotebookApp):
  27. default_url = Unicode('/example')
  28. def init_webapp(self):
  29. """initialize tornado webapp and httpserver.
  30. """
  31. super(ExampleApp, self).init_webapp()
  32. default_handlers = [
  33. (r'/example/?', ExampleHandler),
  34. (r"/example/(.*)", FileFindHandler,
  35. {'path': 'build'}),
  36. ]
  37. self.web_app.add_handlers(".*$", default_handlers)
  38. if __name__ == '__main__':
  39. ExampleApp.launch_instance()