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

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

# -*- encoding: utf-8 -*- 

r""" 

Basic Output Types 

 

The Sage rich representation system requires a special container class 

to hold the data for each type of rich output. They all inherit from 

:class:`OutputBase`, though a more typical example is 

:class:`OutputPlainText`. Some output classes consist of more than one 

data buffer, for example jmol or certain animation formats. The output 

class is independent of user preferences and of the display 

backend. 

 

The display backends can define derived classes to attach 

backend-specific display functionality to, for example how to launch a 

viewer. But they must not change how the output container is 

created. To enforce this, the Sage ``_rich_repr_`` magic method will 

only ever see the output class defined here. The display manager will 

promote it to a backend-specific subclass if necessary prior to 

displaying it. 

 

To create new types of output, you must create your own subclass of 

:class:`OutputBase` and register it in 

:mod:`sage.repl.rich_output.output_catalog`. 

 

.. warning:: 

 

All rich output data in sublasses of :class:`OutputBase` must be 

contained in :class:`~sage.repl.rich_output.buffer.OutputBuffer` 

instances. You must never reference any files on the local file 

system, as there is no guarantee that the notebook server and the 

worker process are on the same computer. Or even share a common 

file system. 

""" 

 

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

# Copyright (C) 2015 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 sage.structure.sage_object import SageObject 

from sage.repl.rich_output.buffer import OutputBuffer 

 

 

class OutputBase(SageObject): 

""" 

Base class for all rich output containers. 

""" 

 

def _repr_(self): 

""" 

Return a string representation. 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_basic import OutputBase 

sage: output_base = OutputBase() 

sage: output_base._repr_() 

'OutputBase container' 

""" 

return '{0} container'.format(self.__class__.__name__) 

 

@classmethod 

def example(cls): 

""" 

Construct a sample instance 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of the :class:`OutputBase` subclass. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_basic import OutputBase 

sage: OutputBase.example() 

Traceback (most recent call last): 

... 

NotImplementedError: derived classes must implement this class method 

""" 

raise NotImplementedError('derived classes must implement this class method') 

 

 

class OutputPlainText(OutputBase): 

 

def __init__(self, plain_text): 

""" 

Plain Text Output 

 

INPUT: 

 

- ``plain_text`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, 

a bytes (string in Python 2.x) or string (unicode in Python 

2.x) can be passed directly which will then be converted 

into an 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. The 

plain text output. 

 

This should always be exactly the same as the (non-rich) 

output from the ``_repr_`` method. Every backend object must 

support plain text output as fallback. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputPlainText 

sage: OutputPlainText('foo') 

OutputPlainText container 

""" 

self.text = OutputBuffer(plain_text) 

 

@classmethod 

def example(cls): 

""" 

Construct a sample plain text output container 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of :class:`OutputPlainText`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputPlainText 

sage: OutputPlainText.example() 

OutputPlainText container 

sage: OutputPlainText.example().text.get_str() 

'Example plain text output' 

""" 

return cls('Example plain text output') 

 

def print_to_stdout(self): 

""" 

Write the data to stdout. 

 

This is just a convenience method to help with debugging. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputPlainText 

sage: plain_text = OutputPlainText.example() 

sage: plain_text.print_to_stdout() 

Example plain text output 

""" 

print(self.text.get_str()) 

 

 

class OutputAsciiArt(OutputBase): 

 

def __init__(self, ascii_art): 

""" 

ASCII Art Output 

 

INPUT: 

 

- ``ascii_art`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, 

a string (bytes) can be passed directly which will then be 

converted into an 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Ascii 

art rendered into a string. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputAsciiArt 

sage: OutputAsciiArt(':-}') 

OutputAsciiArt container 

""" 

self.ascii_art = OutputBuffer(ascii_art) 

 

@classmethod 

def example(cls): 

r""" 

Construct a sample ascii art output container 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of :class:`OutputAsciiArt`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputAsciiArt 

sage: OutputAsciiArt.example() 

OutputAsciiArt container 

sage: OutputAsciiArt.example().ascii_art.get_str() 

'[ * * * * ]\n[ ** ** * * * * * * ]\n[ ***, * , * , **, ** , *, * , * , * ]' 

""" 

return cls('[ * * * * ]\n' 

'[ ** ** * * * * * * ]\n' 

'[ ***, * , * , **, ** , *, * , * , * ]') 

 

def print_to_stdout(self): 

""" 

Write the data to stdout. 

 

This is just a convenience method to help with debugging. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputAsciiArt 

sage: ascii_art = OutputAsciiArt.example() 

sage: ascii_art.print_to_stdout() 

[ * * * * ] 

[ ** ** * * * * * * ] 

[ ***, * , * , **, ** , *, * , * , * ] 

""" 

print(self.ascii_art.get_str()) 

 

 

class OutputUnicodeArt(OutputBase): 

 

def __init__(self, unicode_art): 

""" 

Unicode Art Output 

 

Similar to :class:`OutputAsciiArt` but using the entire 

unicode range. 

 

INPUT: 

 

- ``unicode_art`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, 

a string (unicode in Python 2.x) can be passed directly 

which will then be converted into an 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Unicode 

art rendered into a string. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputUnicodeArt 

sage: OutputUnicodeArt(u':-}') 

OutputUnicodeArt container 

""" 

