main.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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``.
  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. def get_template(self, name):
  22. loader = FileSystemLoader(os.getcwd())
  23. return loader.load(self.settings['jinja2_env'], name)
  24. class ExampleApp(NotebookApp):
  25. default_url = Unicode('/example')
  26. def init_webapp(self):
  27. """initialize tornado webapp and httpserver.
  28. """
  29. super(ExampleApp, self).init_webapp()
  30. default_handlers = [
  31. (r'/example/?', ExampleHandler),
  32. (r"/example/(.*)", FileFindHandler,
  33. {'path': 'build'}),
  34. ]
  35. self.web_app.add_handlers(".*$", default_handlers)
  36. if __name__ == '__main__':
  37. ExampleApp.launch_instance()