Skip to content

Commit

Permalink
Update mathjax setting
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Aug 1, 2023
1 parent b7e53cc commit 4412104
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 524 deletions.
2 changes: 1 addition & 1 deletion packages/voila/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@jupyterlab/mainmenu": "^4.0.0",
"@jupyterlab/markdownviewer-extension": "^4.0.0",
"@jupyterlab/markedparser-extension": "^4.0.0",
"@jupyterlab/mathjax-extension": "^4.0.0",
"@jupyterlab/mathjax2-extension": "^4.0.0-alpha.21",
"@jupyterlab/nbformat": "^4.0.0",
"@jupyterlab/notebook": "^4.0.0",
"@jupyterlab/outputarea": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/voila/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async function main() {
(p: any) => p.id === '@jupyterlab/codemirror-extension:languages'
),
require('@jupyterlab/markedparser-extension'),
require('@jupyterlab/mathjax2-extension'),
require('@jupyterlab/rendermime-extension'),
require('@jupyterlab/theme-light-extension'),
require('@jupyterlab/theme-dark-extension'),
Expand Down
20 changes: 18 additions & 2 deletions voila/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@

_kernel_id_regex = r"(?P<kernel_id>\w+-\w+-\w+-\w+-\w+)"

HERE = os.path.dirname(os.path.abspath(__file__))


def _(x):
return x
Expand Down Expand Up @@ -205,6 +203,22 @@ class Voila(Application):

static_paths = List([STATIC_ROOT], config=True, help=_("paths to static assets"))

mathjax_config = Unicode(
"TeX-AMS_CHTML-full,Safe",
help="""
Mathjax default configuration
""",
).tag(config=True)

mathjax_url = Unicode(
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js",
help="""
URL to load Mathjax from.
Defaults to loading from cdnjs.
""",
).tag(config=True)

port_retries = Integer(
50,
config=True,
Expand Down Expand Up @@ -613,6 +627,8 @@ def init_settings(self) -> Dict:
identity_provider=self.identity_provider,
kernel_websocket_connection_class=self.kernel_websocket_connection_class,
login_url=url_path_join(self.base_url, "/login"),
mathjax_config=self.mathjax_config,
mathjax_url=self.mathjax_url,
)
settings[self.name] = self # Why???

Expand Down
6 changes: 4 additions & 2 deletions voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def initialize(self, **kwargs):
self.traitlet_config = kwargs.pop("config", None)
self.voila_configuration = kwargs["voila_configuration"]
self.prelaunch_hook = kwargs.get("prelaunch_hook", None)

# we want to avoid starting multiple kernels due to template mistakes
self.kernel_started = False

Expand Down Expand Up @@ -182,7 +181,8 @@ async def get_generator(self, path=None):
if file_extenstion not in supported_file_extensions:
self.redirect_to_file(path)
return

mathjax_config = self.settings.get("mathjax_config")
mathjax_url = self.settings.get("mathjax_url")
gen = NotebookRenderer(
request_handler=self,
voila_configuration=self.voila_configuration,
Expand All @@ -201,6 +201,8 @@ async def get_generator(self, path=None):
extension_whitelist=self.voila_configuration.extension_whitelist,
extension_blacklist=self.voila_configuration.extension_blacklist,
),
mathjax_config=mathjax_config,
mathjax_url=mathjax_url,
)

await gen.initialize(template=template_arg, theme=theme_arg)
Expand Down
5 changes: 4 additions & 1 deletion voila/notebook_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def __init__(self, **kwargs):
self.kernel_started = False
self.stop_generator = False
self.rendered_cache: List[str] = []
self.mathjax_url = kwargs.get("mathjax_url")
self.mathjax_config = kwargs.get("mathjax_config")

async def initialize(self, **kwargs) -> None:
"""Initialize the notebook generator."""
Expand Down Expand Up @@ -133,7 +135,7 @@ async def initialize(self, **kwargs) -> None:
extra_resources = extra_resources.to_dict()
if extra_resources:
recursive_update(self.resources, extra_resources)

mathjax_full_url = f"{self.mathjax_url}?config={self.mathjax_config}"
self.exporter = VoilaExporter(
template_paths=self.template_paths,
template_name=self.template_name,
Expand All @@ -143,6 +145,7 @@ async def initialize(self, **kwargs) -> None:
base_url=self.base_url,
page_config=self.page_config,
show_margins=self.voila_configuration.show_margins,
mathjax_url=mathjax_full_url,
)

if self.voila_configuration.strip_sources:
Expand Down
9 changes: 2 additions & 7 deletions voila/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,8 @@ def get_page_config(
"fullStaticUrl": url_path_join(base_url, "voila/static"),
"fullLabextensionsUrl": url_path_join(base_url, "voila/labextensions"),
}

mathjax_config = settings.get("mathjax_config", "TeX-AMS_HTML-full,Safe")
# TODO Remove CDN usage.
mathjax_url = settings.get(
"mathjax_url",
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js",
)
mathjax_config = settings.get("mathjax_config")
mathjax_url = settings.get("mathjax_url")
page_config.setdefault("mathjaxConfig", mathjax_config)
page_config.setdefault("fullMathjaxUrl", mathjax_url)

Expand Down
5 changes: 5 additions & 0 deletions voila/voila_kernel_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ def _notebook_renderer_factory(
notebook. Defaults to None.
"""
voila_configuration = self.parent.voila_configuration
settings = self.parent.app.settings
mathjax_config = settings.get("mathjax_config")
mathjax_url = settings.get("mathjax_url")
return NotebookRenderer(
voila_configuration=voila_configuration,
traitlet_config=self.parent.config,
Expand All @@ -383,6 +386,8 @@ def _notebook_renderer_factory(
extension_whitelist=voila_configuration.extension_whitelist,
extension_blacklist=voila_configuration.extension_blacklist,
),
mathjax_config=mathjax_config,
mathjax_url=mathjax_url,
)

def _notebook_filter(self, nb_path: Path) -> bool:
Expand Down
Loading

0 comments on commit 4412104

Please sign in to comment.