Show More
@@ -0,0 +1,82 b'' | |||
|
1 | """Utility function for installing MathJax javascript library into | |
|
2 | the notebook's 'static' directory, for offline use. | |
|
3 | ||
|
4 | Authors: | |
|
5 | ||
|
6 | * Min RK | |
|
7 | """ | |
|
8 | ||
|
9 | #----------------------------------------------------------------------------- | |
|
10 | # Copyright (C) 2008-2011 The IPython Development Team | |
|
11 | # | |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
|
13 | # the file COPYING, distributed as part of this software. | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | #----------------------------------------------------------------------------- | |
|
17 | # Imports | |
|
18 | #----------------------------------------------------------------------------- | |
|
19 | ||
|
20 | import os | |
|
21 | import shutil | |
|
22 | import urllib2 | |
|
23 | import tempfile | |
|
24 | import tarfile | |
|
25 | ||
|
26 | from IPython.frontend.html import notebook as nbmod | |
|
27 | ||
|
28 | #----------------------------------------------------------------------------- | |
|
29 | # Imports | |
|
30 | #----------------------------------------------------------------------------- | |
|
31 | ||
|
32 | def install_mathjax(tag='v1.1', replace=False): | |
|
33 | """Download and install MathJax for offline use. | |
|
34 | ||
|
35 | This will install mathjax to the 'static' dir in the IPython notebook | |
|
36 | package, so it will fail if the caller does not have write access | |
|
37 | to that location. | |
|
38 | ||
|
39 | MathJax is a ~15MB download, and ~150MB installed. | |
|
40 | ||
|
41 | Parameters | |
|
42 | ---------- | |
|
43 | ||
|
44 | replace : bool [False] | |
|
45 | Whether to remove and replace an existing install. | |
|
46 | tag : str ['v1.1'] | |
|
47 | Which tag to download. Default is 'v1.1', the current stable release, | |
|
48 | but alternatives include 'v1.1a' and 'master'. | |
|
49 | """ | |
|
50 | mathjax_url = "https://github.com/mathjax/MathJax/tarball/%s"%tag | |
|
51 | ||
|
52 | nbdir = os.path.dirname(os.path.abspath(nbmod.__file__)) | |
|
53 | static = os.path.join(nbdir, 'static') | |
|
54 | dest = os.path.join(static, 'mathjax') | |
|
55 | ||
|
56 | # check for existence and permissions | |
|
57 | if not os.access(static, os.W_OK): | |
|
58 | raise IOError("Need have write access to %s"%static) | |
|
59 | if os.path.exists(dest): | |
|
60 | if replace: | |
|
61 | if not os.access(dest, os.W_OK): | |
|
62 | raise IOError("Need have write access to %s"%dest) | |
|
63 | print "removing previous MathJax install" | |
|
64 | shutil.rmtree(dest) | |
|
65 | else: | |
|
66 | print "offline MathJax apparently already installed" | |
|
67 | return | |
|
68 | ||
|
69 | # download mathjax | |
|
70 | print "Downloading mathjax source..." | |
|
71 | response = urllib2.urlopen(mathjax_url) | |
|
72 | print "done" | |
|
73 | # use 'r|gz' stream mode, because socket file-like objects can't seek: | |
|
74 | tar = tarfile.open(fileobj=response.fp, mode='r|gz') | |
|
75 | topdir = tar.firstmember.path | |
|
76 | print "Extracting to %s"%dest | |
|
77 | tar.extractall(static) | |
|
78 | # it will be mathjax-MathJax-<sha>, rename to just mathjax | |
|
79 | os.rename(os.path.join(static, topdir), dest) | |
|
80 | ||
|
81 | ||
|
82 | __all__ = ['install_mathjax'] |
General Comments 0
You need to be logged in to leave comments.
Login now