Skip to content

Commit 2029881

Browse files
committed
changed Callbacks to Output; added methods for svg/img xml production
1 parent 4d833c6 commit 2029881

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed

mathics/builtin/graphics.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,14 +1463,8 @@ def boxes_to_xml(self, leaves, **options):
14631463
w += 2
14641464
h += 2
14651465

1466-
xml = (
1467-
'<svg xmlns:svg="http://www.w3.org/2000/svg" '
1468-
'xmlns="http://www.w3.org/2000/svg"\nversion="1.0" width="%f" '
1469-
'height="%f" viewBox="%f %f %f %f">%s</svg>') % (
1470-
width, height, xmin, ymin, w, h, svg)
1471-
1472-
xml = """<mtable><mtr><mtd>%s</mtd></mtr></mtable>""" % xml
1473-
return xml
1466+
return options['evaluation'].output.svg_xml(
1467+
data=svg, width=width, height=height, viewbox=[xmin, ymin, w, h])
14741468

14751469
def axis_ticks(self, xmin, xmax):
14761470
def round_to_zero(value):

mathics/core/evaluation.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,26 @@ def get_data(self):
144144
}
145145

146146

147-
class Callbacks(object):
148-
def __init__(self, out=None, clear_output=None, display_data=None):
149-
self.out = out
150-
self.clear_output = clear_output
151-
self.display_data = display_data
147+
class Output(object):
148+
def out(self, out):
149+
pass
150+
151+
def clear_output(wait=False):
152+
raise NotImplementedError
153+
154+
def display_data(self, result):
155+
raise NotImplementedError
156+
157+
def svg_xml(self, data, width, height, viewbox):
158+
raise NotImplementedError
159+
160+
def img_xml(self, data, width, height):
161+
raise NotImplementedError
152162

153163

154164
class Evaluation(object):
155165
def __init__(self, definitions=None,
156-
callbacks=None, format='text', catch_interrupt=True):
166+
output=None, format='text', catch_interrupt=True):
157167
from mathics.core.definitions import Definitions
158168

159169
if definitions is None:
@@ -163,7 +173,7 @@ def __init__(self, definitions=None,
163173
self.timeout = False
164174
self.stopped = False
165175
self.out = []
166-
self.callbacks = callbacks if callbacks else Callbacks()
176+
self.output = output if output else Output()
167177
self.listeners = {}
168178
self.options = None
169179
self.predetermined_out = None
@@ -364,17 +374,15 @@ def message(self, symbol, tag, *args):
364374
'StringForm', text, *(from_python(arg) for arg in args)), 'text')
365375

366376
self.out.append(Message(symbol_shortname, tag, text))
367-
if self.callbacks.out:
368-
self.callbacks.out(self.out[-1])
377+
self.output.out(self.out[-1])
369378

370379
def print_out(self, text):
371380
from mathics.core.expression import from_python
372381

373382
text = self.format_output(from_python(text), 'text')
374383

375384
self.out.append(Print(text))
376-
if self.callbacks.out:
377-
self.callbacks.out(self.out[-1])
385+
self.output.out(self.out[-1])
378386
if settings.DEBUG_PRINT:
379387
print('OUT: ' + text)
380388

mathics/main.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from mathics.core.definitions import Definitions
1515
from mathics.core.expression import strip_context
16-
from mathics.core.evaluation import Evaluation, Callbacks
16+
from mathics.core.evaluation import Evaluation, Output
1717
from mathics.core.parser import LineFeeder, FileLineFeeder
1818
from mathics import version_string, license_string, __version__
1919
from mathics import settings
@@ -176,6 +176,14 @@ def empty(self):
176176
return False
177177

178178

