Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

# -*- coding: utf-8 -*- 

r""" 

IPython Displayhook Formatters 

 

The classes in this module can be used as IPython displayhook 

formatters. It has two main features, by default the displayhook 

contains a new facility for displaying lists of matrices in an easier 

to read format:: 

 

sage: [identity_matrix(i) for i in range(2,5)] 

[ 

[1 0 0 0] 

[1 0 0] [0 1 0 0] 

[1 0] [0 1 0] [0 0 1 0] 

[0 1], [0 0 1], [0 0 0 1] 

] 

 

This facility uses :meth:`_repr_` (and a simple string) to try do a nice read 

format (see :meth:`sage.structure.parent.Parent._repr_option` for details). 

 

With this displayhook there exists an other way for displaying object and more 

generally, all sage expression as an ASCII art object:: 

 

sage: from sage.repl.interpreter import get_test_shell 

sage: shell = get_test_shell() 

sage: shell.run_cell('%display ascii_art') 

sage: shell.run_cell('integral(x^2/pi^x, x)') 

/ 2 2 \ -x*log(pi) 

-\x *log (pi) + 2*x*log(pi) + 2/*e 

--------------------------------------------- 

3 

log (pi) 

sage: shell.run_cell("i = var('i')") 

sage: shell.run_cell('sum(i*x^i, i, 0, 10)') 

10 9 8 7 6 5 4 3 2 

10*x + 9*x + 8*x + 7*x + 6*x + 5*x + 4*x + 3*x + 2*x + x 

sage: shell.run_cell('StandardTableaux(4).list()') 

[ 

[ 1 4 

[ 1 3 4 1 2 4 1 2 3 1 3 1 2 2 

[ 1 2 3 4, 2 , 3 , 4 , 2 4, 3 4, 3 , 

<BLANKLINE> 

1 ] 

1 3 1 2 2 ] 

2 3 3 ] 

4 , 4 , 4 ] 

sage: shell.run_cell('%display default') 

sage: shell.quit() 

 

This other facility uses a simple 

:class:`~sage.typeset.ascii_art.AsciiArt` object (see and 

:meth:`sage.structure.sage_object.SageObject._ascii_art_`). """ 

 

#***************************************************************************** 

# Copyright (C) 2014 Volker Braun <vbraun.name@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

 

from IPython.core.formatters import DisplayFormatter, PlainTextFormatter 

from IPython.utils.py3compat import str_to_unicode, unicode_to_str 

 

from sage.structure.sage_object import SageObject 

from sage.repl.display.pretty_print import SagePrettyPrinter 

 

 

PLAIN_TEXT = u'text/plain' 

TEXT_HTML = u'text/html' 

 

 

class SageDisplayFormatter(DisplayFormatter): 

 

def __init__(self, *args, **kwds): 

""" 

This is where the Sage rich objects are translated to IPython 

 

INPUT/OUTPUT: 

 

See the IPython documentation. 

 

EXAMPLES: 

 

This is part of how Sage works with the IPython output 

system. It cannot be used in doctests:: 

 

sage: from sage.repl.display.formatter import SageDisplayFormatter 

sage: fmt = SageDisplayFormatter() 

Traceback (most recent call last): 

... 

RuntimeError: check failed: current backend is invalid 

""" 

super(SageDisplayFormatter, self).__init__(*args, **kwds) 

from sage.repl.rich_output.display_manager import get_display_manager 

self.dm = get_display_manager() 

from sage.repl.rich_output.backend_ipython import BackendIPython 

self.dm.check_backend_class(BackendIPython) 

 

def default_mime(self): 

r""" 

Return the default mime output(s) 

 

If these are the only output mime types from the Sage rich output machinery, then 

:meth:`format` will try to fall back to IPythons internal formatting. 

 

OUTPUT: 

 

List of mime type strings. Usually just text/plain, though possibly more depending on 

display manager preferences. 

 

EXAMPLES:: 

 

sage: from sage.repl.interpreter import get_test_shell 

sage: from sage.repl.rich_output.backend_ipython import BackendIPython 

sage: backend = BackendIPython() 

sage: shell = get_test_shell() 

sage: backend.install(shell=shell) 

sage: shell.run_cell('get_ipython().display_formatter.default_mime()') 

[u'text/plain'] 

sage: shell.run_cell('%display latex') # indirect doctest 

sage: shell.run_cell('get_ipython().display_formatter.default_mime()') 

\newcommand{\Bold}[1]{\mathbf{#1}}\left[\verb|text/plain|, \verb|text/html|\right] 

sage: shell.run_cell('%display default') 

sage: shell.quit() 

""" 

if self.dm.preferences.text == 'latex': 

return [PLAIN_TEXT, TEXT_HTML] 

return [PLAIN_TEXT] 

 

def format(self, obj, include=None, exclude=None): 

