-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
backend_cell.py
189 lines (150 loc) · 6.28 KB
/
backend_cell.py
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
# -*- encoding: utf-8 -*-
"""
SageMathCell Backend for the Sage Rich Output System
This module defines the SageMathCell backends for
:mod:`sage.repl.rich_output`.
"""
#*****************************************************************************
# Copyright (C) 2015 Andrey Novoseltsev <[email protected]>
#
# 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/
#*****************************************************************************
import os
import stat
import sys
import tempfile
from sage.repl.rich_output.backend_ipython import BackendIPython
from sage.repl.rich_output.output_catalog import *
from misc import display_file, display_html, display_message
class BackendCell(BackendIPython):
"""
Backend for SageMathCell
EXAMPLES::
sage: from sage.repl.rich_output.backend_cell import BackendCell
sage: BackendCell()
SageMathCell
"""
def _repr_(self):
"""
Return a string representation
OUTPUT:
String.
EXAMPLES::
sage: from sage.repl.rich_output.backend_cell import BackendCell
sage: backend = BackendCell()
sage: backend._repr_()
'SageMathCell'
"""
return 'SageMathCell'
def display_immediately(self, plain_text, rich_output):
"""
Show output immediately.
This method is similar to the rich output :meth:`displayhook`,
except that it can be invoked at any time.
INPUT:
Same as :meth:`displayhook`.
EXAMPLES::
sage: from sage.repl.rich_output.output_basic import OutputPlainText
sage: plain_text = OutputPlainText.example()
sage: from sage.repl.rich_output.backend_cell import BackendCell
sage: backend = BackendCell()
sage: _ = backend.display_immediately(plain_text, plain_text)
Example plain text output
"""
if isinstance(rich_output, OutputPlainText):
return {u'text/plain': rich_output.text.get_str()}, {}
if isinstance(rich_output, OutputAsciiArt):
return {u'text/plain': rich_output.ascii_art.get_str()}, {}
if isinstance(rich_output, OutputLatex):
display_html(rich_output.latex.get_str())
elif isinstance(rich_output, OutputHtml):
display_html(rich_output.html.get_str())
elif isinstance(rich_output, OutputImageGif):
display_file(rich_output.gif.filename(), 'text/image-filename')
elif isinstance(rich_output, OutputImageJpg):
display_file(rich_output.jpg.filename(), 'text/image-filename')
elif isinstance(rich_output, OutputImagePdf):
display_file(rich_output.pdf.filename(), 'text/image-filename')
elif isinstance(rich_output, OutputImagePng):
display_file(rich_output.png.filename(), 'text/image-filename')
elif isinstance(rich_output, OutputImageSvg):
display_file(rich_output.svg.filename(), 'text/image-filename')
elif isinstance(rich_output, OutputSceneJmol):
path = tempfile.mkdtemp(suffix=".jmol", dir=".")
os.chmod(path, stat.S_IRWXU + stat.S_IXGRP + stat.S_IXOTH)
rich_output.scene_zip.save_as(os.path.join(path, 'scene.zip'))
rich_output.preview_png.save_as(os.path.join(path, 'preview.png'))
display_message({'text/plain': 'application/x-jmol file',
'application/x-jmol': path})
elif isinstance(rich_output, OutputSceneThreejs):
path = tempfile.mkstemp(suffix='.html', dir='.')[1]
path = os.path.relpath(path)
rich_output.html.save_as(path)
os.chmod(path, stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH)
display_html("""
<iframe
scrolling="no"
src="cell://{}"
style="
border: 1px silver solid;
height: 500px;
min-width: 500px;
width: 75%;
"
>
</iframe>
""".format(path))
sys._sage_.sent_files[path] = os.path.getmtime(path)
else:
raise TypeError('rich_output type not supported, got {0}'.format(rich_output))
return {u'text/plain': None}, {}
displayhook = display_immediately
def supported_output(self):
"""
Return the outputs that are supported by SageMathCell backend.
OUTPUT:
Iterable of output container classes, that is, subclass of
:class:`~sage.repl.rich_output.output_basic.OutputBase`).
The order is ignored.
EXAMPLES::
sage: from sage.repl.rich_output.backend_cell import BackendCell
sage: backend = BackendCell()
sage: supp = backend.supported_output(); supp # random output
set([<class 'sage.repl.rich_output.output_graphics.OutputImageGif'>,
...,
<class 'sage.repl.rich_output.output_graphics.OutputImagePng'>])
sage: from sage.repl.rich_output.output_basic import OutputLatex
sage: OutputLatex in supp
True
"""
return set([
OutputPlainText,
OutputAsciiArt,
OutputLatex,
OutputHtml,
OutputImageGif,
OutputImageJpg,
OutputImagePdf,
OutputImagePng,
OutputImageSvg,
OutputSceneJmol,
OutputSceneThreejs,
#OutputSceneWavefront,
])
def threejs_offline_scripts(self):
"""
Return script tags for ``viewer=threejs`` with ``online=False``.
OUTPUT:
- a string
EXAMPLES::
sage: from sage.repl.rich_output.backend_cell import BackendCell
sage: backend = BackendCell()
sage: backend.threejs_offline_scripts()
'...<script ...</script>...'
"""
return """
<script src="/static/threejs/three.min.js"></script>
"""