Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagecell
Path: blob/master/backend_cell.py
447 views
1
# -*- encoding: utf-8 -*-
2
"""
3
SageMathCell Backend for the Sage Rich Output System
4
5
This module defines the SageMathCell backends for
6
:mod:`sage.repl.rich_output`.
7
"""
8
9
#*****************************************************************************
10
# Copyright (C) 2015 Andrey Novoseltsev <[email protected]>
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
# http://www.gnu.org/licenses/
16
#*****************************************************************************
17
18
import os
19
import stat
20
import sys
21
import tempfile
22
23
24
from sage.repl.rich_output.backend_ipython import BackendIPython
25
from sage.repl.rich_output.output_catalog import *
26
27
28
from misc import display_file, display_html, display_message
29
30
31
class BackendCell(BackendIPython):
32
"""
33
Backend for SageMathCell
34
35
EXAMPLES::
36
37
sage: from sage.repl.rich_output.backend_cell import BackendCell
38
sage: BackendCell()
39
SageMathCell
40
"""
41
42
def _repr_(self):
43
"""
44
Return a string representation
45
46
OUTPUT:
47
48
String.
49
50
EXAMPLES::
51
52
sage: from sage.repl.rich_output.backend_cell import BackendCell
53
sage: backend = BackendCell()
54
sage: backend._repr_()
55
'SageMathCell'
56
"""
57
return 'SageMathCell'
58
59
def display_immediately(self, plain_text, rich_output):
60
"""
61
Show output immediately.
62
63
This method is similar to the rich output :meth:`displayhook`,
64
except that it can be invoked at any time.
65
66
INPUT:
67
68
Same as :meth:`displayhook`.
69
70
EXAMPLES::
71
72
sage: from sage.repl.rich_output.output_basic import OutputPlainText
73
sage: plain_text = OutputPlainText.example()
74
sage: from sage.repl.rich_output.backend_cell import BackendCell
75
sage: backend = BackendCell()
76
sage: _ = backend.display_immediately(plain_text, plain_text)
77
Example plain text output
78
"""
79
if isinstance(rich_output, OutputPlainText):
80
return {u'text/plain': rich_output.text.get_str()}, {}
81
if isinstance(rich_output, OutputAsciiArt):
82
return {u'text/plain': rich_output.ascii_art.get_str()}, {}
83
84
if isinstance(rich_output, OutputLatex):
85
display_html(rich_output.latex.get_str())
86
elif isinstance(rich_output, OutputHtml):
87
display_html(rich_output.html.get_str())
88
89
elif isinstance(rich_output, OutputImageGif):
90
display_file(rich_output.gif.filename(), 'text/image-filename')
91
elif isinstance(rich_output, OutputImageJpg):
92
display_file(rich_output.jpg.filename(), 'text/image-filename')
93
elif isinstance(rich_output, OutputImagePdf):
94
display_file(rich_output.pdf.filename(), 'text/image-filename')
95
elif isinstance(rich_output, OutputImagePng):
96
display_file(rich_output.png.filename(), 'text/image-filename')
97
elif isinstance(rich_output, OutputImageSvg):
98
display_file(rich_output.svg.filename(), 'text/image-filename')
99
100
elif isinstance(rich_output, OutputSceneJmol):
101
path = tempfile.mkdtemp(suffix=".jmol", dir=".")
102
os.chmod(path, stat.S_IRWXU + stat.S_IXGRP + stat.S_IXOTH)
103
rich_output.scene_zip.save_as(os.path.join(path, 'scene.zip'))
104
rich_output.preview_png.save_as(os.path.join(path, 'preview.png'))
105
display_message({'text/plain': 'application/x-jmol file',
106
'application/x-jmol': path})
107
elif isinstance(rich_output, OutputSceneThreejs):
108
path = tempfile.mkstemp(suffix='.html', dir='.')[1]
109
path = os.path.relpath(path)
110
rich_output.html.save_as(path)
111
os.chmod(path, stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH)
112
display_html("""
113
<iframe
114
scrolling="no"
115
src="cell://{}"
116
style="
117
border: 1px silver solid;
118
height: 500px;
119
min-width: 500px;
120
width: 75%;
121
"
122
>
123
</iframe>
124
""".format(path))
125
sys._sage_.sent_files[path] = os.path.getmtime(path)
126
127
else:
128
raise TypeError('rich_output type not supported, got {0}'.format(rich_output))
129
return {u'text/plain': None}, {}
130
131
displayhook = display_immediately
132
133
def supported_output(self):
134
"""
135
Return the outputs that are supported by SageMathCell backend.
136
137
OUTPUT:
138
139
Iterable of output container classes, that is, subclass of
140
:class:`~sage.repl.rich_output.output_basic.OutputBase`).
141
The order is ignored.
142
143
EXAMPLES::
144
145
sage: from sage.repl.rich_output.backend_cell import BackendCell
146
sage: backend = BackendCell()
147
sage: supp = backend.supported_output(); supp # random output
148
set([<class 'sage.repl.rich_output.output_graphics.OutputImageGif'>,
149
...,
150
<class 'sage.repl.rich_output.output_graphics.OutputImagePng'>])
151
sage: from sage.repl.rich_output.output_basic import OutputLatex
152
sage: OutputLatex in supp
153
True
154
"""
155
return set([
156
OutputPlainText,
157
OutputAsciiArt,
158
OutputLatex,
159
OutputHtml,
160
161
OutputImageGif,
162
OutputImageJpg,
163
OutputImagePdf,
164
OutputImagePng,
165
OutputImageSvg,
166
167
OutputSceneJmol,
168
OutputSceneThreejs,
169
#OutputSceneWavefront,
170
])
171
172
def threejs_offline_scripts(self):
173
"""
174
Return script tags for ``viewer=threejs`` with ``online=False``.
175
176
OUTPUT:
177
178
- a string
179
180
EXAMPLES::
181
182
sage: from sage.repl.rich_output.backend_cell import BackendCell
183
sage: backend = BackendCell()
184
sage: backend.threejs_offline_scripts()
185
'...<script ...</script>...'
186
"""
187
return """
188
<script src="/static/threejs/three.min.js"></script>
189
"""
190
191