Skip to content

Commit

Permalink
Merge pull request #8 from harshad16/modh
Browse files Browse the repository at this point in the history
enable plugin for jupyter git
  • Loading branch information
harshad16 authored Jul 26, 2021
2 parents acda5be + d66c228 commit ff9dc2c
Show file tree
Hide file tree
Showing 16 changed files with 1,606 additions and 756 deletions.
6 changes: 4 additions & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ verify_ssl = true
[packages]
notebook = ">=6.0.2"
jupyterhub = ">=1.3"
jupyterlab = ">=3.0.0"
jupyterlab = "==3.0.14"
jupyterlab-requirements = ">=0.6.4"
jupyter_kernel_gateway = "==2.4.0"
jupyter-nbrequirements = "*"
jupyterlab-git = "*"
supervisor = "==4.1.0"
jupyter_nbextensions_configurator = "*"
ipython = "==7.16.1"
cryptography = "==3.3.1"
dictdiffer = "==0.5.0"
jupyter-server= "==1.6.1"
nbclassic = "==0.2.6"
jupyter-server= "==1.4.1"

[requires]
python_version = "3.8"
643 changes: 352 additions & 291 deletions Pipfile.lock

Large diffs are not rendered by default.

90 changes: 90 additions & 0 deletions jupyter-server-mathjax/copyAssets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const fse = require("fs-extra");
const path = require("path");

const srcDir = path.join(__dirname, "node_modules", "mathjax");
const dstDir = path.join(__dirname, "jupyter_server_mathjax", "static");

/*
* Copy MathJax static assets, but trim which assets in a similar way to what
* notebook does on dist (in setupbase).
*/

const rps = path.sep.replace("\\", "\\\\");

function re_join(parts) {
return parts.join(rps);
}

const include = [
["MathJax.js"],
["LICENSE"],
["config", "TeX-AMS-MML_HTMLorMML-full.js"],
["config", "Safe.js"],
];

const re_include = [
["jax", "output", `[^${rps}]+.js$`],
["jax", "output", "autoload", ".*"],
["localization", ".*"],
["fonts", "HTML-CSS", "STIX-Web", "woff", ".*"],
["extensions", ".*"],
["jax", "input", "TeX", ".*"],
["jax", "output", "HTML-CSS", "fonts", "STIX-Web", ".*"],
["jax", "output", "SVG", "fonts", "STIX-Web", ".*"],
["jax", "element", "mml", ".*"],
];

function isPartial(parts, candidate) {
return parts.length <= candidate.length;
}

function partialPathMatch(parts, candidate) {
const np = Math.min(candidate.length, parts.length);
for (let i = 0; i < np; ++i) {
if (candidate[i] !== parts[i]) {
return false;
}
}
return true;
}

function pathOk(p) {
if (!p) {
return true;
}
const parts = p.split(path.sep);
for (let c of include) {
if (isPartial(parts, c)) {
// Check for partial matches (to ensure dirs get included)
if (partialPathMatch(parts, c)) {
return true;
}
} else {
if (c.join(path.sep) == p) {
return true;
}
}
}
for (let c of re_include) {
const lead = c.slice(0, c.length - 1);
if (isPartial(parts, lead)) {
// Check for partial matches (to ensure dirs get included)
if (partialPathMatch(parts, lead)) {
return true;
}
} else {
const re = new RegExp(re_join(c));
if (re.test(p)) {
return true;
}
}
}
return false;
}

function filterFunc(src, dest) {
const relative = path.relative(srcDir, src);
return pathOk(relative);
}

fse.copy(srcDir, dstDir, { filter: filterFunc });
10 changes: 10 additions & 0 deletions jupyter-server-mathjax/jupyter_server_mathjax/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from .app import MathJaxExtension


def _jupyter_server_extension_points():
return [
{"module": "jupyter_server_mathjax", "app": MathJaxExtension},
]
2 changes: 2 additions & 0 deletions jupyter-server-mathjax/jupyter_server_mathjax/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version_info = (0, 2, 3)
__version__ = ".".join(map(str, version_info))
77 changes: 77 additions & 0 deletions jupyter-server-mathjax/jupyter_server_mathjax/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 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
from jupyter_server.transutils import _


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


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

warnings.warn(
"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=_("""The MathJax.js configuration file that is to be used."""),
)

@observe("mathjax_config")
def _update_mathjax_config(self, change):
self.log.info(_("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
)
},
)
],
)
Empty file.
14 changes: 14 additions & 0 deletions jupyter-server-mathjax/jupyter_server_mathjax/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import io
import logging
import pytest
from traitlets import default

pytest_plugins = ["jupyter_server.pytest_plugin"]


@pytest.fixture
def jp_server_config():
return {"ServerApp": {"jpserver_extensions": {"jupyter_server_mathjax": True}}}
47 changes: 47 additions & 0 deletions jupyter-server-mathjax/jupyter_server_mathjax/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

"""Basic tests for the notebook handlers.
"""

import pytest
from tornado.httpclient import HTTPClientError
from jupyter_server.utils import url_path_join as ujoin


async def test_mathjax_mainjs_handler(jp_fetch):
r = await jp_fetch("static", "jupyter_server_mathjax", "MathJax.js")
assert r.code == 200


async def test_mathjax_conf_handler(jp_fetch):
r = await jp_fetch(
"static", "jupyter_server_mathjax", "config", "TeX-AMS-MML_HTMLorMML-full.js"
)
assert r.code == 200

r = await jp_fetch("static", "jupyter_server_mathjax", "config", "Safe.js")
assert r.code == 200


@pytest.mark.parametrize(
"asset_file",
["MathJax.js", "config/TeX-AMS-MML_HTMLorMML-full.js", "config/Safe.js"],
)
async def test_redirects_from_classic_notebook_endpoints(
jp_fetch, jp_base_url, asset_file
):
old_prefix = ujoin("static", "components", "MathJax")
new_prefix = ujoin("static", "jupyter_server_mathjax")

# Verify that the redirect is in place
with pytest.raises(HTTPClientError) as error_info, pytest.deprecated_call(
match="Redirecting old Notebook MathJax URL .*"
):
await jp_fetch(old_prefix, asset_file, follow_redirects=False)

err = error_info.value
assert err.code == 301
assert err.response.headers["Location"] == ujoin(
jp_base_url, new_prefix, asset_file
)
Loading

0 comments on commit ff9dc2c

Please sign in to comment.