Show More
@@ -23,18 +23,18 b' import urllib2' | |||||
23 | import tempfile |
|
23 | import tempfile | |
24 | import tarfile |
|
24 | import tarfile | |
25 |
|
25 | |||
26 | from IPython.frontend.html import notebook as nbmod |
|
26 | from IPython.utils.path import locate_profile | |
27 |
|
27 | |||
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | # Imports |
|
29 | # Imports | |
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 |
|
31 | |||
32 |
def install_mathjax(tag='v |
|
32 | def install_mathjax(tag='v2.0', replace=False, dest=None): | |
33 | """Download and install MathJax for offline use. |
|
33 | """Download and install MathJax for offline use. | |
34 |
|
34 | |||
35 | This will install mathjax to the 'static' dir in the IPython notebook |
|
35 | You can use this to install mathjax to a location on your static file | |
36 | package, so it will fail if the caller does not have write access |
|
36 | path. This includes the `static` directory within your IPython profile, | |
37 | to that location. |
|
37 | which is the default location for this install. | |
38 |
|
38 | |||
39 | MathJax is a ~15MB download, and ~150MB installed. |
|
39 | MathJax is a ~15MB download, and ~150MB installed. | |
40 |
|
40 | |||
@@ -43,23 +43,34 b" def install_mathjax(tag='v1.1', replace=False):" | |||||
43 |
|
43 | |||
44 | replace : bool [False] |
|
44 | replace : bool [False] | |
45 | Whether to remove and replace an existing install. |
|
45 | Whether to remove and replace an existing install. | |
46 |
tag : str ['v |
|
46 | tag : str ['v2.0'] | |
47 |
Which tag to download. Default is 'v |
|
47 | Which tag to download. Default is 'v2.0', the current stable release, | |
48 |
but alternatives include 'v1.1 |
|
48 | but alternatives include 'v1.1' and 'master'. | |
|
49 | dest : path | |||
|
50 | The path to the directory in which mathjax will be installed. | |||
|
51 | The default is `IPYTHONDIR/profile_default/static`. | |||
|
52 | dest must be on your notebook static_path when you run the notebook server. | |||
|
53 | The default location works for this. | |||
49 | """ |
|
54 | """ | |
50 | mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s"%tag |
|
|||
51 |
|
55 | |||
52 | nbdir = os.path.dirname(os.path.abspath(nbmod.__file__)) |
|
56 | mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s" % tag | |
53 | static = os.path.join(nbdir, 'static') |
|
57 | ||
|
58 | if dest is None: | |||
|
59 | dest = os.path.join(locate_profile('default'), 'static') | |||
|
60 | ||||
|
61 | if not os.path.exists(dest): | |||
|
62 | os.mkdir(dest) | |||
|
63 | ||||
|
64 | static = dest | |||
54 | dest = os.path.join(static, 'mathjax') |
|
65 | dest = os.path.join(static, 'mathjax') | |
55 |
|
66 | |||
56 | # check for existence and permissions |
|
67 | # check for existence and permissions | |
57 | if not os.access(static, os.W_OK): |
|
68 | if not os.access(static, os.W_OK): | |
58 | raise IOError("Need have write access to %s"%static) |
|
69 | raise IOError("Need have write access to %s" % static) | |
59 | if os.path.exists(dest): |
|
70 | if os.path.exists(dest): | |
60 | if replace: |
|
71 | if replace: | |
61 | if not os.access(dest, os.W_OK): |
|
72 | if not os.access(dest, os.W_OK): | |
62 | raise IOError("Need have write access to %s"%dest) |
|
73 | raise IOError("Need have write access to %s" % dest) | |
63 | print "removing previous MathJax install" |
|
74 | print "removing previous MathJax install" | |
64 | shutil.rmtree(dest) |
|
75 | shutil.rmtree(dest) | |
65 | else: |
|
76 | else: | |
@@ -67,13 +78,13 b" def install_mathjax(tag='v1.1', replace=False):" | |||||
67 | return |
|
78 | return | |
68 |
|
79 | |||
69 | # download mathjax |
|
80 | # download mathjax | |
70 | print "Downloading mathjax source..." |
|
81 | print "Downloading mathjax source from %s ..." % mathjax_url | |
71 | response = urllib2.urlopen(mathjax_url) |
|
82 | response = urllib2.urlopen(mathjax_url) | |
72 | print "done" |
|
83 | print "done" | |
73 | # use 'r|gz' stream mode, because socket file-like objects can't seek: |
|
84 | # use 'r|gz' stream mode, because socket file-like objects can't seek: | |
74 | tar = tarfile.open(fileobj=response.fp, mode='r|gz') |
|
85 | tar = tarfile.open(fileobj=response.fp, mode='r|gz') | |
75 | topdir = tar.firstmember.path |
|
86 | topdir = tar.firstmember.path | |
76 | print "Extracting to %s"%dest |
|
87 | print "Extracting to %s" % dest | |
77 | tar.extractall(static) |
|
88 | tar.extractall(static) | |
78 | # it will be mathjax-MathJax-<sha>, rename to just mathjax |
|
89 | # it will be mathjax-MathJax-<sha>, rename to just mathjax | |
79 | os.rename(os.path.join(static, topdir), dest) |
|
90 | os.rename(os.path.join(static, topdir), dest) |
General Comments 0
You need to be logged in to leave comments.
Login now