Show More
@@ -1,237 +1,13 b'' | |||
|
1 | 1 | #!/usr/bin/python |
|
2 | """Utility function for installing MathJax javascript library into | |
|
3 | your IPython nbextensions directory, for offline use. | |
|
4 | ||
|
5 | Authors: | |
|
6 | ||
|
7 | * Min RK | |
|
8 | * Mark Sienkiewicz | |
|
9 | * Matthias Bussonnier | |
|
10 | ||
|
11 | To download and install MathJax: | |
|
12 | ||
|
13 | From Python: | |
|
14 | ||
|
15 | >>> from IPython.external.mathjax import install_mathjax | |
|
16 | >>> install_mathjax() | |
|
17 | ||
|
18 | From the command line: | |
|
19 | ||
|
20 | $ python -m IPython.external.mathjax | |
|
21 | ||
|
22 | To a specific location: | |
|
23 | ||
|
24 | $ python -m IPython.external.mathjax -i /usr/share/ | |
|
25 | ||
|
26 | will install mathjax to /usr/share/mathjax | |
|
27 | ||
|
28 | To install MathJax from a file you have already downloaded: | |
|
29 | ||
|
30 | $ python -m IPython.external.mathjax mathjax-xxx.tar.gz | |
|
31 | $ python -m IPython.external.mathjax mathjax-xxx.zip | |
|
32 | ||
|
33 | It will not install MathJax if it is already there. Use -r to | |
|
34 | replace the existing copy of MathJax. | |
|
35 | ||
|
36 | To find the directory where IPython would like MathJax installed: | |
|
37 | ||
|
38 | $ python -m IPython.external.mathjax -d | |
|
39 | ||
|
40 | 2 |
""" |
|
41 | from __future__ import print_function | |
|
42 | ||
|
43 | ||
|
44 | #----------------------------------------------------------------------------- | |
|
45 | # Copyright (C) 2011 The IPython Development Team | |
|
46 | # | |
|
47 | # Distributed under the terms of the BSD License. The full license is in | |
|
48 | # the file COPYING, distributed as part of this software. | |
|
49 | #----------------------------------------------------------------------------- | |
|
50 | ||
|
51 | ||
|
52 | #----------------------------------------------------------------------------- | |
|
53 | # Imports | |
|
54 | #----------------------------------------------------------------------------- | |
|
55 | ||
|
56 | import argparse | |
|
57 | import os | |
|
58 | import shutil | |
|
59 | import sys | |
|
60 | import tarfile | |
|
61 | import zipfile | |
|
62 | ||
|
63 | from IPython.paths import get_ipython_dir | |
|
64 | ||
|
65 | try: | |
|
66 | from urllib.request import urlopen # Py 3 | |
|
67 | except ImportError: | |
|
68 | from urllib2 import urlopen | |
|
69 | ||
|
70 | #----------------------------------------------------------------------------- | |
|
71 | # | |
|
72 | #----------------------------------------------------------------------------- | |
|
73 | ||
|
74 | # Where mathjax will be installed | |
|
75 | ||
|
76 | nbextensions = os.path.join(get_ipython_dir(), 'nbextensions') | |
|
77 | default_dest = os.path.join(nbextensions, 'mathjax') | |
|
78 | ||
|
79 | # Test for access to install mathjax | |
|
80 | ||
|
81 | def prepare_dest(dest, replace=False): | |
|
82 | """prepare the destination folder for mathjax install | |
|
83 | ||
|
84 | Returns False if mathjax appears to already be installed and there is nothing to do, | |
|
85 | True otherwise. | |
|
86 | """ | |
|
87 | ||
|
88 | parent = os.path.abspath(os.path.join(dest, os.path.pardir)) | |
|
89 | if not os.path.exists(parent): | |
|
90 | os.makedirs(parent) | |
|
91 | ||
|
92 | if os.path.exists(dest): | |
|
93 | if replace: | |
|
94 | print("removing existing MathJax at %s" % dest) | |
|
95 | shutil.rmtree(dest) | |
|
96 | return True | |
|
97 | else: | |
|
98 | mathjax_js = os.path.join(dest, 'MathJax.js') | |
|
99 | if not os.path.exists(mathjax_js): | |
|
100 | raise IOError("%s exists, but does not contain MathJax.js" % dest) | |
|
101 | print("%s already exists" % mathjax_js) | |
|
102 | return False | |
|
103 | else: | |
|
104 | return True | |
|
3 | `IPython.external.mathjax` is deprecated with IPython 4.0+ | |
|
105 | 4 |
|
|
5 | mathjax is now install by default with the notebook package | |
|
106 | 6 |
|
|
107 | def extract_tar(fd, dest): | |
|
108 | """extract a tarball from filelike `fd` to destination `dest`""" | |
|
109 | # use 'r|gz' stream mode, because socket file-like objects can't seek: | |
|
110 | tar = tarfile.open(fileobj=fd, mode='r|gz') | |
|
111 | ||
|
112 | # The first entry in the archive is the top-level dir | |
|
113 | topdir = tar.firstmember.path | |
|
114 | ||
|
115 | # extract the archive (contains a single directory) to the destination directory | |
|
116 | parent = os.path.abspath(os.path.join(dest, os.path.pardir)) | |
|
117 | tar.extractall(parent) | |
|
118 | ||
|
119 | # it will be mathjax-MathJax-<sha>, rename to just mathjax | |
|
120 | os.rename(os.path.join(parent, topdir), dest) | |
|
121 | ||
|
122 | ||
|
123 | def extract_zip(fd, dest): | |
|
124 | """extract a zip file from filelike `fd` to destination `dest`""" | |
|
125 | z = zipfile.ZipFile(fd, 'r') | |
|
126 | ||
|
127 | # The first entry in the archive is the top-level dir | |
|
128 | topdir = z.namelist()[0] | |
|
129 | ||
|
130 | # extract the archive (contains a single directory) to the static/ directory | |
|
131 | parent = os.path.abspath(os.path.join(dest, os.path.pardir)) | |
|
132 | z.extractall(parent) | |
|
133 | ||
|
134 | # it will be mathjax-MathJax-<sha>, rename to just mathjax | |
|
135 | os.rename(os.path.join(parent, topdir), dest) | |
|
136 | ||
|
137 | ||
|
138 | def install_mathjax(tag='2.4.0', dest=default_dest, replace=False, file=None, extractor=extract_tar): | |
|
139 | """Download and/or install MathJax for offline use. | |
|
140 | ||
|
141 | This will install mathjax to the nbextensions dir in your IPYTHONDIR. | |
|
142 | ||
|
143 | MathJax is a ~15MB download, and ~150MB installed. | |
|
144 | ||
|
145 | Parameters | |
|
146 | ---------- | |
|
147 | ||
|
148 | replace : bool [False] | |
|
149 | Whether to remove and replace an existing install. | |
|
150 | dest : str [IPYTHONDIR/nbextensions/mathjax] | |
|
151 | Where to install mathjax | |
|
152 | tag : str ['2.4.0'] | |
|
153 | Which tag to download. Default is '2.4.0', the current stable release, | |
|
154 | but alternatives include 'v1.1a' and 'master'. | |
|
155 | file : file like object [ defualt to content of https://github.com/mathjax/MathJax/tarball/#{tag}] | |
|
156 | File handle from which to untar/unzip/... mathjax | |
|
157 | extractor : function | |
|
158 | Method to use to untar/unzip/... `file` | |
|
159 | """ | |
|
160 | try: | |
|
161 | anything_to_do = prepare_dest(dest, replace) | |
|
162 | except OSError as e: | |
|
163 | print("ERROR %s, require write access to %s" % (e, dest)) | |
|
164 | return 1 | |
|
165 | else: | |
|
166 | if not anything_to_do: | |
|
167 | return 0 | |
|
168 | ||
|
169 | if file is None: | |
|
170 | # download mathjax | |
|
171 | mathjax_url = "https://github.com/mathjax/MathJax/archive/%s.tar.gz" %tag | |
|
172 | print("Downloading mathjax source from %s" % mathjax_url) | |
|
173 | response = urlopen(mathjax_url) | |
|
174 | file = response.fp | |
|
175 | ||
|
176 | print("Extracting to %s" % dest) | |
|
177 | extractor(file, dest) | |
|
178 | return 0 | |
|
179 | ||
|
180 | ||
|
181 | def main(): | |
|
182 | parser = argparse.ArgumentParser( | |
|
183 | description="""Install mathjax from internet or local archive""", | |
|
184 | ) | |
|
185 | ||
|
186 | parser.add_argument( | |
|
187 | '-i', | |
|
188 | '--install-dir', | |
|
189 | default=nbextensions, | |
|
190 | help='custom installation directory. Mathjax will be installed in here/mathjax') | |
|
191 | ||
|
192 | parser.add_argument( | |
|
193 | '-d', | |
|
194 | '--print-dest', | |
|
195 | action='store_true', | |
|
196 | help='print where mathjax would be installed and exit') | |
|
197 | parser.add_argument( | |
|
198 | '-r', | |
|
199 | '--replace', | |
|
200 | action='store_true', | |
|
201 | help='Whether to replace current mathjax if it already exists') | |
|
202 | parser.add_argument('filename', | |
|
203 | help="the local tar/zip-ball filename containing mathjax", | |
|
204 | nargs='?', | |
|
205 | metavar='filename') | |
|
206 | ||
|
207 | pargs = parser.parse_args() | |
|
208 | ||
|
209 | dest = os.path.join(pargs.install_dir, 'mathjax') | |
|
210 | ||
|
211 | if pargs.print_dest: | |
|
212 | print(dest) | |
|
213 | return | |
|
214 | ||
|
215 | # remove/replace existing mathjax? | |
|
216 | replace = pargs.replace | |
|
217 | ||
|
218 | # do it | |
|
219 | if pargs.filename: | |
|
220 | fname = pargs.filename | |
|
221 | ||
|
222 | # automatically detect zip/tar - could do something based | |
|
223 | # on file content, but really not cost-effective here. | |
|
224 | if fname.endswith('.zip'): | |
|
225 | extractor = extract_zip | |
|
226 | else : | |
|
227 | extractor = extract_tar | |
|
228 | # do it | |
|
229 | return install_mathjax(file=open(fname, "rb"), replace=replace, extractor=extractor, dest=dest) | |
|
230 | else: | |
|
231 | return install_mathjax(replace=replace, dest=dest) | |
|
7 | """ | |
|
232 | 8 | |
|
9 | import sys | |
|
233 | 10 | |
|
234 | 11 | if __name__ == '__main__' : |
|
235 | sys.exit(main()) | |
|
12 | sys.exit("IPython.external.mathjax is deprecated") | |
|
236 | 13 | |
|
237 | __all__ = ['install_mathjax', 'main', 'default_dest'] |
General Comments 0
You need to be logged in to leave comments.
Login now