##// END OF EJS Templates
latex_to_png returns None when the backend cannot be used...
Takafumi Arakaki -
Show More
@@ -41,6 +41,8 b" def latex_to_png(s, encode=False, backend='mpl'):"
41 41 backend : {mpl, dvipng}
42 42 Backend for producing PNG data.
43 43
44 None is returned when the backend cannot be used.
45
44 46 """
45 47 if backend == 'mpl':
46 48 f = latex_to_png_mpl
@@ -49,13 +51,16 b" def latex_to_png(s, encode=False, backend='mpl'):"
49 51 else:
50 52 raise ValueError('No such backend {0}'.format(backend))
51 53 bin_data = f(s)
52 if encode:
54 if encode and bin_data:
53 55 bin_data = encodestring(bin_data)
54 56 return bin_data
55 57
56 58
57 59 def latex_to_png_mpl(s):
58 from matplotlib import mathtext
60 try:
61 from matplotlib import mathtext
62 except ImportError:
63 return None
59 64
60 65 mt = mathtext.MathTextParser('bitmap')
61 66 f = StringIO()
@@ -84,12 +89,13 b' def latex_to_png_dvipng(s):'
84 89
85 90 with open(outfile) as f:
86 91 bin_data = f.read()
92 except subprocess.CalledProcessError:
93 bin_data = None
87 94 finally:
88 95 shutil.rmtree(workdir)
89 96 return bin_data
90 97
91 98
92
93 99 _latex_header = r'''
94 100 \documentclass{article}
95 101 \usepackage{amsmath}
@@ -116,7 +122,8 b" def latex_to_html(s, alt='image'):"
116 122 The alt text to use for the HTML.
117 123 """
118 124 base64_data = latex_to_png(s, encode=True)
119 return _data_uri_template_png % (base64_data, alt)
125 if base64_data:
126 return _data_uri_template_png % (base64_data, alt)
120 127
121 128
122 129 # From matplotlib, thanks to mdboom. Once this is in matplotlib releases, we
General Comments 0
You need to be logged in to leave comments. Login now