# Internally, all buffers store bytes. Unicode is always utf-8 

# encoded. 

if not isinstance(unicode_art, bytes): 

unicode_art = unicode_art.encode('utf-8') 

self.unicode_art = OutputBuffer(unicode_art) 

 

@classmethod 

def example(cls): 

r""" 

Construct a sample unicode art output container 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of :class:`OutputUnicodeArt`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputUnicodeArt 

sage: OutputUnicodeArt.example() 

OutputUnicodeArt container 

sage: print(OutputUnicodeArt.example().unicode_art.get_unicode()) 

⎛-11 0 1⎞ 

⎜ 3 -1 0⎟ 

⎝ -1 -1 0⎠ 

""" 

return cls(u'⎛-11 0 1⎞\n' 

u'⎜ 3 -1 0⎟\n' 

u'⎝ -1 -1 0⎠') 

 

def print_to_stdout(self): 

""" 

Write the data to stdout. 

 

This is just a convenience method to help with debugging. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputUnicodeArt 

sage: unicode_art = OutputUnicodeArt.example() 

sage: unicode_art.print_to_stdout() 

⎛-11 0 1⎞ 

⎜ 3 -1 0⎟ 

⎝ -1 -1 0⎠ 

""" 

print(self.unicode_art.get_unicode()) 

 

 

class OutputLatex(OutputBase): 

 

def __init__(self, latex): 

""" 

LaTeX Output 

 

.. note:: 

 

The LaTeX commands will only use a subset of LaTeX that 

can be displayed by MathJax. 

 

INPUT: 

 

- ``latex`` -- 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. Alternatively, 

a string (bytes) can be passed directly which will then be 

converted into an 

:class:`~sage.repl.rich_output.buffer.OutputBuffer`. String 

containing the latex equation code. Excludes the surrounding 

dollar signs / LaTeX equation environment. Also excludes the 

surrounding MathJax ``<html>`` tag. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputLatex 

sage: OutputLatex('<html><script type="math/tex; mode=display">1</script></html>') 

OutputLatex container 

""" 

self.latex = OutputBuffer(latex) 

 

def mathjax(self, display=True): 

r""" 

Return the LaTeX with a surrounding MathJax HTML code. 

 

INPUT: 

 

- ``display`` -- boolean. Whether to return display (as 

opposed to inline) TeX. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputLatex 

sage: rich_output = OutputLatex('1') 

sage: rich_output.latex 

buffer containing 1 bytes 

sage: rich_output.latex.get_str() 

'1' 

sage: rich_output.mathjax() 

'<html><script type="math/tex; mode=display">1</script></html>' 

sage: rich_output.mathjax(display=False) 

'<html><script type="math/tex">1</script></html>' 

""" 

if display: 

template = r'<html><script type="math/tex; mode=display">{0}</script></html>' 

else: 

template = r'<html><script type="math/tex">{0}</script></html>' 

return template.format(self.latex.get_str()) 

 

def display_equation(self): 

r""" 

Return the LaTeX code for a display equation 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputLatex 

sage: rich_output = OutputLatex('1') 

sage: rich_output.latex 

buffer containing 1 bytes 

sage: rich_output.latex.get_str() 

'1' 

sage: rich_output.display_equation() 

'\\begin{equation}\n1\n\\end{equation}' 

""" 

return '\n'.join([r'\begin{equation}', self.latex.get_str(), 

r'\end{equation}']) 

 

def inline_equation(self): 

r""" 

Return the LaTeX code for an inline equation 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputLatex 

sage: rich_output = OutputLatex('1') 

sage: rich_output.latex 

buffer containing 1 bytes 

sage: rich_output.latex.get_str() 

'1' 

sage: rich_output.inline_equation() 

'\\begin{math}\n1\n\\end{math}' 

""" 

return '\n'.join([r'\begin{math}', self.latex.get_str(), r'\end{math}']) 

 

@classmethod 

def example(cls): 

r""" 

Construct a sample LaTeX output container 

 

This static method is meant for doctests, so they can easily 

construct an example. 

 

OUTPUT: 

 

An instance of :class:`OutputLatex`. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputLatex 

sage: OutputLatex.example() 

OutputLatex container 

sage: OutputLatex.example().latex.get_str() 

'\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\int \\sin\\left(x\\right)\\,{d x}' 

""" 

return cls(r'\newcommand{\Bold}[1]{\mathbf{#1}}' 

r'\int \sin\left(x\right)\,{d x}') 

 

def print_to_stdout(self): 

r""" 

Write the data to stdout. 

 

This is just a convenience method to help with debugging. 

 

EXAMPLES:: 

 

sage: from sage.repl.rich_output.output_catalog import OutputLatex 

sage: rich_output = OutputLatex.example() 

sage: rich_output.print_to_stdout() 

\newcommand{\Bold}[1]{\mathbf{#1}}\int \sin\left(x\right)\,{d x} 

""" 

print(self.latex.get_str())