6
6
7
7
from mathics .core .expression import String , strip_context
8
8
from mathics import settings
9
- from mathics .core .evaluation import Evaluation
9
+ from mathics .core .evaluation import Evaluation , Output
10
10
11
11
from mathics .builtin .base import Builtin
12
12
from mathics .core .expression import Expression , Symbol , Integer , from_python
18
18
_jupyter = False
19
19
20
20
try :
21
- from ipywidgets import ( IntSlider , FloatSlider , ToggleButtons , Box , DOMWidget )
21
+ from ipywidgets import IntSlider , FloatSlider , ToggleButtons , Box , DOMWidget
22
22
from IPython .core .formatters import IPythonDisplayFormatter
23
23
_ipywidgets = True
24
24
except ImportError :
@@ -187,6 +187,20 @@ def _add_widget(self, widget, name, parse, label):
187
187
self ._widgets .append (widget )
188
188
189
189
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
+
190
204
class Manipulate (Builtin ):
191
205
"""
192
206
<dl>
@@ -239,22 +253,22 @@ class Manipulate(Builtin):
239
253
240
254
messages = {
241
255
'jupyter' : 'Manipulate[] only works inside a Jupyter notebook.' ,
242
- 'noipywidget' : 'Manipulate[] needs the ipywidgets module to work.' ,
243
256
'imathics' : 'Your IMathics kernel does not seem to support all necessary operations. ' +
244
257
'Please check that you have the latest version installed.' ,
245
258
'widgetmake' : 'Jupyter widget construction failed with "``".' ,
246
259
'widgetargs' : 'Illegal variable range or step parameters for ``.' ,
247
260
'widgetdisp' : 'Jupyter failed to display the widget.' ,
248
261
}
249
262
263
+ requires = (
264
+ 'ipywidgets' ,
265
+ )
266
+
250
267
def apply (self , expr , args , evaluation ):
251
268
'Manipulate[expr_, args__]'
252
269
if (not _jupyter ) or (not Kernel .initialized ()) or (Kernel .instance () is None ):
253
270
return evaluation .message ('Manipulate' , 'jupyter' )
254
271
255
- if not _ipywidgets :
256
- return evaluation .message ('Manipulate' , 'noipywidget' )
257
-
258
272
instantiator = _WidgetInstantiator () # knows about the arguments and their widgets
259
273
260
274
for arg in args .get_sequence ():
@@ -266,23 +280,25 @@ def apply(self, expr, args, evaluation):
266
280
except JupyterWidgetError as e :
267
281
return evaluation .message ('Manipulate' , 'widgetmake' , e .err )
268
282
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
271
285
272
- if clear_output_callback is None or display_data_callback is None :
286
+ try :
287
+ clear_output_callback (wait = True )
288
+ except NotImplementedError :
273
289
return evaluation .message ('Manipulate' , 'imathics' )
274
290
275
291
def callback (** kwargs ):
276
292
clear_output_callback (wait = True )
277
293
278
294
line_no = evaluation .definitions .get_line_no ()
279
295
280
- new_evaluation = Evaluation (evaluation .definitions , result_callback = display_data_callback ,
281
- out_callback = evaluation .out_callback )
282
-
283
296
vars = [Expression ('Set' , Symbol (name ), value ) for name , value in kwargs .items ()]
284
297
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 = {})
286
302
287
303
evaluation .definitions .set_line_no (line_no ) # do not increment line_no for manipulate computations
288
304
0 commit comments