main.py 1.4 KB

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