preview.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import os
  2. from os.path import join
  3. import shutil
  4. import tempfile
  5. from pathlib import Path
  6. try:
  7. from subprocess import STDOUT, CalledProcessError, check_output
  8. except ImportError:
  9. pass
  10. from sympy.utilities.decorator import doctest_depends_on
  11. from sympy.utilities.misc import debug
  12. from .latex import latex
  13. __doctest_requires__ = {('preview',): ['pyglet']}
  14. def _check_output_no_window(*args, **kwargs):
  15. # Avoid showing a cmd.exe window when running this
  16. # on Windows
  17. if os.name == 'nt':
  18. creation_flag = 0x08000000 # CREATE_NO_WINDOW
  19. else:
  20. creation_flag = 0 # Default value
  21. return check_output(*args, creationflags=creation_flag, **kwargs)
  22. def system_default_viewer(fname, fmt):
  23. """ Open fname with the default system viewer.
  24. In practice, it is impossible for python to know when the system viewer is
  25. done. For this reason, we ensure the passed file will not be deleted under
  26. it, and this function does not attempt to block.
  27. """
  28. # copy to a new temporary file that will not be deleted
  29. with tempfile.NamedTemporaryFile(prefix='sympy-preview-',
  30. suffix=os.path.splitext(fname)[1],
  31. delete=False) as temp_f:
  32. with open(fname, 'rb') as f:
  33. shutil.copyfileobj(f, temp_f)
  34. import platform
  35. if platform.system() == 'Darwin':
  36. import subprocess
  37. subprocess.call(('open', temp_f.name))
  38. elif platform.system() == 'Windows':
  39. os.startfile(temp_f.name)
  40. else:
  41. import subprocess
  42. subprocess.call(('xdg-open', temp_f.name))
  43. def pyglet_viewer(fname, fmt):
  44. try:
  45. from pyglet import window, image, gl
  46. from pyglet.window import key
  47. from pyglet.image.codecs import ImageDecodeException
  48. except ImportError:
  49. raise ImportError("pyglet is required for preview.\n visit https://pyglet.org/")
  50. try:
  51. img = image.load(fname)
  52. except ImageDecodeException:
  53. raise ValueError("pyglet preview does not work for '{}' files.".format(fmt))
  54. offset = 25
  55. config = gl.Config(double_buffer=False)
  56. win = window.Window(
  57. width=img.width + 2*offset,
  58. height=img.height + 2*offset,
  59. caption="SymPy",
  60. resizable=False,
  61. config=config
  62. )
  63. win.set_vsync(False)
  64. try:
  65. def on_close():
  66. win.has_exit = True
  67. win.on_close = on_close
  68. def on_key_press(symbol, modifiers):
  69. if symbol in [key.Q, key.ESCAPE]:
  70. on_close()
  71. win.on_key_press = on_key_press
  72. def on_expose():
  73. gl.glClearColor(1.0, 1.0, 1.0, 1.0)
  74. gl.glClear(gl.GL_COLOR_BUFFER_BIT)
  75. img.blit(
  76. (win.width - img.width) / 2,
  77. (win.height - img.height) / 2
  78. )
  79. win.on_expose = on_expose
  80. while not win.has_exit:
  81. win.dispatch_events()
  82. win.flip()
  83. except KeyboardInterrupt:
  84. pass
  85. win.close()
  86. def _get_latex_main(expr, *, preamble=None, packages=(), extra_preamble=None,
  87. euler=True, fontsize=None, **latex_settings):
  88. """
  89. Generate string of a LaTeX document rendering ``expr``.
  90. """
  91. if preamble is None:
  92. actual_packages = packages + ("amsmath", "amsfonts")
  93. if euler:
  94. actual_packages += ("euler",)
  95. package_includes = "\n" + "\n".join(["\\usepackage{%s}" % p
  96. for p in actual_packages])
  97. if extra_preamble:
  98. package_includes += extra_preamble
  99. if not fontsize:
  100. fontsize = "12pt"
  101. elif isinstance(fontsize, int):
  102. fontsize = "{}pt".format(fontsize)
  103. preamble = r"""\documentclass[varwidth,%s]{standalone}
  104. %s
  105. \begin{document}
  106. """ % (fontsize, package_includes)
  107. else:
  108. if packages or extra_preamble:
  109. raise ValueError("The \"packages\" or \"extra_preamble\" keywords"
  110. "must not be set if a "
  111. "custom LaTeX preamble was specified")
  112. if isinstance(expr, str):
  113. latex_string = expr
  114. else:
  115. latex_string = ('$\\displaystyle ' +
  116. latex(expr, mode='plain', **latex_settings) +
  117. '$')
  118. return preamble + '\n' + latex_string + '\n\n' + r"\end{document}"
  119. @doctest_depends_on(exe=('latex', 'dvipng'), modules=('pyglet',),
  120. disable_viewers=('evince', 'gimp', 'superior-dvi-viewer'))
  121. def preview(expr, output='png', viewer=None, euler=True, packages=(),
  122. filename=None, outputbuffer=None, preamble=None, dvioptions=None,
  123. outputTexFile=None, extra_preamble=None, fontsize=None,
  124. **latex_settings):
  125. r"""
  126. View expression or LaTeX markup in PNG, DVI, PostScript or PDF form.
  127. If the expr argument is an expression, it will be exported to LaTeX and
  128. then compiled using the available TeX distribution. The first argument,
  129. 'expr', may also be a LaTeX string. The function will then run the
  130. appropriate viewer for the given output format or use the user defined
  131. one. By default png output is generated.
  132. By default pretty Euler fonts are used for typesetting (they were used to
  133. typeset the well known "Concrete Mathematics" book). For that to work, you
  134. need the 'eulervm.sty' LaTeX style (in Debian/Ubuntu, install the
  135. texlive-fonts-extra package). If you prefer default AMS fonts or your
  136. system lacks 'eulervm' LaTeX package then unset the 'euler' keyword
  137. argument.
  138. To use viewer auto-detection, lets say for 'png' output, issue
  139. >>> from sympy import symbols, preview, Symbol
  140. >>> x, y = symbols("x,y")
  141. >>> preview(x + y, output='png')
  142. This will choose 'pyglet' by default. To select a different one, do
  143. >>> preview(x + y, output='png', viewer='gimp')
  144. The 'png' format is considered special. For all other formats the rules
  145. are slightly different. As an example we will take 'dvi' output format. If
  146. you would run
  147. >>> preview(x + y, output='dvi')
  148. then 'view' will look for available 'dvi' viewers on your system
  149. (predefined in the function, so it will try evince, first, then kdvi and
  150. xdvi). If nothing is found, it will fall back to using a system file
  151. association (via ``open`` and ``xdg-open``). To always use your system file
  152. association without searching for the above readers, use
  153. >>> from sympy.printing.preview import system_default_viewer
  154. >>> preview(x + y, output='dvi', viewer=system_default_viewer)
  155. If this still does not find the viewer you want, it can be set explicitly.
  156. >>> preview(x + y, output='dvi', viewer='superior-dvi-viewer')
  157. This will skip auto-detection and will run user specified
  158. 'superior-dvi-viewer'. If ``view`` fails to find it on your system it will
  159. gracefully raise an exception.
  160. You may also enter ``'file'`` for the viewer argument. Doing so will cause
  161. this function to return a file object in read-only mode, if ``filename``
  162. is unset. However, if it was set, then 'preview' writes the generated
  163. file to this filename instead.
  164. There is also support for writing to a ``io.BytesIO`` like object, which
  165. needs to be passed to the ``outputbuffer`` argument.
  166. >>> from io import BytesIO
  167. >>> obj = BytesIO()
  168. >>> preview(x + y, output='png', viewer='BytesIO',
  169. ... outputbuffer=obj)
  170. The LaTeX preamble can be customized by setting the 'preamble' keyword
  171. argument. This can be used, e.g., to set a different font size, use a
  172. custom documentclass or import certain set of LaTeX packages.
  173. >>> preamble = "\\documentclass[10pt]{article}\n" \
  174. ... "\\usepackage{amsmath,amsfonts}\\begin{document}"
  175. >>> preview(x + y, output='png', preamble=preamble)
  176. It is also possible to use the standard preamble and provide additional
  177. information to the preamble using the ``extra_preamble`` keyword argument.
  178. >>> from sympy import sin
  179. >>> extra_preamble = "\\renewcommand{\\sin}{\\cos}"
  180. >>> preview(sin(x), output='png', extra_preamble=extra_preamble)
  181. If the value of 'output' is different from 'dvi' then command line
  182. options can be set ('dvioptions' argument) for the execution of the
  183. 'dvi'+output conversion tool. These options have to be in the form of a
  184. list of strings (see ``subprocess.Popen``).
  185. Additional keyword args will be passed to the :func:`~sympy.printing.latex.latex` call,
  186. e.g., the ``symbol_names`` flag.
  187. >>> phidd = Symbol('phidd')
  188. >>> preview(phidd, symbol_names={phidd: r'\ddot{\varphi}'})
  189. For post-processing the generated TeX File can be written to a file by
  190. passing the desired filename to the 'outputTexFile' keyword
  191. argument. To write the TeX code to a file named
  192. ``"sample.tex"`` and run the default png viewer to display the resulting
  193. bitmap, do
  194. >>> preview(x + y, outputTexFile="sample.tex")
  195. """
  196. # pyglet is the default for png
  197. if viewer is None and output == "png":
  198. try:
  199. import pyglet # noqa: F401
  200. except ImportError:
  201. pass
  202. else:
  203. viewer = pyglet_viewer
  204. # look up a known application
  205. if viewer is None:
  206. # sorted in order from most pretty to most ugly
  207. # very discussable, but indeed 'gv' looks awful :)
  208. candidates = {
  209. "dvi": [ "evince", "okular", "kdvi", "xdvi" ],
  210. "ps": [ "evince", "okular", "gsview", "gv" ],
  211. "pdf": [ "evince", "okular", "kpdf", "acroread", "xpdf", "gv" ],
  212. }
  213. for candidate in candidates.get(output, []):
  214. path = shutil.which(candidate)
  215. if path is not None:
  216. viewer = path
  217. break
  218. # otherwise, use the system default for file association
  219. if viewer is None:
  220. viewer = system_default_viewer
  221. if viewer == "file":
  222. if filename is None:
  223. raise ValueError("filename has to be specified if viewer=\"file\"")
  224. elif viewer == "BytesIO":
  225. if outputbuffer is None:
  226. raise ValueError("outputbuffer has to be a BytesIO "
  227. "compatible object if viewer=\"BytesIO\"")
  228. elif not callable(viewer) and not shutil.which(viewer):
  229. raise OSError("Unrecognized viewer: %s" % viewer)
  230. latex_main = _get_latex_main(expr, preamble=preamble, packages=packages,
  231. euler=euler, extra_preamble=extra_preamble,
  232. fontsize=fontsize, **latex_settings)
  233. debug("Latex code:")
  234. debug(latex_main)
  235. with tempfile.TemporaryDirectory() as workdir:
  236. Path(join(workdir, 'texput.tex')).write_text(latex_main, encoding='utf-8')
  237. if outputTexFile is not None:
  238. shutil.copyfile(join(workdir, 'texput.tex'), outputTexFile)
  239. if not shutil.which('latex'):
  240. raise RuntimeError("latex program is not installed")
  241. try:
  242. _check_output_no_window(
  243. ['latex', '-halt-on-error', '-interaction=nonstopmode',
  244. 'texput.tex'],
  245. cwd=workdir,
  246. stderr=STDOUT)
  247. except CalledProcessError as e:
  248. raise RuntimeError(
  249. "'latex' exited abnormally with the following output:\n%s" %
  250. e.output)
  251. src = "texput.%s" % (output)
  252. if output != "dvi":
  253. # in order of preference
  254. commandnames = {
  255. "ps": ["dvips"],
  256. "pdf": ["dvipdfmx", "dvipdfm", "dvipdf"],
  257. "png": ["dvipng"],
  258. "svg": ["dvisvgm"],
  259. }
  260. try:
  261. cmd_variants = commandnames[output]
  262. except KeyError:
  263. raise ValueError("Invalid output format: %s" % output) from None
  264. # find an appropriate command
  265. for cmd_variant in cmd_variants:
  266. cmd_path = shutil.which(cmd_variant)
  267. if cmd_path:
  268. cmd = [cmd_path]
  269. break
  270. else:
  271. if len(cmd_variants) > 1:
  272. raise RuntimeError("None of %s are installed" % ", ".join(cmd_variants))
  273. else:
  274. raise RuntimeError("%s is not installed" % cmd_variants[0])
  275. defaultoptions = {
  276. "dvipng": ["-T", "tight", "-z", "9", "--truecolor"],
  277. "dvisvgm": ["--no-fonts"],
  278. }
  279. commandend = {
  280. "dvips": ["-o", src, "texput.dvi"],
  281. "dvipdf": ["texput.dvi", src],
  282. "dvipdfm": ["-o", src, "texput.dvi"],
  283. "dvipdfmx": ["-o", src, "texput.dvi"],
  284. "dvipng": ["-o", src, "texput.dvi"],
  285. "dvisvgm": ["-o", src, "texput.dvi"],
  286. }
  287. if dvioptions is not None:
  288. cmd.extend(dvioptions)
  289. else:
  290. cmd.extend(defaultoptions.get(cmd_variant, []))
  291. cmd.extend(commandend[cmd_variant])
  292. try:
  293. _check_output_no_window(cmd, cwd=workdir, stderr=STDOUT)
  294. except CalledProcessError as e:
  295. raise RuntimeError(
  296. "'%s' exited abnormally with the following output:\n%s" %
  297. (' '.join(cmd), e.output))
  298. if viewer == "file":
  299. shutil.move(join(workdir, src), filename)
  300. elif viewer == "BytesIO":
  301. s = Path(join(workdir, src)).read_bytes()
  302. outputbuffer.write(s)
  303. elif callable(viewer):
  304. viewer(join(workdir, src), fmt=output)
  305. else:
  306. try:
  307. _check_output_no_window(
  308. [viewer, src], cwd=workdir, stderr=STDOUT)
  309. except CalledProcessError as e:
  310. raise RuntimeError(
  311. "'%s %s' exited abnormally with the following output:\n%s" %
  312. (viewer, src, e.output))