%display latex vs. ipywidgets

asked 2020-10-17 17:00:38 -0500

rrogers gravatar image

updated 2020-10-18 03:31:13 -0500

slelievre gravatar image

This is part informational and part a question of style In Sage Manifolds, %display latex is set to produce "pretty" output like thus

%display plain
var('a, g')
display(sqrt(a + g))
%display latex
sqrt(a + g)

which everybody knows, but a more insidious thing is that it breaks ipywidgets.Hbox() "display". I did a hack in this code below; but does anybody know of a more elegant (re. automatic) means. This is a little long but illustrates what is lost. Try changing %display lines at the bottom.

%display latex  # Just to copy the problem in
import matplotlib.pyplot as plt
from IPython.display import display, HTML
import ipywidgets

# Save
file_name = 'rings.html'
save_desc = 'save as' + file_name
save_box = ipywidgets.Checkbox(False, description=save_desc)

# Red ring
red_ring = Horizon()
red_box=red_ring.box(desc='red ring')
red_ring.color = 'red'
red_ring.radius =2

# Black ring
black_ring = Horizon()
black_box = black_ring.box(desc='black ring')
black_ring.color = 'black'
black_ring.radius = 3

# Green ring
green_ring = Horizon()
green_box = green_ring.box(desc='green ring')
green_ring.color = 'green'
green_ring.radius = 4

ui = ipywidgets.HBox([red_box, black_box, green_box, save_box])

def f(red, black, green, save_f):
    global plt_obj
    plt_obj = sphere((1, 0, 0), .6)
    if red:
        plt_obj += red_ring.gobject()
    if black:
        plt_obj += black_ring.gobject()
    if green:
        plt_obj += green_ring.gobject()
    if save_f:
       save(plt_obj,file_name)
    show(plt_obj, viewer='threejs')

out_show = ipywidgets.interactive_output(f, {'red': red_box,
                                             'black': black_box,
                                             'green': green_box,
                                             'save_f' : save_box})

%display plain
display(ui)
display(out_show)
%display latex
edit retag flag offensive close merge delete