r""" 

Use the Sage rich output instead of IPython 

 

INPUT/OUTPUT: 

 

See the IPython documentation. 

 

EXAMPLES:: 

 

sage: [identity_matrix(i) for i in range(3,7)] 

[ 

[1 0 0 0 0 0] 

[1 0 0 0 0] [0 1 0 0 0 0] 

[1 0 0 0] [0 1 0 0 0] [0 0 1 0 0 0] 

[1 0 0] [0 1 0 0] [0 0 1 0 0] [0 0 0 1 0 0] 

[0 1 0] [0 0 1 0] [0 0 0 1 0] [0 0 0 0 1 0] 

[0 0 1], [0 0 0 1], [0 0 0 0 1], [0 0 0 0 0 1] 

] 

sage: from sage.repl.interpreter import get_test_shell 

sage: shell = get_test_shell() 

sage: shell.run_cell('%display ascii_art') # indirect doctest 

sage: shell.run_cell("i = var('i')") 

sage: shell.run_cell('sum(i*x^i, i, 0, 10)') 

10 9 8 7 6 5 4 3 2 

10*x + 9*x + 8*x + 7*x + 6*x + 5*x + 4*x + 3*x + 2*x + x 

sage: shell.run_cell('%display default') 

sage: shell.quit() 

 

TESTS:: 

 

sage: import os 

sage: from sage.env import SAGE_EXTCODE 

sage: example_png = os.path.join(SAGE_EXTCODE, 'doctest', 'rich_output', 'example.png') 

sage: from sage.repl.rich_output.backend_ipython import BackendIPython 

sage: backend = BackendIPython() 

sage: shell = get_test_shell() 

sage: backend.install(shell=shell) 

sage: shell.run_cell('get_ipython().display_formatter') 

<sage.repl.display.formatter.SageDisplayFormatter object at 0x...> 

sage: shell.run_cell('from IPython.display import Image') 

sage: shell.run_cell('ipython_image = Image("{0}")'.format(example_png)) 

sage: shell.run_cell('ipython_image') 

<IPython.core.display.Image object> 

sage: shell.run_cell('get_ipython().display_formatter.format(ipython_image)') 

({u'image/png': '\x89PNG...', 

u'text/plain': u'<IPython.core.display.Image object>'}, 

{}) 

 

Test that IPython images still work even in latex output mode:: 

 

sage: shell.run_cell('%display latex') # indirect doctest 

sage: shell.run_cell('set(get_ipython().display_formatter.format(ipython_image)[0].keys())' 

....: ' == set(["text/plain", "image/png"])') 

\newcommand{\Bold}[1]{\mathbf{#1}}\mathrm{True} 

sage: shell.run_cell('%display default') 

sage: shell.quit() 

 

Test that ``__repr__`` is only called once when generating text output:: 

 

sage: class Repper(object): 

....: def __repr__(self): 

....: print('__repr__ called') 

....: return 'I am repper' 

sage: Repper() 

__repr__ called 

I am repper 

""" 

# First, use Sage rich output if there is any 

sage_format, sage_metadata = self.dm.displayhook(obj) 

assert PLAIN_TEXT in sage_format, 'plain text is always present' 

if not set(sage_format.keys()).issubset(self.default_mime()): 

return sage_format, sage_metadata 

# Second, try IPython widgets (obj._ipython_display_ and type registry) 

if self.ipython_display_formatter(obj): 

return {}, {} 

# Finally, try IPython rich representation (obj._repr_foo_ methods and ipython hardcoded types) 

if exclude is not None: 

exclude = list(exclude) + self.default_mime() 

else: 

exclude = self.default_mime() 

ipy_format, ipy_metadata = super(SageDisplayFormatter, self).format( 

obj, include=include, exclude=exclude) 

if not ipy_format: 

return sage_format, sage_metadata 

ipy_format[PLAIN_TEXT] = sage_format[PLAIN_TEXT] 

if PLAIN_TEXT in sage_metadata: 

ipy_metadata[PLAIN_TEXT] = sage_metadata[PLAIN_TEXT] 

return ipy_format, ipy_metadata 

 

 

class SagePlainTextFormatter(PlainTextFormatter): 

 

def __init__(self, *args, **kwds): 

r""" 

Improved plain text IPython formatter. 

 

In particular, it correctly print lists of matrices or other 

objects (see 

:meth:`sage.structure.parent.Parent._repr_option`). 

 

.. warning:: 

 

This IPython formatter is NOT used. You could use it to 

enable Sage formatting in IPython, but Sage uses its own 

rich output system that is more flexible and supports 

different backends. 

 

INPUT/OUTPUT: 

 

See the IPython documentation. 

 

EXAMPLES:: 

 

sage: from sage.repl.interpreter import get_test_shell 

sage: shell = get_test_shell() 

sage: shell.display_formatter.formatters['text/plain'] 

<IPython.core.formatters.PlainTextFormatter object at 0x...> 

sage: shell.quit() 

""" 

super(SagePlainTextFormatter, self).__init__(*args, **kwds) 

 

def __call__(self, obj): 

""" 

Compute the pretty representation of the object. 

 

Adapted from ``IPython.core.formatters.PlainTextPrettyPrint``. 

 

INPUT: 

 

- ``obj`` -- anything. 

 

OUTPUT: 

 

String. The plain text representation. 

 

EXAMPLES:: 

 

sage: from sage.repl.display.formatter import SagePlainTextFormatter 

sage: fmt = SagePlainTextFormatter() 

sage: fmt 

<sage.repl.display.formatter.SagePlainTextFormatter object at 0x...> 

sage: fmt(2) 

---- calling ipython formatter ---- 

'2' 

sage: a = identity_matrix(ZZ, 2) 

sage: fmt([a, a]) 

---- calling ipython formatter ---- 

'[\n[1 0] [1 0]\n[0 1], [0 1]\n]' 

""" 

from sage.doctest import DOCTEST_MODE 

if DOCTEST_MODE: 

# Just to show that this is never executed in any other doctests in the Sage library 

print('---- calling ipython formatter ----') 

from six import StringIO 

stream = StringIO() 

printer = SagePrettyPrinter( 

stream, self.max_width, unicode_to_str(self.newline)) 

printer.pretty(obj) 

printer.flush() 

return stream.getvalue()