Basics Of Interacts

Description

In sage we can make interactive applications that accept user input to change the output. Here, we will make an interact that simply plots a line whose slope may be changed by the user using a slider. To inform sage that an interact is being created, we write the line @interact then define a subroutine with interactive variables.

Sage Cell

Code

@interact
def plotChangingSlope(m = slider(vmin=-2, vmax=2, step_size=0.5, default=1, label='slope', display_value=True)):
    plot(m*x, (x, -5, 5), ymin=-5, ymax=5).show()
    print('y =', m, 'x')
    return

Every slider parameter has been labeled to show their purpose. vmin and vmax set the minimum and maximum values, step_size sets the interval between each slider option, default sets the default value, label sets the text labeling the slider and display_value determines whether the value the slider is currently on is shown.

Options

Other selection options

Selectors

We can also get user input for the interact using a selector, which provides a number of specific options for the user rather than a range.

A basic selector has 3 parameters. values is a list of the options the user can choose, label is the label of the selector and default is the default value of the selector.

Code

@interact
def plotChangingSlope(m = selector(values=choices, label='slope', default=1)):
    plot(m*x, (x, -5, 5), ymin=-5, ymax=5).show()
    print('y =', m, 'x')
    return

Buttons

Selectors have a parameter called buttons, which if set to true changes the interface to buttons.

Code

def plotChangingSlope(m = selector(values=choices, label='slope', default=1, buttons=True)):
    plot(m*x, (x, -5, 5), ymin=-5, ymax=5).show()
    print('y =', m, 'x')
    return

Tags

Primary Tags—Programming: Python

Secondary Tags—Python: Interacts

Related Cells

Attribute

Permalink:

Author:

Date: 02 Apr 2019 17:11

Submitted by: Zane Corbiere

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License