Skip to content

Commit 05a5ea2

Browse files
committed
adapted to match latest changes in IMathics and Mathics
1 parent 5420906 commit 05a5ea2

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

mathics/builtin/manipulate.py

+29-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from mathics.core.expression import String, strip_context
88
from mathics import settings
9-
from mathics.core.evaluation import Evaluation
9+
from mathics.core.evaluation import Evaluation, Output
1010

1111
from mathics.builtin.base import Builtin
1212
from mathics.core.expression import Expression, Symbol, Integer, from_python
@@ -18,7 +18,7 @@
1818
_jupyter = False
1919

2020
try:
21-
from ipywidgets import (IntSlider, FloatSlider, ToggleButtons, Box, DOMWidget)
21+
from ipywidgets import IntSlider, FloatSlider, ToggleButtons, Box, DOMWidget
2222
from IPython.core.formatters import IPythonDisplayFormatter
2323
_ipywidgets = True
2424
except ImportError:
@@ -187,6 +187,20 @@ def _add_widget(self, widget, name, parse, label):
187187
self._widgets.append(widget)
188188

189189

190+
class ManipulateOutput(Output):
191+
def max_stored_size(self, settings):
192+
return self.output.max_stored_size(settings)
193+
194+
def out(self, out):
195+
return self.output.out(out)
196+
197+
def clear_output(wait=False):
198+
raise NotImplementedError
199+
200+
def display_data(self, result):
201+
raise NotImplementedError
202+
203+
190204
class Manipulate(Builtin):
191205
"""
192206
<dl>
@@ -239,22 +253,22 @@ class Manipulate(Builtin):
239253

240254
messages = {
241255
'jupyter': 'Manipulate[] only works inside a Jupyter notebook.',
242-
'noipywidget': 'Manipulate[] needs the ipywidgets module to work.',
243256
'imathics': 'Your IMathics kernel does not seem to support all necessary operations. ' +
244257
'Please check that you have the latest version installed.',
245258
'widgetmake': 'Jupyter widget construction failed with "``".',
246259
'widgetargs': 'Illegal variable range or step parameters for ``.',
247260
'widgetdisp': 'Jupyter failed to display the widget.',
248261
}
249262

263+
requires = (
264+
'ipywidgets',
265+
)
266+
250267
def apply(self, expr, args, evaluation):
251268
'Manipulate[expr_, args__]'
252269
if (not _jupyter) or (not Kernel.initialized()) or (Kernel.instance() is None):
253270
return evaluation.message('Manipulate', 'jupyter')
254271

255-
if not _ipywidgets:
256-
return evaluation.message('Manipulate', 'noipywidget')
257-
258272
instantiator = _WidgetInstantiator() # knows about the arguments and their widgets
259273

260274
for arg in args.get_sequence():
@@ -266,23 +280,25 @@ def apply(self, expr, args, evaluation):
266280
except JupyterWidgetError as e:
267281
return evaluation.message('Manipulate', 'widgetmake', e.err)
268282

269-
clear_output_callback = evaluation.clear_output_callback
270-
display_data_callback = evaluation.display_data_callback # for pushing updates
283+
clear_output_callback = evaluation.output.clear
284+
display_data_callback = evaluation.output.display # for pushing updates
271285

272-
if clear_output_callback is None or display_data_callback is None:
286+
try:
287+
clear_output_callback(wait=True)
288+
except NotImplementedError:
273289
return evaluation.message('Manipulate', 'imathics')
274290

275291
def callback(**kwargs):
276292
clear_output_callback(wait=True)
277293

278294
line_no = evaluation.definitions.get_line_no()
279295

280-
new_evaluation = Evaluation(evaluation.definitions, result_callback=display_data_callback,
281-
out_callback=evaluation.out_callback)
282-
283296
vars = [Expression('Set', Symbol(name), value) for name, value in kwargs.items()]
284297
evaluatable = Expression('ReleaseHold', Expression('Module', Expression('List', *vars), expr))
285-
new_evaluation.evaluate([evaluatable], timeout=settings.TIMEOUT)
298+
299+
result = evaluation.evaluate(evaluatable, timeout=settings.TIMEOUT)
300+
if result:
301+
display_data_callback(data=result.result, metadata={})
286302

287303
evaluation.definitions.set_line_no(line_no) # do not increment line_no for manipulate computations
288304

0 commit comments

Comments
 (0)