Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
# CoCalc Examples Documentation File
2
# Copyright: SageMath Inc., 2016
3
# License: Creative Commons: Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
4
---
5
language: sage
6
category: Extras / Interact
7
---
8
title: Introduction
9
descr: |
10
The decorator `@interact` is a powerful tool.
11
It allows you to add interactive UI elements to your worksheet,
12
such that a function is evaluate with different arguments.
13
14
Ref.: [@interact](http://doc.sagemath.org/html/en/reference/notebook/sagenb/notebook/interact.html#sagenb.notebook.interact.interact) --
15
but be aware, that this is **not** the same implementation as in Sage Worksheets.
16
The API is only similar, so expect some differences or missing details!
17
code: |
18
@interact
19
def func(k = slider(0, 10, .1)):
20
print("k: %s" % k)
21
s = (1 - sqrt(k))^2
22
print("s: %s" % s)
23
---
24
title: Symbolic Function
25
descr: |
26
This is a very simple example,
27
where the we first define a symbolic function $f(x)$,
28
and then substitute it with the variable `x` defined by the interact.
29
Both `x` variables aren't the same,
30
and therefore the evaluation of $f$ works as expected.
31
code: |
32
%var x
33
f(x) = 2*x + 1
34
@interact
35
def f_interact(x=(0, 100)):
36
print 'f({}) = {}'.format(x, f(x))
37
---
38
title: Multiple Sliders
39
descr: |
40
It's straight forward to add multiple sliders.
41
42
The `range_slider` retuns a tuple!
43
code: |
44
@interact
45
def func(k = slider(0, 10, .1), j = (-10, 10), l = range_slider(-5, 5)):
46
print("k: %s" % k)
47
print("j: %s" % j)
48
print("l: [%s, %s]" % l)
49
50