Sage Interact

From Norsemathology
Jump to navigation Jump to search

Sage Interact

Sage allows you to create interactive cells where users can adjust sliders and enter values into text boxes. The keyword @interact tells Sage that a cell is interactive.

Examples

Simple Graph Viewer

This code offers the user a text box to enter a function to graph, and text boxes to enter the minimum x value and maximum x value.

@interact
def _(Function = x^2 + 1, X_Min = -3.0,X_Max = 3.0,):
    html('<h2>Simple Grapher</h2>')
    p = plot( Function, (X_Min, X_Max) )
    show(p)

Graph viewer.png

Binary Search Tree Visualization

This code allows the user to enter values to be inserted into a binary search tree and draws a visualization of the binary search tree. Note: This is still in development and the drawing code doesn't behave quite correctly yet.

class TreeNode:
    def __init__(self, val, left_child = None, right_child = None):
        self.__val = val
        self.__left = left_child
        self.__right = right_child

    def getValue(self):
        return self.__val

    def getLeft(self):
        return self.__left

    def getRight(self):
        return self.__right

    def setLeft(self, node):
        self.__left = node

    def setRight(self, node):
        self.__right = node

    def __len__(self):
        return int(1)

class BSTree:
    def __init__(self, root):
        if root == None:
            self.__root = None
            self.__numItems = 0
        else:
            self.__root = root
            self.__numItems = len(root)
        self.__startLoc = [0,100]

    def printTree(self):
        print "Binary Search Tree: Values in Prefix Order:"
        self.__printPrefix(self.__root)

    def __printPrefix(self,node):
        if node == None:
            return
        print node.getValue()
        self.__printPrefix(node.getLeft())
        self.__printPrefix(node.getRight())

    def __len__(self):
        return self.__numItems

    def __str__(self):
        return 'Binary Search Tree with %d items'%self.__numItems

    def __contains__(self,val):
        return self.__find(val, self.__root)

    def __find(self,val,node):
        if node == None:
            return False
        elif node.getValue() == val:
            return True
        elif node.getValue() < val:
            return self.__find(val,node.getRight())
        else:
            return self.__find(val,node.getLeft())

    def add(self, val):
        self.__addNode( val , self.__root )
        self.__numItems += 1

    def __addNode(self, val, node):
        if node == None:
            return TreeNode( val )
        elif node.getValue() < val:
            newSubTree = self.__addNode( val, node.getRight() )
            node.setRight(newSubTree)
            return node
        else:
            newSubTree = self.__addNode( val, node.getLeft() )
            node.setLeft( newSubTree )
            return node

    def printTreeInteractive(self,g):
        return self.__printInteractive(g,self.__root,0,1,self.__startLoc)

    def __printInteractive(self, g, node, depth, lr, loc):
        if node != None:
            oldLoc = copy(loc)
            loc[0] = loc[0] - 20*lr
            loc[1] = loc[1] - 15
            if node != self.__root:
                g += line( [ oldLoc , loc ] )
            g += circle( ( loc[0], loc[1]), 3 )
            g += text( str( node.getValue() ) , ( loc[0], loc[1]) )
            g += self.__printInteractive( g, node.getLeft(), depth + 1, 1, copy(loc) )
            g += self.__printInteractive( g, node.getRight(), depth + 1, -1, copy(loc) )
        return g

tree = BSTree(TreeNode(25))
@interact
def _(Add_Node = 20):
    g = Graphics()
    html('<h2>Binary Search Tree</h2>')
    tree.add( int( Add_Node ) )
    g = Graphics()
    g = tree.printTreeInteractive( g )
    g.show(axes=False)

Sage binary tree.png