sympy_printing.py
65 lines
| 2.1 KiB
| text/x-python
|
PythonLexer
Brian Granger
|
r3278 | """A print function that pretty prints sympy Basic objects. | ||
Authors: | ||||
* Brian Granger | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (C) 2008-2011 The IPython Development Team | ||||
# | ||||
# Distributed under the terms of the BSD License. The full license is in | ||||
# the file COPYING, distributed as part of this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Brian Granger
|
r3279 | from IPython.lib.latextools import latex_to_png | ||
from sympy import pretty, latex | ||||
Brian Granger
|
r3278 | |||
#----------------------------------------------------------------------------- | ||||
# Definitions of magic functions for use with IPython | ||||
#----------------------------------------------------------------------------- | ||||
def print_basic_unicode(o, p, cycle): | ||||
"""A function to pretty print sympy Basic objects.""" | ||||
if cycle: | ||||
return p.text('Basic(...)') | ||||
out = pretty(o, use_unicode=True) | ||||
if '\n' in out: | ||||
p.text(u'\n') | ||||
p.text(out) | ||||
Brian Granger
|
r3279 | def print_png(o): | ||
"""A funciton to display sympy expression using LaTex -> PNG.""" | ||||
Brian Granger
|
r3283 | s = latex(o, mode='inline') | ||
Brian Granger
|
r3285 | # mathtext does not understand certain latex flags, so we try to replace | ||
# them with suitable subs. | ||||
Brian Granger
|
r3284 | s = s.replace('\\operatorname','') | ||
Brian Granger
|
r3285 | s = s.replace('\\overline', '\\bar') | ||
Brian Granger
|
r3279 | png = latex_to_png(s, encode=True) | ||
return png | ||||
Brian Granger
|
r3278 | _loaded = False | ||
def load_ipython_extension(ip): | ||||
"""Load the extension in IPython.""" | ||||
global _loaded | ||||
if not _loaded: | ||||
plaintext_formatter = ip.display_formatter.formatters['text/plain'] | ||||
plaintext_formatter.for_type_by_name( | ||||
'sympy.core.basic', 'Basic', print_basic_unicode | ||||
) | ||||
Brian Granger
|
r3285 | plaintext_formatter.for_type_by_name( | ||
'sympy.matrices.matrices', 'Matrix', print_basic_unicode | ||||
) | ||||
Brian Granger
|
r3279 | png_formatter = ip.display_formatter.formatters['image/png'] | ||
png_formatter.for_type_by_name( | ||||
'sympy.core.basic', 'Basic', print_png | ||||
) | ||||
Brian Granger
|
r3278 | _loaded = True | ||