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
- Converting Between Degrees and Radians. A Sage interact for radian and degree conversion.
- Interact to Plot Direction Fields for Differential Equations. A Sage interact for plotting direction fields and solutions for first-order differential equations.
- Interact to Plot Phase Planes of Systems of Differential Equations. A Sage interact Interact that plots phase planes of systems of Differential Equations with nullclines.
- Interact to Calculate Permutations and Combinations. A Sage interact to calculate permutations and combinations.
- Converting Between Degrees and Radians. A Sage interact that converts between degrees and radians.
- Polar Coordinates. A Sage interact for translating rectangular coordinates into polar coordinates.
Attribute
Permalink:
Author:
Date: 02 Apr 2019 17:11
Submitted by: Zane Corbiere