Show More
@@ -17,7 +17,7 Authors: | |||
|
17 | 17 | # Imports |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 |
from |
|
|
20 | from io import BytesIO | |
|
21 | 21 | from base64 import encodestring |
|
22 | 22 | import os |
|
23 | 23 | import tempfile |
@@ -27,6 +27,7 import subprocess | |||
|
27 | 27 | from IPython.utils.process import find_cmd, FindCmdError |
|
28 | 28 | from IPython.config.configurable import SingletonConfigurable |
|
29 | 29 | from IPython.utils.traitlets import Instance, List, CBool, CUnicode |
|
30 | from IPython.utils.py3compat import bytes_to_str | |
|
30 | 31 | |
|
31 | 32 | #----------------------------------------------------------------------------- |
|
32 | 33 | # Tools |
@@ -111,7 +112,7 def latex_to_png_mpl(s, wrap): | |||
|
111 | 112 | if wrap: |
|
112 | 113 | s = '${0}$'.format(s) |
|
113 | 114 | mt = mathtext.MathTextParser('bitmap') |
|
114 |
f = |
|
|
115 | f = BytesIO() | |
|
115 | 116 | mt.to_png(f, s, fontsize=12) |
|
116 | 117 | return f.getvalue() |
|
117 | 118 | |
@@ -140,7 +141,7 def latex_to_png_dvipng(s, wrap): | |||
|
140 | 141 | "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir, |
|
141 | 142 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
|
142 | 143 | |
|
143 | with open(outfile) as f: | |
|
144 | with open(outfile, "rb") as f: | |
|
144 | 145 | bin_data = f.read() |
|
145 | 146 | finally: |
|
146 | 147 | shutil.rmtree(workdir) |
@@ -197,7 +198,7 def latex_to_html(s, alt='image'): | |||
|
197 | 198 | alt : str |
|
198 | 199 | The alt text to use for the HTML. |
|
199 | 200 | """ |
|
200 | base64_data = latex_to_png(s, encode=True) | |
|
201 | base64_data = bytes_to_str(latex_to_png(s, encode=True), 'ascii') | |
|
201 | 202 | if base64_data: |
|
202 | 203 | return _data_uri_template_png % (base64_data, alt) |
|
203 | 204 |
@@ -11,7 +11,7 | |||
|
11 | 11 | import nose.tools as nt |
|
12 | 12 | |
|
13 | 13 | from IPython.lib import latextools |
|
14 | from IPython.testing.decorators import onlyif_cmds_exist | |
|
14 | from IPython.testing.decorators import onlyif_cmds_exist, skipif_not_matplotlib | |
|
15 | 15 | from IPython.testing.tools import monkeypatch |
|
16 | 16 | from IPython.utils.process import FindCmdError |
|
17 | 17 | |
@@ -49,6 +49,26 def test_latex_to_png_dvipng_runs(): | |||
|
49 | 49 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): |
|
50 | 50 | yield (latextools.latex_to_png_dvipng, s, wrap) |
|
51 | 51 | |
|
52 | @skipif_not_matplotlib | |
|
53 | def test_latex_to_png_mpl_runs(): | |
|
54 | """ | |
|
55 | Test that latex_to_png_mpl just runs without error. | |
|
56 | """ | |
|
57 | def mock_kpsewhich(filename): | |
|
58 | nt.assert_equals(filename, "breqn.sty") | |
|
59 | return None | |
|
60 | ||
|
61 | for (s, wrap) in [("$x^2$", False), ("x^2", True)]: | |
|
62 | yield (latextools.latex_to_png_mpl, s, wrap) | |
|
63 | ||
|
64 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): | |
|
65 | yield (latextools.latex_to_png_mpl, s, wrap) | |
|
66 | ||
|
67 | @skipif_not_matplotlib | |
|
68 | def test_latex_to_html(): | |
|
69 | img = latextools.latex_to_html("$x^2$") | |
|
70 | nt.assert_in("data:image/png;base64,iVBOR", img) | |
|
71 | ||
|
52 | 72 | |
|
53 | 73 | def test_genelatex_no_wrap(): |
|
54 | 74 | """ |
General Comments 0
You need to be logged in to leave comments.
Login now