traits.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import os
  2. from traitlets import (
  3. HasTraits, Dict, Unicode, List, Bool,
  4. observe, default
  5. )
  6. from jupyter_core.paths import jupyter_path
  7. from jupyter_server.transutils import _i18n
  8. from jupyter_server.utils import url_path_join
  9. class NotebookAppTraits(HasTraits):
  10. ignore_minified_js = Bool(False,
  11. config=True,
  12. help=_i18n(
  13. 'Deprecated: Use minified JS file or not, mainly use during dev to avoid JS recompilation'),
  14. )
  15. jinja_environment_options = Dict(config=True,
  16. help=_i18n("Supply extra arguments that will be passed to Jinja environment."))
  17. jinja_template_vars = Dict(
  18. config=True,
  19. help=_i18n(
  20. "Extra variables to supply to jinja templates when rendering."),
  21. )
  22. enable_mathjax = Bool(True, config=True,
  23. help="""Whether to enable MathJax for typesetting math/TeX
  24. MathJax is the javascript library Jupyter uses to render math/LaTeX. It is
  25. very large, so you may want to disable it if you have a slow internet
  26. connection, or for offline use of the notebook.
  27. When disabled, equations etc. will appear as their untransformed TeX source.
  28. """
  29. )
  30. @observe('enable_mathjax')
  31. def _update_enable_mathjax(self, change):
  32. """set mathjax url to empty if mathjax is disabled"""
  33. if not change['new']:
  34. self.mathjax_url = u''
  35. extra_static_paths = List(Unicode(), config=True,
  36. help="""Extra paths to search for serving static files.
  37. This allows adding javascript/css to be available from the notebook server machine,
  38. or overriding individual files in the IPython"""
  39. )
  40. @property
  41. def static_file_path(self):
  42. """return extra paths + the default location"""
  43. return self.extra_static_paths
  44. static_custom_path = List(Unicode(),
  45. help=_i18n(
  46. """Path to search for custom.js, css""")
  47. )
  48. @default('static_custom_path')
  49. def _default_static_custom_path(self):
  50. return [
  51. os.path.join(self.config_dir, 'custom')
  52. ]
  53. extra_template_paths = List(Unicode(), config=True,
  54. help=_i18n("""Extra paths to search for serving jinja templates.
  55. Can be used to override templates from notebook.templates.""")
  56. )
  57. @property
  58. def template_file_path(self):
  59. """return extra paths + the default locations"""
  60. return self.extra_template_paths
  61. extra_nbextensions_path = List(Unicode(), config=True,
  62. help=_i18n(
  63. """extra paths to look for Javascript notebook extensions""")
  64. )
  65. @property
  66. def nbextensions_path(self):
  67. """The path to look for Javascript notebook extensions"""
  68. path = self.extra_nbextensions_path + jupyter_path('nbextensions')
  69. # FIXME: remove IPython nbextensions path after a migration period
  70. try:
  71. from IPython.paths import get_ipython_dir
  72. except ImportError:
  73. pass
  74. else:
  75. path.append(os.path.join(get_ipython_dir(), 'nbextensions'))
  76. return path
  77. mathjax_url = Unicode("", config=True,
  78. help="""A custom url for MathJax.js.
  79. Should be in the form of a case-sensitive url to MathJax,
  80. for example: /static/components/MathJax/MathJax.js
  81. """
  82. )
  83. @property
  84. def static_url_prefix(self):
  85. """Get the static url prefix for serving static files."""
  86. return super(NotebookAppTraits, self).static_url_prefix
  87. @default('mathjax_url')
  88. def _default_mathjax_url(self):
  89. if not self.enable_mathjax:
  90. return u''
  91. static_url_prefix = self.static_url_prefix
  92. return url_path_join(static_url_prefix, 'components', 'MathJax', 'MathJax.js')
  93. @observe('mathjax_url')
  94. def _update_mathjax_url(self, change):
  95. new = change['new']
  96. if new and not self.enable_mathjax:
  97. # enable_mathjax=False overrides mathjax_url
  98. self.mathjax_url = u''
  99. else:
  100. self.log.info(_i18n("Using MathJax: %s"), new)
  101. mathjax_config = Unicode("TeX-AMS-MML_HTMLorMML-full,Safe", config=True,
  102. help=_i18n(
  103. """The MathJax.js configuration file that is to be used.""")
  104. )
  105. @observe('mathjax_config')
  106. def _update_mathjax_config(self, change):
  107. self.log.info(
  108. _i18n("Using MathJax configuration file: %s"), change['new'])
  109. quit_button = Bool(True, config=True,
  110. help="""If True, display a button in the dashboard to quit
  111. (shutdown the notebook server)."""
  112. )
  113. nbserver_extensions = Dict({}, config=True,
  114. help=(_i18n("Dict of Python modules to load as notebook server extensions."
  115. "Entry values can be used to enable and disable the loading of"
  116. "the extensions. The extensions will be loaded in alphabetical "
  117. "order."))
  118. )