In [1]:
%matplotlib inline
from pylab import *
from pylab import *

In [2]:
#importing widgets
from ipywidgets import widgets
from IPython.display import display

Basic plot example


In [3]:
from matplotlib.pyplot import figure, plot, xlabel, ylabel, title, show

In [4]:
x=linspace(0,5,10)
y=x**2

In [5]:
figure()
plot(x,y,'r')
xlabel('x')
ylabel('y')
title('title')
show()


$$c = \sqrt{a^2 + b^2}$$
$$ \begin{align} c =& \sqrt{a^2 + b^2} \\ =&\sqrt{4+16} \\ \end{align} $$
$$ \begin{align} f(x)= & x^2 \\ = & {{x[1]}} \end{align} $$

This text contains a value like $x[1]$ is {{x[1]}}

Widget example


In [6]:
from IPython.display import display
text = widgets.FloatText()

floatText = widgets.FloatText(description='MyField',min=-5,max=5)
floatSlider = widgets.FloatSlider(description='MyField',min=-5,max=5)


#https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Basics.html
float_link = widgets.jslink((floatText, 'value'), (floatSlider, 'value'))

Here we will set the fields to one of several values so that we can see pre-configured examples.


In [7]:
floatSlider.value=1

In [136]:
bSatiationPreset=widgets.Button(
    description='Salt Satiation',
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Click me'
)

bDeprivationPreset=widgets.Button(
    description='Salt Deprivation2',
    button_style='', # 'success', 'info', 'warning', 'danger' or ''
    tooltip='Click me'
)

def bDeprivationPreset_on_click(b):
    floatSlider.value=0.5
    
    
def bSatiationPreset_on_click(b):
    floatSlider.value=0.71717

    
bDeprivationPreset.on_click(bDeprivationPreset_on_click)
bSatiationPreset.on_click(bSatiationPreset_on_click)

#floatSlider.observe(bDeprivationPreset_on_click,names='value')


myw=widgets.HTMLMath('$a+b=$'+str(floatSlider.value))

In [137]:
display(floatText,floatSlider)
display(bSatiationPreset)
display(bDeprivationPreset)
display(myw)



In [10]:
txtArea = widgets.Text()
display(txtArea)

myb= widgets.Button(description="234")
def add_text(b):
    txtArea.value = txtArea.value + txtArea.value


myb.on_click(add_text)

display(myb)



In [13]:
from IPython.display import display, Markdown, Latex

#display(Markdown('*some markdown* $\phi$'))
# If you particularly want to display maths, this is more direct:
display(Latex('\phi=' + str(round(floatText.value,2))))
display(Latex('$\begin{align}\phi=&' + str(round(floatText.value,2)) + " \\ =& \alpha\end{align}$"))
display(Markdown('$$'))
display(Markdown('\\begin{align}'))
display(        '\phi=' + str(round(floatText.value,2)))
display(Markdown('\\end{align}'))
display(Markdown('$$'))


\phi=1.0
$egin{align}\phi=&1.0 \ =& lpha\end{align}$

$$

\begin{align}

'\\phi=1.0'

\end{align}

$$


In [36]:
from IPython.display import Markdown

one = 1
two = 2
myanswer = one + two**2

Markdown("# Title")
Markdown("""
# Math
## Addition
Here is a simple addition example: ${one} + {two}^2 = {myanswer}$

Here is a multi-line equation

$$
\\begin{{align}}
\\begin{{split}}
c = & a + b \\\\
 = & {one} + {two}^2 \\\\
 = & {myanswer}
 \end{{split}}
\end{{align}}
$$
""".format(one=one, two=two, myanswer=myanswer))


Out[36]:

Math

Addition

Here is a simple addition example: $1 + 2^2 = 5$

Here is a multi-line equation

$$ \begin{align} c = & a + b \\ = & 1 + 2^2 \\ = & 5 \end{align} $$

In [ ]:


In [131]:
a = widgets.IntSlider(description='a')
b = widgets.IntSlider(description='b')
c = widgets.IntSlider(description='c')

def g(a, b, c):
    print('${}*{}*{}={}$'.format(a, b, c, a*b*c))

def h(a, b, c):
    print(Markdown('${}\\times{}\\times{}={}$'.format(a, b, c, a*b*c)))
    
def f(a, b, c):
    print('{}*{}*{}={}'.format(a, b, c, a*b*c))

def f2(a, b, c):
    widgets.HTMLMath(value=Markdown('${}\\times{}\\times{}={}$'.format(a, b, c, a*b*c))._repr_markdown_())

def f3(a, b, c):
    print(Markdown('${}\\times{}\\times{}={}$'.format(a, b, c, a*b*c))._repr_markdown_())

def f4(a, b, c):
    return Markdown('${}\\times{}\\times{}={}$'.format(a, b, c, a*b*c))._repr_markdown_()

def f5(a, b, c):
    print('${}\\times{}\\times{}={}$'.format(a, b, c, a*b*c))

def f6(a, b, c):
    display(widgets.HTMLMath(value='${}\\times{}\\times{}={}$'.format(a, b, c, a*b*c)))
    
def f7(a, b, c):
    display(Markdown('${}\\times{}\\times{}={}$'.format(a, b, c, a*b*c)))

out = widgets.interactive_output(f7, {'a': a, 'b': b, 'c': c})


widgets.HBox([widgets.VBox([a, b, c]), out])
#widgets.HBox([widgets.VBox([a, b, c]), widgets.HTMLMath('$x=y+z$')])



In [106]:
Markdown('$${}\\times{}\\times{}={}$$'.format(a.value, b.value, c.value, a.value*b.value*c.value))


Out[106]:
$$0\times0\times0=0$$

In [60]:
myMarkDown


Out[60]:
$$3\times4\times4=48$$

In [67]:
%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np

def f(m, b):
    plt.figure(2)
    x = np.linspace(-10, 10, num=1000)
    plt.plot(x, m * x + b)
    plt.ylim(-5, 5)
    plt.show()

interactive_plot = interactive(f, m=(-2.0, 2.0), b=(-3, 3, 0.5))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot



In [83]:
def g(x,y):
    return Markdown("""
$$
{x}\\times {y}={z}
$$
""".format(x=3,y=4,z=4*5))

In [79]:



Out[79]:
$$ 3\times 4=20 $$

In [84]:
print(g(6,7))


<IPython.core.display.Markdown object>

In [89]:
x=3
y=4
Markdown("""
$$
\\begin{{align}}
\\begin{{split}}
z & =x \\times y \\\\
 & = {x} \\times {y} \\\\
 & = {z}
\end{{split}}
\end{{align}}
$$
""".format(x=x,y=y,z=x*y))


Out[89]:
$$ \begin{align} \begin{split} z & =x \times y \\ & = 3 \times 4 \\ & = 12 \end{split} \end{align} $$

In [90]:
Latex("""
$$
\\begin{{align}}
\\begin{{split}}
z & =x \\times y \\\\
 & = {x} \\times {y} \\\\
 & = {z}
\end{{split}}
\end{{align}}
$$
""".format(x=x,y=y,z=x*y))


Out[90]:
$$ \begin{align} \begin{split} z & =x \times y \\ & = 3 \times 4 \\ & = 12 \end{split} \end{align} $$

In [ ]: