passagemath-repl 10.4.62__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. passagemath_repl-10.4.62.data/scripts/sage-cachegrind +25 -0
  2. passagemath_repl-10.4.62.data/scripts/sage-callgrind +16 -0
  3. passagemath_repl-10.4.62.data/scripts/sage-cleaner +230 -0
  4. passagemath_repl-10.4.62.data/scripts/sage-coverage +327 -0
  5. passagemath_repl-10.4.62.data/scripts/sage-eval +14 -0
  6. passagemath_repl-10.4.62.data/scripts/sage-fixdoctests +708 -0
  7. passagemath_repl-10.4.62.data/scripts/sage-inline-fortran +12 -0
  8. passagemath_repl-10.4.62.data/scripts/sage-ipynb2rst +50 -0
  9. passagemath_repl-10.4.62.data/scripts/sage-ipython +16 -0
  10. passagemath_repl-10.4.62.data/scripts/sage-massif +25 -0
  11. passagemath_repl-10.4.62.data/scripts/sage-notebook +267 -0
  12. passagemath_repl-10.4.62.data/scripts/sage-omega +25 -0
  13. passagemath_repl-10.4.62.data/scripts/sage-preparse +302 -0
  14. passagemath_repl-10.4.62.data/scripts/sage-run +27 -0
  15. passagemath_repl-10.4.62.data/scripts/sage-run-cython +10 -0
  16. passagemath_repl-10.4.62.data/scripts/sage-runtests +9 -0
  17. passagemath_repl-10.4.62.data/scripts/sage-startuptime.py +163 -0
  18. passagemath_repl-10.4.62.data/scripts/sage-valgrind +34 -0
  19. passagemath_repl-10.4.62.dist-info/METADATA +77 -0
  20. passagemath_repl-10.4.62.dist-info/RECORD +162 -0
  21. passagemath_repl-10.4.62.dist-info/WHEEL +5 -0
  22. passagemath_repl-10.4.62.dist-info/top_level.txt +1 -0
  23. sage/all__sagemath_repl.py +119 -0
  24. sage/doctest/__init__.py +4 -0
  25. sage/doctest/__main__.py +236 -0
  26. sage/doctest/all.py +4 -0
  27. sage/doctest/check_tolerance.py +261 -0
  28. sage/doctest/control.py +1727 -0
  29. sage/doctest/external.py +534 -0
  30. sage/doctest/fixtures.py +383 -0
  31. sage/doctest/forker.py +2665 -0
  32. sage/doctest/marked_output.py +102 -0
  33. sage/doctest/parsing.py +1708 -0
  34. sage/doctest/parsing_test.py +79 -0
  35. sage/doctest/reporting.py +733 -0
  36. sage/doctest/rif_tol.py +124 -0
  37. sage/doctest/sources.py +1657 -0
  38. sage/doctest/test.py +584 -0
  39. sage/doctest/tests/1second.rst +4 -0
  40. sage/doctest/tests/99seconds.rst +4 -0
  41. sage/doctest/tests/abort.rst +5 -0
  42. sage/doctest/tests/atexit.rst +7 -0
  43. sage/doctest/tests/fail_and_die.rst +6 -0
  44. sage/doctest/tests/initial.rst +15 -0
  45. sage/doctest/tests/interrupt.rst +7 -0
  46. sage/doctest/tests/interrupt_diehard.rst +14 -0
  47. sage/doctest/tests/keyboardinterrupt.rst +11 -0
  48. sage/doctest/tests/longtime.rst +5 -0
  49. sage/doctest/tests/nodoctest +5 -0
  50. sage/doctest/tests/random_seed.rst +4 -0
  51. sage/doctest/tests/show_skipped.rst +18 -0
  52. sage/doctest/tests/sig_on.rst +9 -0
  53. sage/doctest/tests/simple_failure.rst +8 -0
  54. sage/doctest/tests/sleep_and_raise.rst +106 -0
  55. sage/doctest/tests/tolerance.rst +31 -0
  56. sage/doctest/util.py +750 -0
  57. sage/interfaces/cleaner.py +48 -0
  58. sage/interfaces/quit.py +163 -0
  59. sage/misc/all__sagemath_repl.py +51 -0
  60. sage/misc/banner.py +235 -0
  61. sage/misc/benchmark.py +221 -0
  62. sage/misc/classgraph.py +131 -0
  63. sage/misc/copying.py +22 -0
  64. sage/misc/cython.py +694 -0
  65. sage/misc/dev_tools.py +745 -0
  66. sage/misc/edit_module.py +304 -0
  67. sage/misc/explain_pickle.py +3079 -0
  68. sage/misc/gperftools.py +361 -0
  69. sage/misc/inline_fortran.py +212 -0
  70. sage/misc/messaging.py +86 -0
  71. sage/misc/pager.py +21 -0
  72. sage/misc/profiler.py +179 -0
  73. sage/misc/python.py +70 -0
  74. sage/misc/remote_file.py +53 -0
  75. sage/misc/sage_eval.py +246 -0
  76. sage/misc/sage_input.py +3621 -0
  77. sage/misc/sagedoc.py +1742 -0
  78. sage/misc/sh.py +38 -0
  79. sage/misc/trace.py +90 -0
  80. sage/repl/__init__.py +16 -0
  81. sage/repl/all.py +15 -0
  82. sage/repl/attach.py +625 -0
  83. sage/repl/configuration.py +186 -0
  84. sage/repl/display/__init__.py +1 -0
  85. sage/repl/display/fancy_repr.py +354 -0
  86. sage/repl/display/formatter.py +318 -0
  87. sage/repl/display/jsmol_iframe.py +290 -0
  88. sage/repl/display/pretty_print.py +153 -0
  89. sage/repl/display/util.py +163 -0
  90. sage/repl/image.py +302 -0
  91. sage/repl/inputhook.py +91 -0
  92. sage/repl/interface_magic.py +298 -0
  93. sage/repl/interpreter.py +854 -0
  94. sage/repl/ipython_extension.py +593 -0
  95. sage/repl/ipython_kernel/__init__.py +1 -0
  96. sage/repl/ipython_kernel/__main__.py +4 -0
  97. sage/repl/ipython_kernel/all_jupyter.py +10 -0
  98. sage/repl/ipython_kernel/install.py +301 -0
  99. sage/repl/ipython_kernel/interact.py +278 -0
  100. sage/repl/ipython_kernel/kernel.py +217 -0
  101. sage/repl/ipython_kernel/widgets.py +466 -0
  102. sage/repl/ipython_kernel/widgets_sagenb.py +587 -0
  103. sage/repl/ipython_tests.py +163 -0
  104. sage/repl/load.py +326 -0
  105. sage/repl/preparse.py +2218 -0
  106. sage/repl/prompts.py +90 -0
  107. sage/repl/rich_output/__init__.py +4 -0
  108. sage/repl/rich_output/backend_base.py +648 -0
  109. sage/repl/rich_output/backend_doctest.py +316 -0
  110. sage/repl/rich_output/backend_emacs.py +151 -0
  111. sage/repl/rich_output/backend_ipython.py +596 -0
  112. sage/repl/rich_output/buffer.py +311 -0
  113. sage/repl/rich_output/display_manager.py +829 -0
  114. sage/repl/rich_output/example.avi +0 -0
  115. sage/repl/rich_output/example.canvas3d +1 -0
  116. sage/repl/rich_output/example.dvi +0 -0
  117. sage/repl/rich_output/example.flv +0 -0
  118. sage/repl/rich_output/example.gif +0 -0
  119. sage/repl/rich_output/example.jpg +0 -0
  120. sage/repl/rich_output/example.mkv +0 -0
  121. sage/repl/rich_output/example.mov +0 -0
  122. sage/repl/rich_output/example.mp4 +0 -0
  123. sage/repl/rich_output/example.ogv +0 -0
  124. sage/repl/rich_output/example.pdf +0 -0
  125. sage/repl/rich_output/example.png +0 -0
  126. sage/repl/rich_output/example.svg +54 -0
  127. sage/repl/rich_output/example.webm +0 -0
  128. sage/repl/rich_output/example.wmv +0 -0
  129. sage/repl/rich_output/example_jmol.spt.zip +0 -0
  130. sage/repl/rich_output/example_wavefront_scene.mtl +7 -0
  131. sage/repl/rich_output/example_wavefront_scene.obj +17 -0
  132. sage/repl/rich_output/output_basic.py +391 -0
  133. sage/repl/rich_output/output_browser.py +103 -0
  134. sage/repl/rich_output/output_catalog.py +54 -0
  135. sage/repl/rich_output/output_graphics.py +320 -0
  136. sage/repl/rich_output/output_graphics3d.py +345 -0
  137. sage/repl/rich_output/output_video.py +231 -0
  138. sage/repl/rich_output/preferences.py +432 -0
  139. sage/repl/rich_output/pretty_print.py +339 -0
  140. sage/repl/rich_output/test_backend.py +201 -0
  141. sage/repl/user_globals.py +214 -0
  142. sage/tests/__init__.py +1 -0
  143. sage/tests/all.py +3 -0
  144. sage/tests/article_heuberger_krenn_kropf_fsm-in-sage.py +630 -0
  145. sage/tests/arxiv_0812_2725.py +351 -0
  146. sage/tests/benchmark.py +1923 -0
  147. sage/tests/book_schilling_zabrocki_kschur_primer.py +795 -0
  148. sage/tests/book_stein_ent.py +651 -0
  149. sage/tests/book_stein_modform.py +558 -0
  150. sage/tests/cmdline.py +790 -0
  151. sage/tests/combinatorial_hopf_algebras.py +52 -0
  152. sage/tests/finite_poset.py +623 -0
  153. sage/tests/functools_partial_src.py +27 -0
  154. sage/tests/gosper-sum.py +218 -0
  155. sage/tests/lazy_imports.py +28 -0
  156. sage/tests/modular_group_cohomology.py +80 -0
  157. sage/tests/numpy.py +21 -0
  158. sage/tests/parigp.py +76 -0
  159. sage/tests/startup.py +27 -0
  160. sage/tests/symbolic-series.py +76 -0
  161. sage/tests/sympy.py +16 -0
  162. sage/tests/test_deprecation.py +31 -0
@@ -0,0 +1,596 @@
1
+ # sage_setup: distribution = sagemath-repl
2
+ """
3
+ IPython Backend for the Sage Rich Output System
4
+
5
+ This module defines the IPython backends for
6
+ :mod:`sage.repl.rich_output`.
7
+ """
8
+
9
+ # ****************************************************************************
10
+ # Copyright (C) 2015 Volker Braun <vbraun.name@gmail.com>
11
+ #
12
+ # Distributed under the terms of the GNU General Public License (GPL)
13
+ # as published by the Free Software Foundation; either version 2 of
14
+ # the License, or (at your option) any later version.
15
+ # https://www.gnu.org/licenses/
16
+ # ****************************************************************************
17
+
18
+ import os
19
+ import sys
20
+ import html
21
+ from IPython.display import publish_display_data
22
+ from sage.repl.rich_output.backend_base import BackendBase
23
+ from sage.repl.rich_output.output_catalog import *
24
+
25
+
26
+ class BackendIPython(BackendBase):
27
+ """
28
+ Common base for the IPython UIs.
29
+
30
+ EXAMPLES::
31
+
32
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPython
33
+ sage: BackendIPython()._repr_()
34
+ Traceback (most recent call last):
35
+ ...
36
+ NotImplementedError: derived classes must implement this method
37
+ """
38
+
39
+ def install(self, **kwds):
40
+ """
41
+ Switch the Sage rich output to the IPython backend.
42
+
43
+ INPUT:
44
+
45
+ - ``shell`` -- keyword argument; the IPython shell
46
+
47
+ No tests since switching away from the doctest rich output
48
+ backend will break the doctests.
49
+
50
+ EXAMPLES::
51
+
52
+ sage: from sage.repl.interpreter import get_test_shell
53
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPython
54
+ sage: backend = BackendIPython()
55
+ sage: shell = get_test_shell()
56
+ sage: backend.install(shell=shell)
57
+ sage: shell.run_cell('1+1')
58
+ 2
59
+ """
60
+ shell = kwds['shell']
61
+ from sage.repl.display.formatter import SageDisplayFormatter
62
+ shell.display_formatter = SageDisplayFormatter(parent=shell)
63
+ shell.configurables.append(shell.display_formatter)
64
+
65
+ def set_underscore_variable(self, obj):
66
+ """
67
+ Set the ``_`` builtin variable.
68
+
69
+ Since IPython handles the history itself, this does nothing.
70
+
71
+ INPUT:
72
+
73
+ - ``obj`` -- anything
74
+
75
+ EXAMPLES::
76
+
77
+ sage: from sage.repl.interpreter import get_test_shell
78
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPython
79
+ sage: backend = BackendIPython()
80
+ sage: backend.set_underscore_variable(123)
81
+ sage: _
82
+ 0
83
+ """
84
+ pass
85
+
86
+ def display_immediately(self, plain_text, rich_output):
87
+ """
88
+ Show output immediately.
89
+
90
+ This method is similar to the rich output :meth:`displayhook`,
91
+ except that it can be invoked at any time.
92
+
93
+ INPUT:
94
+
95
+ Same as :meth:`displayhook`.
96
+
97
+ OUTPUT: this method does not return anything
98
+
99
+ EXAMPLES::
100
+
101
+ sage: from sage.repl.rich_output.output_basic import OutputPlainText
102
+ sage: plain_text = OutputPlainText.example()
103
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook
104
+ sage: backend = BackendIPythonNotebook()
105
+ sage: _ = backend.display_immediately(plain_text, plain_text)
106
+ Example plain text output
107
+ """
108
+ formatted, metadata = self.displayhook(plain_text, rich_output)
109
+ if not formatted:
110
+ return
111
+ publish_display_data(data=formatted, metadata=metadata)
112
+
113
+
114
+ class BackendIPythonCommandline(BackendIPython):
115
+ """
116
+ Backend for the IPython Command Line.
117
+
118
+ EXAMPLES::
119
+
120
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
121
+ sage: BackendIPythonCommandline()
122
+ IPython command line
123
+ """
124
+
125
+ def default_preferences(self):
126
+ """
127
+ Return the backend's display preferences.
128
+
129
+ The default for the commandline is to not plot graphs since
130
+ the launching of an external viewer is considered too
131
+ disruptive.
132
+
133
+ OUTPUT:
134
+
135
+ Instance of
136
+ :class:`~sage.repl.rich_output.preferences.DisplayPreferences`.
137
+
138
+ EXAMPLES::
139
+
140
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
141
+ sage: backend = BackendIPythonCommandline()
142
+ sage: backend.default_preferences()
143
+ Display preferences:
144
+ * align_latex is not specified
145
+ * graphics is not specified
146
+ * supplemental_plot = never
147
+ * text is not specified
148
+ """
149
+ from sage.repl.rich_output.preferences import DisplayPreferences
150
+ return DisplayPreferences(supplemental_plot='never')
151
+
152
+ def _repr_(self):
153
+ """
154
+ Return a string representation.
155
+
156
+ OUTPUT: string
157
+
158
+ EXAMPLES::
159
+
160
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
161
+ sage: backend = BackendIPythonCommandline()
162
+ sage: backend._repr_()
163
+ 'IPython command line'
164
+ """
165
+ return 'IPython command line'
166
+
167
+ def supported_output(self):
168
+ """
169
+ Return the outputs that are supported by the IPython commandline backend.
170
+
171
+ OUTPUT:
172
+
173
+ Iterable of output container classes, that is, subclass of
174
+ :class:`~sage.repl.rich_output.output_basic.OutputBase`).
175
+ The order is ignored.
176
+
177
+ EXAMPLES::
178
+
179
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
180
+ sage: backend = BackendIPythonCommandline()
181
+ sage: supp = backend.supported_output(); supp # random output
182
+ set([<class 'sage.repl.rich_output.output_graphics.OutputImageGif'>,
183
+ ...,
184
+ <class 'sage.repl.rich_output.output_graphics.OutputImagePng'>])
185
+ sage: from sage.repl.rich_output.output_basic import OutputLatex
186
+ sage: OutputLatex in supp
187
+ True
188
+ """
189
+ return set([
190
+ OutputPlainText, OutputAsciiArt, OutputUnicodeArt, OutputLatex,
191
+ OutputImagePng, OutputImageGif,
192
+ OutputImagePdf, OutputImageDvi,
193
+ OutputSceneJmol, OutputSceneWavefront, OutputSceneThreejs,
194
+ ])
195
+
196
+ def displayhook(self, plain_text, rich_output):
197
+ """
198
+ Backend implementation of the displayhook.
199
+
200
+ INPUT:
201
+
202
+ - ``plain_text`` -- instance of
203
+ :class:`~sage.repl.rich_output.output_basic.OutputPlainText`. The
204
+ plain text version of the output.
205
+
206
+ - ``rich_output`` -- instance of an output container class
207
+ (subclass of
208
+ :class:`~sage.repl.rich_output.output_basic.OutputBase`). Guaranteed
209
+ to be one of the output containers returned from
210
+ :meth:`supported_output`, possibly the same as
211
+ ``plain_text``.
212
+
213
+ OUTPUT:
214
+
215
+ The IPython commandline display hook returns the IPython
216
+ display data, a pair of dictionaries. The first dictionary
217
+ contains mime types as keys and the respective output as
218
+ value. The second dictionary is metadata.
219
+
220
+ EXAMPLES::
221
+
222
+ sage: from sage.repl.rich_output.output_basic import OutputPlainText
223
+ sage: plain_text = OutputPlainText.example()
224
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
225
+ sage: backend = BackendIPythonCommandline()
226
+ sage: backend.displayhook(plain_text, plain_text)
227
+ ({'text/plain': 'Example plain text output'}, {})
228
+
229
+ TESTS:
230
+
231
+ We verify that unicode strings work::
232
+
233
+ sage: class Foo(sage.structure.sage_object.SageObject):
234
+ ....: def _rich_repr_(self, dm):
235
+ ....: return dm.types.OutputPlainText('Motörhead')
236
+ sage: from sage.repl.rich_output import get_display_manager
237
+ sage: dm = get_display_manager()
238
+ sage: dm.displayhook(Foo())
239
+ ({'text/plain': 'Mot\xf6rhead'}, {})
240
+ """
241
+ if isinstance(rich_output, OutputPlainText):
242
+ return ({'text/plain': rich_output.text.get_str()}, {})
243
+ elif isinstance(rich_output, OutputAsciiArt):
244
+ return ({'text/plain': rich_output.ascii_art.get_str()}, {})
245
+ elif isinstance(rich_output, OutputUnicodeArt):
246
+ return ({'text/plain': rich_output.unicode_art.get_str()}, {})
247
+ elif isinstance(rich_output, OutputLatex):
248
+ return ({'text/plain': rich_output.latex.get_str()}, {})
249
+ elif isinstance(rich_output, OutputImagePng):
250
+ msg = self.launch_viewer(
251
+ rich_output.png.filename(ext='png'), plain_text.text.get_str())
252
+ return ({'text/plain': msg}, {})
253
+ elif isinstance(rich_output, OutputImageGif):
254
+ msg = self.launch_viewer(
255
+ rich_output.gif.filename(ext='gif'), plain_text.text.get_str())
256
+ return ({'text/plain': msg}, {})
257
+ elif isinstance(rich_output, OutputImagePdf):
258
+ msg = self.launch_viewer(
259
+ rich_output.pdf.filename(ext='pdf'), plain_text.text.get_str())
260
+ return ({'text/plain': msg}, {})
261
+ elif isinstance(rich_output, OutputImageDvi):
262
+ msg = self.launch_viewer(
263
+ rich_output.dvi.filename(ext='dvi'), plain_text.text.get_str())
264
+ return ({'text/plain': msg}, {})
265
+ elif isinstance(rich_output, OutputSceneJmol):
266
+ msg = self.launch_jmol(rich_output, plain_text.text.get_str())
267
+ return ({'text/plain': msg}, {})
268
+ elif isinstance(rich_output, OutputSceneWavefront):
269
+ msg = self.launch_sage3d(rich_output, plain_text.text.get_str())
270
+ return ({'text/plain': msg}, {})
271
+ elif isinstance(rich_output, OutputSceneThreejs):
272
+ msg = self.launch_viewer(
273
+ rich_output.html.filename(ext='html'), plain_text.text.get_str())
274
+ return ({'text/plain': msg}, {})
275
+ else:
276
+ raise TypeError('rich_output type not supported')
277
+
278
+ def display_immediately(self, plain_text, rich_output):
279
+ """
280
+ Show output without going back to the command line prompt.
281
+
282
+ This method is similar to the rich output :meth:`displayhook`,
283
+ except that it can be invoked at any time. On the Sage command
284
+ line it launches viewers just like :meth:`displayhook`.
285
+
286
+ INPUT:
287
+
288
+ Same as :meth:`displayhook`.
289
+
290
+ OUTPUT: this method does not return anything
291
+
292
+ EXAMPLES::
293
+
294
+ sage: from sage.repl.rich_output.output_basic import OutputPlainText
295
+ sage: plain_text = OutputPlainText.example()
296
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
297
+ sage: backend = BackendIPythonCommandline()
298
+ sage: backend.display_immediately(plain_text, plain_text)
299
+ Example plain text output
300
+ """
301
+ formatdata, metadata = self.displayhook(plain_text, rich_output)
302
+ print(formatdata['text/plain'])
303
+
304
+ def launch_viewer(self, image_file, plain_text):
305
+ """
306
+ Launch external viewer for the graphics file.
307
+
308
+ INPUT:
309
+
310
+ - ``image_file`` -- string; file name of the image file
311
+
312
+ - ``plain_text`` -- string; the plain text representation of
313
+ the image file
314
+
315
+ OUTPUT:
316
+
317
+ String. Human-readable message indicating whether the viewer
318
+ was launched successfully.
319
+
320
+ EXAMPLES::
321
+
322
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
323
+ sage: backend = BackendIPythonCommandline()
324
+ sage: backend.launch_viewer('/path/to/foo.bar', 'Graphics object')
325
+ 'Launched bar viewer for Graphics object'
326
+ """
327
+ base, dot_ext = os.path.splitext(image_file)
328
+ ext = dot_ext.lstrip(os.path.extsep)
329
+ from sage.misc.viewer import viewer
330
+ command = viewer(ext)
331
+ if not command:
332
+ command = viewer.browser()
333
+ from sage.doctest import DOCTEST_MODE
334
+ if not DOCTEST_MODE:
335
+ os.system('{0} {1} 2>/dev/null 1>/dev/null &'
336
+ .format(command, image_file))
337
+ return 'Launched {0} viewer for {1}'.format(ext, plain_text)
338
+
339
+ def launch_jmol(self, output_jmol, plain_text):
340
+ """
341
+ Launch the stand-alone jmol viewer.
342
+
343
+ INPUT:
344
+
345
+ - ``output_jmol`` --
346
+ :class:`~sage.repl.rich_output.output_graphics3d.OutputSceneJmol`; the
347
+ scene to launch Jmol with
348
+
349
+ - ``plain_text`` -- string; the plain text representation
350
+
351
+ OUTPUT:
352
+
353
+ string; human-readable message indicating that the viewer was launched.
354
+
355
+ EXAMPLES::
356
+
357
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
358
+ sage: backend = BackendIPythonCommandline()
359
+ sage: from sage.repl.rich_output.output_graphics3d import OutputSceneJmol
360
+ sage: backend.launch_jmol(OutputSceneJmol.example(), 'Graphics3d object') # needs sage.plot
361
+ 'Launched jmol viewer for Graphics3d object'
362
+ """
363
+ from sage.doctest import DOCTEST_MODE
364
+ from sage.interfaces.jmoldata import JmolData
365
+ jdata = JmolData()
366
+ if not jdata.is_jmol_available() and not DOCTEST_MODE:
367
+ raise RuntimeError('jmol cannot run, no suitable java version found')
368
+ launch_script = output_jmol.launch_script_filename()
369
+ jmol_cmd = 'jmol'
370
+ if not DOCTEST_MODE:
371
+ os.system('{0} {1} 2>/dev/null 1>/dev/null &'
372
+ .format(jmol_cmd, launch_script))
373
+ return 'Launched jmol viewer for {0}'.format(plain_text)
374
+
375
+ def is_in_terminal(self):
376
+ """
377
+ Test whether the UI is meant to run in a terminal.
378
+
379
+ See
380
+ :meth:`sage.repl.rich_output.display_manager.DisplayManager.is_in_terminal`
381
+ for details.
382
+
383
+ OUTPUT: ``True`` for the IPython commandline
384
+
385
+ EXAMPLES::
386
+
387
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
388
+ sage: backend = BackendIPythonCommandline()
389
+ sage: backend.is_in_terminal()
390
+ True
391
+ """
392
+ return True
393
+
394
+ def threejs_offline_scripts(self):
395
+ """
396
+ Three.js script for the IPython command line.
397
+
398
+ OUTPUT: string containing script tag
399
+
400
+ EXAMPLES::
401
+
402
+ sage: # needs threejs
403
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonCommandline
404
+ sage: backend = BackendIPythonCommandline()
405
+ sage: backend.threejs_offline_scripts()
406
+ '...<script ...</script>...'
407
+ """
408
+ from sage.features.threejs import Threejs
409
+
410
+ if not Threejs().is_present():
411
+ return ''
412
+
413
+ script = Threejs().absolute_filename()
414
+
415
+ return '\n<script src="{0}"></script>'.format(script)
416
+
417
+
418
+ IFRAME_TEMPLATE = """
419
+ <iframe srcdoc="{escaped_html}"
420
+ width="{width}"
421
+ height="{height}"
422
+ style="border: 0;">
423
+ </iframe>
424
+ """
425
+
426
+
427
+ class BackendIPythonNotebook(BackendIPython):
428
+ """
429
+ Backend for the IPython Notebook.
430
+
431
+ EXAMPLES::
432
+
433
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook
434
+ sage: BackendIPythonNotebook()
435
+ IPython notebook
436
+ """
437
+
438
+ def _repr_(self):
439
+ """
440
+ Return string representation of the backend.
441
+
442
+ OUTPUT: string
443
+
444
+ EXAMPLES::
445
+
446
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook
447
+ sage: backend = BackendIPythonNotebook()
448
+ sage: backend._repr_()
449
+ 'IPython notebook'
450
+ """
451
+ return 'IPython notebook'
452
+
453
+ def supported_output(self):
454
+ """
455
+ Return the outputs that are supported by the IPython notebook backend.
456
+
457
+ OUTPUT:
458
+
459
+ Iterable of output container classes, that is, subclass of
460
+ :class:`~sage.repl.rich_output.output_basic.OutputBase`).
461
+ The order is ignored.
462
+
463
+ EXAMPLES::
464
+
465
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook
466
+ sage: backend = BackendIPythonNotebook()
467
+ sage: supp = backend.supported_output(); supp # random output
468
+ set([<class 'sage.repl.rich_output.output_graphics.OutputPlainText'>,
469
+ ...,
470
+ <class 'sage.repl.rich_output.output_graphics.OutputImagePdf'>])
471
+ sage: from sage.repl.rich_output.output_basic import OutputLatex
472
+ sage: OutputLatex in supp
473
+ True
474
+ sage: from sage.repl.rich_output.output_graphics import OutputImageGif
475
+ sage: OutputImageGif in supp
476
+ True
477
+ """
478
+ return set([
479
+ OutputPlainText, OutputAsciiArt, OutputUnicodeArt, OutputLatex,
480
+ OutputHtml,
481
+ OutputImagePng, OutputImageGif, OutputImageJpg,
482
+ OutputImageSvg, OutputImagePdf,
483
+ OutputSceneJmol, OutputSceneThreejs,
484
+ ])
485
+
486
+ def displayhook(self, plain_text, rich_output):
487
+ """
488
+ Backend implementation of the displayhook.
489
+
490
+ INPUT:
491
+
492
+ - ``plain_text`` -- instance of
493
+ :class:`~sage.repl.rich_output.output_basic.OutputPlainText`. The
494
+ plain text version of the output.
495
+
496
+ - ``rich_output`` -- instance of an output container class
497
+ (subclass of
498
+ :class:`~sage.repl.rich_output.output_basic.OutputBase`). Guaranteed
499
+ to be one of the output containers returned from
500
+ :meth:`supported_output`, possibly the same as
501
+ ``plain_text``.
502
+
503
+ OUTPUT:
504
+
505
+ The IPython notebook display hook returns the IPython
506
+ display data, a pair of dictionaries. The first dictionary
507
+ contains mime types as keys and the respective output as
508
+ value. The second dictionary is metadata.
509
+
510
+ EXAMPLES::
511
+
512
+ sage: from sage.repl.rich_output.output_basic import OutputPlainText
513
+ sage: plain_text = OutputPlainText.example()
514
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook
515
+ sage: backend = BackendIPythonNotebook()
516
+ sage: backend.displayhook(plain_text, plain_text)
517
+ ({'text/plain': 'Example plain text output'}, {})
518
+ """
519
+ if isinstance(rich_output, OutputPlainText):
520
+ return ({'text/plain': rich_output.text.get_str()}, {})
521
+ elif isinstance(rich_output, OutputAsciiArt):
522
+ return ({'text/plain': rich_output.ascii_art.get_str()}, {})
523
+ elif isinstance(rich_output, OutputUnicodeArt):
524
+ return ({'text/plain': rich_output.unicode_art.get_str()}, {})
525
+ elif isinstance(rich_output, OutputLatex):
526
+ return ({'text/latex': rich_output.latex.get_str(),
527
+ 'text/plain': plain_text.text.get_str(),
528
+ }, {})
529
+ elif isinstance(rich_output, OutputHtml):
530
+ data = {'text/html': rich_output.html.get_str(),
531
+ 'text/plain': plain_text.text.get_str()}
532
+ if rich_output.latex:
533
+ data['text/latex'] = rich_output.latex.get_str()
534
+ return (data, {})
535
+ elif isinstance(rich_output, OutputImagePng):
536
+ return ({'image/png': rich_output.png.get(),
537
+ 'text/plain': plain_text.text.get_str(),
538
+ }, {})
539
+ elif isinstance(rich_output, OutputImageGif):
540
+ return ({'text/html': rich_output.html_fragment(),
541
+ 'text/plain': plain_text.text.get_str(),
542
+ }, {})
543
+ elif isinstance(rich_output, OutputImageJpg):
544
+ return ({'image/jpeg': rich_output.jpg.get(),
545
+ 'text/plain': plain_text.text.get_str(),
546
+ }, {})
547
+ elif isinstance(rich_output, OutputImageSvg):
548
+ return ({'image/svg+xml': rich_output.svg.get(),
549
+ 'text/plain': plain_text.text.get_str(),
550
+ }, {})
551
+ elif isinstance(rich_output, OutputImagePdf):
552
+ return ({'image/png': rich_output.png.get(),
553
+ 'text/plain': plain_text.text.get_str(),
554
+ }, {})
555
+ elif isinstance(rich_output, OutputSceneJmol):
556
+ from sage.repl.display.jsmol_iframe import JSMolHtml
557
+ jsmol = JSMolHtml(rich_output, height=500)
558
+ return ({'text/html': jsmol.iframe(),
559
+ 'text/plain': plain_text.text.get_str(),
560
+ }, {})
561
+ elif isinstance(rich_output, OutputSceneThreejs):
562
+ escaped_html = html.escape(rich_output.html.get_str())
563
+ iframe = IFRAME_TEMPLATE.format(
564
+ escaped_html=escaped_html,
565
+ width='100%',
566
+ height=400,
567
+ )
568
+ return ({'text/html': iframe,
569
+ 'text/plain': plain_text.text.get_str(),
570
+ }, {})
571
+ else:
572
+ raise TypeError('rich_output type not supported')
573
+
574
+ def threejs_offline_scripts(self):
575
+ """
576
+ Three.js script for the IPython notebook.
577
+
578
+ OUTPUT: string containing script tag
579
+
580
+ EXAMPLES::
581
+
582
+ sage: from sage.repl.rich_output.backend_ipython import BackendIPythonNotebook
583
+ sage: backend = BackendIPythonNotebook()
584
+ sage: backend.threejs_offline_scripts() # needs sage.plot
585
+ '...<script src="/nbextensions/threejs-sage/r.../three.min.js...<\\/script>...'
586
+ """
587
+ from sage.repl.rich_output import get_display_manager
588
+ from sage.features.threejs import Threejs
589
+ CDN_script = get_display_manager().threejs_scripts(online=True)
590
+ CDN_script = CDN_script.replace('</script>', r'<\/script>').replace('\n', ' \\\n')
591
+ return """
592
+ <script src="/nbextensions/threejs-sage/{}/three.min.js"></script>
593
+ <script>
594
+ if ( !window.THREE ) document.write('{}');
595
+ </script>
596
+ """.format(Threejs().required_version(), CDN_script)