From 0040446881df83702425c75fe2ac398ffcbe349e 2011-12-06 00:49:04 From: Min RK Date: 2011-12-06 00:49:04 Subject: [PATCH] Merge pull request #1077 from minrk/nb allow the notebook to run without MathJax adds --no-mathjax flag for disabling mathjax, and moves the mathjax URL decision to the server from the browser. closes #1071 --- diff --git a/IPython/frontend/html/notebook/handlers.py b/IPython/frontend/html/notebook/handlers.py index ca92d59..242d47b 100644 --- a/IPython/frontend/html/notebook/handlers.py +++ b/IPython/frontend/html/notebook/handlers.py @@ -219,6 +219,7 @@ class NewHandler(AuthenticatedHandler): base_project_url=u'/', base_kernel_url=u'/', kill_kernel=False, read_only=False, + mathjax_url=self.application.ipython_app.mathjax_url, ) @@ -237,6 +238,7 @@ class NamedNotebookHandler(AuthenticatedHandler): base_project_url=u'/', base_kernel_url=u'/', kill_kernel=False, read_only=self.read_only, + mathjax_url=self.application.ipython_app.mathjax_url, ) diff --git a/IPython/frontend/html/notebook/notebookapp.py b/IPython/frontend/html/notebook/notebookapp.py index 1d17959..d2bdcf6 100644 --- a/IPython/frontend/html/notebook/notebookapp.py +++ b/IPython/frontend/html/notebook/notebookapp.py @@ -128,6 +128,17 @@ flags['no-browser']=( {'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 @@ flags['read-only'] = ( # 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,42 @@ class NotebookApp(BaseIPythonApplication): 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 _enable_mathjax_changed(self, name, old, new): + """set mathjax url to empty if mathjax is disabled""" + if not new: + self.mathjax_url = u'' + + mathjax_url = Unicode("", config=True, + help="""The url for MathJax.js.""" + ) + def _mathjax_url_default(self): + if not self.enable_mathjax: + return u'' + static_path = os.path.join(os.path.dirname(__file__), "static") + if os.path.exists(os.path.join(static_path, 'mathjax', "MathJax.js")): + self.log.info("Using local MathJax") + return u"static/mathjax/MathJax.js" + else: + self.log.info("Using MathJax from CDN") + return u"http://cdn.mathjax.org/mathjax/latest/MathJax.js" + + def _mathjax_url_changed(self, name, old, new): + if new and not self.enable_mathjax: + # enable_mathjax=False overrides mathjax_url + self.mathjax_url = u'' + else: + self.log.info("Using MathJax: %s", new) def parse_command_line(self, argv=None): super(NotebookApp, self).parse_command_line(argv) diff --git a/IPython/frontend/html/notebook/static/css/notebook.css b/IPython/frontend/html/notebook/static/css/notebook.css index 58cb6dd..d7ea21d 100644 --- a/IPython/frontend/html/notebook/static/css/notebook.css +++ b/IPython/frontend/html/notebook/static/css/notebook.css @@ -428,6 +428,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; } diff --git a/IPython/frontend/html/notebook/static/js/cell.js b/IPython/frontend/html/notebook/static/js/cell.js index 6806dd3..2667ec0 100644 --- a/IPython/frontend/html/notebook/static/js/cell.js +++ b/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; diff --git a/IPython/frontend/html/notebook/static/js/codecell.js b/IPython/frontend/html/notebook/static/js/codecell.js index 0e4c68c..f8ac38f 100644 --- a/IPython/frontend/html/notebook/static/js/codecell.js +++ b/IPython/frontend/html/notebook/static/js/codecell.js @@ -522,7 +522,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(); }; }; @@ -574,7 +574,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(); }; }; diff --git a/IPython/frontend/html/notebook/static/js/notebook.js b/IPython/frontend/html/notebook/static/js/notebook.js index d8b2876..1b973b2 100644 --- a/IPython/frontend/html/notebook/static/js/notebook.js +++ b/IPython/frontend/html/notebook/static/js/notebook.js @@ -59,7 +59,7 @@ var IPython = (function (IPython) { var that = this; $(document).keydown(function (event) { // console.log(event); - if (that.read_only) return false; + if (that.read_only) return true; if (event.which === 27) { // Intercept escape at highest level to avoid closing // websocket connection with firefox diff --git a/IPython/frontend/html/notebook/static/js/notebookmain.js b/IPython/frontend/html/notebook/static/js/notebookmain.js index c3cb70a..8326b68 100644 --- a/IPython/frontend/html/notebook/static/js/notebookmain.js +++ b/IPython/frontend/html/notebook/static/js/notebookmain.js @@ -11,17 +11,67 @@ $(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){ + // MathJax loaded + MathJax.Hub.Config({ + tex2jax: { + inlineMath: [ ['$','$'], ["\\(","\\)"] ], + displayMath: [ ['$$','$$'], ["\\[","\\]"] ] + }, + displayAlign: 'left', // Change this to 'center' to center equations. + "HTML-CSS": { + styles: {'.MathJax_Display': {"margin": 0}} + } + }); + }else if (window.mathjax_url != ""){ + // Don't have MathJax, but should. Show dialog. + var dialog = $('
') + .append( + $("

").addClass('dialog').html( + "Math/LaTeX rendering will be disabled." + ) + ).append( + $("

").addClass('dialog').html( + "If you have administrative access to the notebook server and" + + " a working internet connection, you can install a local copy" + + " of MathJax for offline use with the following command on the server" + + " at a Python or IPython prompt:" + ) + ).append( + $("
").addClass('dialog').html(
+                    ">>> from IPython.external import mathjax; mathjax.install_mathjax()"
+                )
+            ).append(
+                $("

").addClass('dialog').html( + "This will try to install MathJax into the IPython source directory." + ) + ).append( + $("

").addClass('dialog').html( + "If IPython is installed to a location that requires" + + " administrative privileges to write, you will need to make this call as" + + " an administrator, via 'sudo'." + ) + ).append( + $("

").addClass('dialog').html( + "When you start the notebook server, you can instruct it to disable MathJax support altogether:" + ) + ).append( + $("
").addClass('dialog').html(
+                    "$ ipython notebook --no-mathjax"
+                )
+            ).append(
+                $("

").addClass('dialog').html( + "which will prevent this dialog from appearing." + ) + ).dialog({ + title: "Failed to retrieve MathJax from '" + window.mathjax_url + "'", + width: "70%", + modal: true, + }) + }else{ + // No MathJax, but none expected. No dialog. + } + IPython.markdown_converter = new Markdown.Converter(); IPython.read_only = $('meta[name=read_only]').attr("content") == 'True'; diff --git a/IPython/frontend/html/notebook/static/js/textcell.js b/IPython/frontend/html/notebook/static/js/textcell.js index 55a9887..601de0f 100644 --- a/IPython/frontend/html/notebook/static/js/textcell.js +++ b/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; }; diff --git a/IPython/frontend/html/notebook/templates/notebook.html b/IPython/frontend/html/notebook/templates/notebook.html index b46220f..876bcec 100644 --- a/IPython/frontend/html/notebook/templates/notebook.html +++ b/IPython/frontend/html/notebook/templates/notebook.html @@ -6,24 +6,13 @@ IPython Notebook - - + {% if mathjax_url %} + + {% end %} @@ -45,7 +34,7 @@ - @@ -71,29 +60,6 @@ Idle - -