Show More
@@ -1,72 +1,70 | |||||
1 | """A print function that pretty prints sympy Basic objects. |
|
1 | """A print function that pretty prints sympy Basic objects. | |
2 |
|
2 | |||
3 | Authors: |
|
3 | Authors: | |
4 | * Brian Granger |
|
4 | * Brian Granger | |
5 | """ |
|
5 | """ | |
6 | #----------------------------------------------------------------------------- |
|
6 | #----------------------------------------------------------------------------- | |
7 | # Copyright (C) 2008-2011 The IPython Development Team |
|
7 | # Copyright (C) 2008-2011 The IPython Development Team | |
8 | # |
|
8 | # | |
9 | # Distributed under the terms of the BSD License. The full license is in |
|
9 | # Distributed under the terms of the BSD License. The full license is in | |
10 | # the file COPYING, distributed as part of this software. |
|
10 | # the file COPYING, distributed as part of this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | from IPython.lib.latextools import latex_to_png |
|
17 | from IPython.lib.latextools import latex_to_png | |
18 | from IPython.testing import decorators as dec |
|
18 | from IPython.testing import decorators as dec | |
|
19 | # use @dec.skipif_not_sympy to skip tests requiring sympy | |||
19 |
|
20 | |||
20 | try: |
|
21 | try: | |
21 | from sympy import pretty, latex |
|
22 | from sympy import pretty, latex | |
22 | except ImportError: |
|
23 | except ImportError: | |
23 | pass |
|
24 | pass | |
24 |
|
25 | |||
25 |
|
26 | |||
26 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
27 | # Definitions of magic functions for use with IPython |
|
28 | # Definitions of magic functions for use with IPython | |
28 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
29 |
|
30 | |||
30 | @dec.skipif_not_sympy |
|
|||
31 | def print_basic_unicode(o, p, cycle): |
|
31 | def print_basic_unicode(o, p, cycle): | |
32 | """A function to pretty print sympy Basic objects.""" |
|
32 | """A function to pretty print sympy Basic objects.""" | |
33 | if cycle: |
|
33 | if cycle: | |
34 | return p.text('Basic(...)') |
|
34 | return p.text('Basic(...)') | |
35 | out = pretty(o, use_unicode=True) |
|
35 | out = pretty(o, use_unicode=True) | |
36 | if '\n' in out: |
|
36 | if '\n' in out: | |
37 | p.text(u'\n') |
|
37 | p.text(u'\n') | |
38 | p.text(out) |
|
38 | p.text(out) | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | @dec.skipif_not_sympy |
|
|||
42 | def print_png(o): |
|
41 | def print_png(o): | |
43 | """A funciton to display sympy expression using LaTex -> PNG.""" |
|
42 | """A funciton to display sympy expression using LaTex -> PNG.""" | |
44 | s = latex(o, mode='inline') |
|
43 | s = latex(o, mode='inline') | |
45 | # mathtext does not understand certain latex flags, so we try to replace |
|
44 | # mathtext does not understand certain latex flags, so we try to replace | |
46 | # them with suitable subs. |
|
45 | # them with suitable subs. | |
47 | s = s.replace('\\operatorname','') |
|
46 | s = s.replace('\\operatorname','') | |
48 | s = s.replace('\\overline', '\\bar') |
|
47 | s = s.replace('\\overline', '\\bar') | |
49 | png = latex_to_png(s, encode=True) |
|
48 | png = latex_to_png(s, encode=True) | |
50 | return png |
|
49 | return png | |
51 |
|
50 | |||
52 | _loaded = False |
|
51 | _loaded = False | |
53 |
|
52 | |||
54 | @dec.skipif_not_sympy |
|
|||
55 | def load_ipython_extension(ip): |
|
53 | def load_ipython_extension(ip): | |
56 | """Load the extension in IPython.""" |
|
54 | """Load the extension in IPython.""" | |
57 | global _loaded |
|
55 | global _loaded | |
58 | if not _loaded: |
|
56 | if not _loaded: | |
59 | plaintext_formatter = ip.display_formatter.formatters['text/plain'] |
|
57 | plaintext_formatter = ip.display_formatter.formatters['text/plain'] | |
60 | plaintext_formatter.for_type_by_name( |
|
58 | plaintext_formatter.for_type_by_name( | |
61 | 'sympy.core.basic', 'Basic', print_basic_unicode |
|
59 | 'sympy.core.basic', 'Basic', print_basic_unicode | |
62 | ) |
|
60 | ) | |
63 | plaintext_formatter.for_type_by_name( |
|
61 | plaintext_formatter.for_type_by_name( | |
64 | 'sympy.matrices.matrices', 'Matrix', print_basic_unicode |
|
62 | 'sympy.matrices.matrices', 'Matrix', print_basic_unicode | |
65 | ) |
|
63 | ) | |
66 |
|
64 | |||
67 | png_formatter = ip.display_formatter.formatters['image/png'] |
|
65 | png_formatter = ip.display_formatter.formatters['image/png'] | |
68 | png_formatter.for_type_by_name( |
|
66 | png_formatter.for_type_by_name( | |
69 | 'sympy.core.basic', 'Basic', print_png |
|
67 | 'sympy.core.basic', 'Basic', print_png | |
70 | ) |
|
68 | ) | |
71 | _loaded = True |
|
69 | _loaded = True | |
72 |
|
70 |
General Comments 0
You need to be logged in to leave comments.
Login now