File: app.py

package info (click to toggle)
jupyter-server-mathjax 0.2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 140 kB
  • sloc: python: 126; javascript: 73; makefile: 12
file content (79 lines) | stat: -rw-r--r-- 2,555 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from pathlib import Path
from traitlets import default, observe, Unicode

from tornado.web import RedirectHandler

from jupyter_server.extension.application import ExtensionApp
from jupyter_server.utils import url_path_join
try:
    from jupyter_server.transutils import _i18n
except ImportError:
    from jupyter_server.transutils import _ as _i18n

STATIC_ASSETS_PATH = Path(__file__).parent / "static"


class DeprecatedRedirectHandler(RedirectHandler):
    def get(self, *args, **kwargs):
        import warnings

        warnings.warn(
            _i18n("Redirecting old Notebook MathJax URL to new one. This will be removed in a future release."),
            PendingDeprecationWarning,
        )
        super().get(*args, **kwargs)


class MathJaxExtension(ExtensionApp):

    name = "jupyter_server_mathjax"

    # By listing the path to the assets here, jupyter_server
    # automatically creates a static file handler at
    # /static/jupyter_server_mathjax/...
    static_paths = [str(STATIC_ASSETS_PATH)]

    mathjax_config = Unicode(
        "TeX-AMS-MML_HTMLorMML-full,Safe",
        config=True,
        help=_i18n("""The MathJax.js configuration file that is to be used."""),
    )

    @observe("mathjax_config")
    def _update_mathjax_config(self, change):
        self.log.info(_i18n("Using MathJax configuration file: %s"), change["new"])

    def initialize_settings(self):
        # Add settings specific to this extension to the
        # tornado webapp settings.
        self.settings.update(
            {
                "mathjax_config": self.mathjax_config,
                "mathjax_url": url_path_join(self.static_url_prefix, "MathJax.js"),
            }
        )

    def initialize_handlers(self):
        webapp = self.serverapp.web_app
        base_url = self.serverapp.base_url
        host_pattern = ".*$"

        # Add a deprecated redirect for all MathJax paths from the classic
        # notebook to the static endpoint created for this extension.
        webapp.add_handlers(
            host_pattern,
            [
                (
                    url_path_join(base_url, "/static/components/MathJax/(.*)"),
                    DeprecatedRedirectHandler,
                    {
                        "url": url_path_join(
                            self.static_url_prefix, "/{0}"  # {0} = group 0 in url path
                        )
                    },
                )
            ],
        )