179+
class TerminalOutput(Output):
180+
def __init__(self, shell):
181+
self.shell = shell
182+
183+
def out(self, out):
184+
return self.shell.out_callback(out)
185+
186+
179187
def main():
180188
argparser = argparse.ArgumentParser(
181189
prog='mathics',
@@ -238,7 +246,7 @@ def main():
238246
if args.execute:
239247
for expr in args.execute:
240248
print(shell.get_in_prompt() + expr)
241-
evaluation = Evaluation(shell.definitions, callbacks=Callbacks(out=shell.out_callback))
249+
evaluation = Evaluation(shell.definitions, output=TerminalOutput(shell))
242250
result = evaluation.parse_evaluate(expr, timeout=settings.TIMEOUT)
243251
shell.print_result(result)
244252

@@ -250,7 +258,7 @@ def main():
250258
try:
251259
while not feeder.empty():
252260
evaluation = Evaluation(
253-
shell.definitions, callbacks=Callbacks(out=shell.out_callback), catch_interrupt=False)
261+
shell.definitions, output=TerminalOutput(shell), catch_interrupt=False)
254262
query = evaluation.parse_feeder(feeder)
255263
if query is None:
256264
continue
@@ -271,7 +279,7 @@ def main():
271279

272280
while True:
273281
try:
274-
evaluation = Evaluation(shell.definitions, callbacks=Callbacks(out=shell.out_callback))
282+
evaluation = Evaluation(shell.definitions, output=TerminalOutput(shell))
275283
query = evaluation.parse_feeder(shell)
276284
if query is None:
277285
continue

mathics/web/views.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
from django.contrib.auth.models import User
1919

2020
from mathics.core.definitions import Definitions
21-
from mathics.core.evaluation import Evaluation, Message, Result
21+
from mathics.core.evaluation import Evaluation, Message, Result, Output
2222

2323
from mathics.web.models import Query, Worksheet
2424
from mathics.web.forms import LoginForm, SaveForm
2525
from mathics.doc import documentation
2626
from mathics.doc.doc import DocPart, DocChapter, DocSection
2727
import six
2828
from six.moves import range
29+
from string import Template
2930

3031
if settings.DEBUG:
3132
JSON_CONTENT_TYPE = 'text/html'
@@ -44,6 +45,47 @@ def __init__(self, result={}):
4445
super(JsonResponse, self).__init__(response, content_type=JSON_CONTENT_TYPE)
4546

4647

48+
class WebOutput(Output):
49+
svg = Template('''
50+
<mtable>
51+
<mtr>
52+
<mtd>
53+
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
54+
version="1.1" width="$width" height="$height" viewBox="$viewbox">
55+
$data
56+
</svg>
57+
</mtd>
58+
</mtr>
59+
</mtable>
60+
''')
61+
62+
img = Template('''
63+
<mtext>
64+
<img src="$data" width="$width" height="$height" />
65+
</mtext>
66+
''')
67+
68+
def out(self, out):
69+
pass
70+
71+
def clear_output(wait=False):
72+
raise NotImplementedError
73+
74+
def display_data(self, result):
75+
raise NotImplementedError
76+
77+
def svg_xml(self, data, width, height, viewbox):
78+
svg = self.svg.substitute(
79+
data=data, width='%d' % width, height='%d' % height,
80+
viewbox=' '.join(['%f' % t for t in viewbox]))
81+
print(svg)
82+
return svg
83+
84+
def img_xml(self, data, width, height):
85+
return self.img.substitue(
86+
data=data, width=width, height=height)
87+
88+
4789
def require_ajax_login(func):
4890
def new_func(request, *args, **kwargs):
4991
if not request.user.is_authenticated():
@@ -102,7 +144,7 @@ def query(request):
102144

103145
user_definitions = request.session.get('definitions')
104146
definitions.set_user_definitions(user_definitions)
105-
evaluation = Evaluation(definitions, format='xml')
147+
evaluation = Evaluation(definitions, format='xml', output=WebOutput())
106148
feeder = MultiLineFeeder(input, '<notebook>')
107149
results = []
108150
try:

0 commit comments

Comments
 (0)