main.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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``.
  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 notebook.utils import url_path_join as ujoin
  15. from traitlets import Unicode
  16. HERE = os.path.dirname(__file__)
  17. class ExampleHandler(IPythonHandler):
  18. """
  19. Serve a notebook file from the filesystem in the notebook interface
  20. """
  21. def get(self):
  22. """Get the main page for the application's interface."""
  23. # Options set here can be read with PageConfig.getOption
  24. config_data = {
  25. # Use camelCase here, since that's what the lab components expect
  26. 'baseUrl': self.base_url,
  27. 'token': self.settings['token'],
  28. 'notebookPath': 'test.ipynb',
  29. 'frontendUrl': ujoin(self.base_url, 'example/'),
  30. # FIXME: Don't use a CDN here
  31. 'mathjaxUrl': "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js",
  32. 'mathjaxConfig': "TeX-AMS_CHTML-full,Safe"
  33. }
  34. return self.write(
  35. self.render_template(
  36. 'index.html',
  37. static=self.static_url,
  38. base_url=self.base_url,
  39. config_data=config_data
  40. )
  41. )
  42. def get_template(self, name):
  43. loader = FileSystemLoader(HERE)
  44. return loader.load(self.settings['jinja2_env'], name)
  45. class ExampleApp(NotebookApp):
  46. default_url = Unicode('/example')
  47. def init_webapp(self):
  48. """initialize tornado webapp and httpserver.
  49. """
  50. super(ExampleApp, self).init_webapp()
  51. default_handlers = [
  52. (ujoin(self.base_url, r'/example/?'), ExampleHandler),
  53. (ujoin(self.base_url, r"/example/(.*)"), FileFindHandler,
  54. {'path': os.path.join(HERE, 'build')})
  55. ]
  56. self.web_app.add_handlers('.*$', default_handlers)
  57. if __name__ == '__main__':
  58. ExampleApp.launch_instance()