Skip to content
Browse files

allow the notebook to run without MathJax

* add `--no-mathjax` flag for disabling mathjax in the notebook server
* A jQuery dialog with our 'no mathjax' message will appear if mathjax is unavailable, but the notebook will be fully functional after dismissal.
* Various calls to MathJax.Hub.typeset moved to Cell.typeset, which checks for MathJax existence and is a no-op without it.

closes #1071
  • Loading branch information...
1 parent 91d457e commit ef48cc07cfb8ee707c6ddba14e66391d481275bc @minrk committed
View
2 IPython/frontend/html/notebook/handlers.py
@@ -219,6 +219,7 @@ def get(self):
base_project_url=u'/', base_kernel_url=u'/',
kill_kernel=False,
read_only=False,
+ enable_mathjax=self.application.ipython_app.enable_mathjax,
)
@@ -237,6 +238,7 @@ def get(self, notebook_id):
base_project_url=u'/', base_kernel_url=u'/',
kill_kernel=False,
read_only=self.read_only,
+ enable_mathjax=self.application.ipython_app.enable_mathjax,
)
View
24 IPython/frontend/html/notebook/notebookapp.py
@@ -128,6 +128,17 @@ def __init__(self, ipython_app, kernel_manager, notebook_manager, log):
{'NotebookApp' : {'open_browser' : False}},
"Don't open the notebook in a browser after startup."
)
+flags['no-mathjax']=(
+ {'NotebookApp' : {'enable_mathjax' : False}},
+ """Disable MathJax
+
+ MathJax is the javascript library IPython uses to render math/LaTeX. It is
+ very large, so you may want to disable it if you have a slow internet
+ connection, or for offline use of the notebook.
+
+ When disabled, equations etc. will appear as their untransformed TeX source.
+ """
+)
flags['read-only'] = (
{'NotebookApp' : {'read_only' : True}},
"""Allow read-only access to notebooks.
@@ -144,7 +155,7 @@ def __init__(self, ipython_app, kernel_manager, notebook_manager, log):
# the flags that are specific to the frontend
# these must be scrubbed before being passed to the kernel,
# or it will raise an error on unrecognized flags
-notebook_flags = ['no-browser', 'read-only']
+notebook_flags = ['no-browser', 'no-mathjax', 'read-only']
aliases = dict(ipkernel_aliases)
@@ -230,6 +241,17 @@ def _ip_changed(self, name, old, new):
read_only = Bool(False, config=True,
help="Whether to prevent editing/execution of notebooks."
)
+
+ enable_mathjax = Bool(True, config=True,
+ help="""Whether to enable MathJax for typesetting math/TeX
+
+ MathJax is the javascript library IPython uses to render math/LaTeX. It is
+ very large, so you may want to disable it if you have a slow internet
+ connection, or for offline use of the notebook.
+
+ When disabled, equations etc. will appear as their untransformed TeX source.
+ """
+ )
def parse_command_line(self, argv=None):
super(NotebookApp, self).parse_command_line(argv)
View
12 IPython/frontend/html/notebook/static/css/notebook.css
@@ -415,6 +415,18 @@ div.text_cell_render {
font-family: monospace;
}
+pre.dialog {
+ background-color: #f7f7f7;
+ border: 1px solid #ddd;
+ border-radius: 3px;
+ padding: 0.4em;
+ padding-left: 2em;
+}
+
+p.dialog{
+ padding : 0.2em;
+}
+
@media print {
body { overflow: visible !important; }
.ui-widget-content { border: 0px; }
View
7 IPython/frontend/html/notebook/static/js/cell.js
@@ -88,6 +88,13 @@ var IPython = (function (IPython) {
// Subclasses must implement create_element.
Cell.prototype.create_element = function () {};
+ // typeset with MathJax if MathJax is available
+ Cell.prototype.typeset = function () {
+ if (window.MathJax){
+ MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
+ }
+ };
+
IPython.Cell = Cell;
return IPython;
View
4 IPython/frontend/html/notebook/static/js/codecell.js
@@ -520,7 +520,7 @@ var IPython = (function (IPython) {
this.element.find('div.output').append(toinsert);
// If we just output latex, typeset it.
if ((json.latex !== undefined) || (json.html !== undefined)) {
- MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
+ this.typeset();
};
};
@@ -572,7 +572,7 @@ var IPython = (function (IPython) {
this.element.find('div.output').append(toinsert);
// If we just output latex, typeset it.
if ( (json.latex !== undefined) || (json.html !== undefined) ) {
- MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
+ this.typeset();
};
};
View
66 IPython/frontend/html/notebook/static/js/notebookmain.js
@@ -12,16 +12,62 @@
$(document).ready(function () {
- MathJax.Hub.Config({
- tex2jax: {
- inlineMath: [ ['$','$'], ["\\(","\\)"] ],
- displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
- },
- displayAlign: 'left', // Change this to 'center' to center equations.
- "HTML-CSS": {
- styles: {'.MathJax_Display': {"margin": 0}}
- }
- });
+ if (window.MathJax == undefined){
+ // MathJax undefined, but expected. Draw warning.
+ window.MathJax = null;
+ var dialog = $('<div></div>').html(
+ "<p class='dialog'>"+
+ "We were unable to retrieve MathJax. Math/LaTeX rendering will be disabled."+
+ "</p>"+
+ "<p class='dialog'>"+
+ "With a working internet connection, you can run the following at a Python"+
+ " or IPython prompt, which will install a local copy of MathJax:"+
+ "</p>"+
+ "<pre class='dialog'>"+
+ ">>> from IPython.external import mathjax; mathjax.install_mathjax()"+
+ "</pre>"+
+ "<p class='dialog'>"+
+ "This will try to install MathJax into the directory where you installed"+
+ " IPython. If you installed IPython to a location that requires"+
+ " administrative privileges to write, you will need to make this call as"+
+ " an administrator."+
+ "</p>"+
+ "<p class='dialog'>"+
+ "On OSX/Linux/Unix, this can be done at the command-line via:"+
+ "</p>"+
+ "<pre class='dialog'>"+
+ "$ sudo python -c 'from IPython.external import mathjax; mathjax.install_mathjax()'"+
+ "</pre>"+
+ "<p class='dialog'>"+
+ "Or you can instruct the notebook server to start without MathJax support, with:"+
+ "<pre class='dialog'>"+
+ "</p>"+
+ "$ ipython notebook --no-mathjax"+
+ "</pre>"+
+ "<p class='dialog'>"+
+ "in which case, equations will not be rendered."+
+ "</p>"
+ ).dialog({
+ title: 'MathJax disabled',
+ width: "70%",
+ modal: true,
+ })
+ }else if (window.MathJax){
+ MathJax.Hub.Config({
+ tex2jax: {
+ inlineMath: [ ['$','$'], ["\\(","\\)"] ],
+ displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
+ },
+ displayAlign: 'left', // Change this to 'center' to center equations.
+ "HTML-CSS": {
+ styles: {'.MathJax_Display': {"margin": 0}}
+ }
+ });
+ }else{
+ // window.MathJax == null
+ // --no-mathjax mode
+ }
+
IPython.markdown_converter = new Markdown.Converter();
IPython.read_only = $('meta[name=read_only]').attr("content") == 'True';
View
6 IPython/frontend/html/notebook/static/js/textcell.js
@@ -175,7 +175,7 @@ var IPython = (function (IPython) {
var text = this.get_source();
if (text === "") { text = this.placeholder; }
this.set_rendered(text);
- MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
+ this.typeset();
this.element.find('div.text_cell_input').hide();
this.element.find("div.text_cell_render").show();
this.rendered = true;
@@ -201,7 +201,7 @@ var IPython = (function (IPython) {
if (text === "") { text = this.placeholder; }
var html = IPython.markdown_converter.makeHtml(text);
this.set_rendered(html);
- MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
+ this.typeset()
this.element.find('div.text_cell_input').hide();
this.element.find("div.text_cell_render").show();
var code_snippets = this.element.find("pre > code");
@@ -255,7 +255,7 @@ var IPython = (function (IPython) {
RSTCell.prototype.handle_render = function (data, status, xhr) {
this.set_rendered(data);
- MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
+ this.typeset();
this.rendered = true;
};
View
44 IPython/frontend/html/notebook/templates/notebook.html
@@ -6,25 +6,24 @@
<title>IPython Notebook</title>
+ {% if enable_mathjax %}
<!-- <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML" charset="utf-8"></script> -->
<script type='text/javascript' src='static/mathjax/MathJax.js?config=TeX-AMS_HTML' charset='utf-8'></script>
<script type="text/javascript">
- function CheckMathJax(){
- var div=document.getElementById("MathJaxFetchingWarning")
- if(window.MathJax){
- document.body.removeChild(div)
- }
- else{
- div.style.display = "block";
- }
- }
- if (typeof MathJax == 'undefined') {
+ if (typeof(MathJax) == 'undefined') {
console.log("No local MathJax, loading from CDN");
document.write(unescape("%3Cscript type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js%3Fconfig=TeX-AMS_HTML' charset='utf-8'%3E%3C/script%3E"));
}else{
console.log("Using local MathJax");
}
</script>
+ {% else %}
+ <script type="text/javascript">
+ // MathJax disabled, set as null to distingish from *missing* MathJax,
+ // where it will be undefined, and should prompt a dialog later.
+ window.MathJax = null;
+ </script>
+ {% end %}
<link rel="stylesheet" href="static/jquery/css/themes/aristo/jquery-wijmo.css" type="text/css" />
<link rel="stylesheet" href="static/codemirror/lib/codemirror.css">
@@ -45,7 +44,7 @@
</head>
-<body onload='CheckMathJax();'
+<body
data-project={{project}} data-notebook-id={{notebook_id}}
data-base-project-url={{base_project_url}} data-base-kernel-url={{base_kernel_url}}
>
@@ -71,29 +70,6 @@
<span id="kernel_status">Idle</span>
</div>
-<div id="MathJaxFetchingWarning"
- style="width:80%; margin:auto;padding-top:20%;text-align: justify; display:none">
- <p style="font-size:26px;">There was an issue trying to fetch MathJax.js
- from the internet.</p>
-
- <p style="padding:0.2em"> With a working internet connection, you can run
- the following at a Python or IPython prompt, which will install a local
- copy of MathJax:</p>
-
- <pre style="background-color:lightblue;border:thin silver solid;padding:0.4em">
- from IPython.external import mathjax; mathjax.install_mathjax()
- </pre>
- This will try to install MathJax into the directory where you installed
- IPython. If you installed IPython to a location that requires
- administrative privileges to write, you will need to make this call as
- an administrator. On OSX/Linux/Unix, this can be done at the
- command-line via:
- <pre style="background-color:lightblue;border:thin silver solid;padding:0.4em">
- sudo python -c "from IPython.external import mathjax; mathjax.install_mathjax()"
- </pre>
- </p>
-</div>
-
<div id="main_app">
<div id="left_panel">

0 comments on commit ef48cc0

Please sign in to comment.
Something went wrong with that request. Please try again.