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