Skip to content
Snippets Groups Projects
Verified Commit b2b08c9d authored by David Beniamine's avatar David Beniamine
Browse files

FUll interactive examples

parent 8b5bdc1e
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Interactive sliders ## Interactive sliders
Les sliders affectent le calcul, la valeur est calulée par un callback python Les sliders affectent le calcul, la valeur est calulée par un callback python
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from ipywidgets import HBox, VBox, IntSlider, interactive_output, Dropdown from ipywidgets import HBox, VBox, IntSlider, interactive_output, Dropdown
from IPython.display import display from IPython.display import display
a = IntSlider() a = IntSlider()
b = IntSlider() b = IntSlider()
def f(a, b): def f(a, b):
print("{} * {} = {}".format(a, b, a * b)) print("{} * {} = {}".format(a, b, a * b))
out = interactive_output(f, { "a": a, "b": b }) out = interactive_output(f, { "a": a, "b": b })
display(HBox([VBox([a, b]), out])) display(HBox([VBox([a, b]), out]))
``` ```
%% Output %% Output
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Un autre slider, pas de calcul Un autre slider, pas de calcul
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from ipywidgets import interact from ipywidgets import interact
def foo(x): def foo(x):
print(x) print(x)
interact(foo, x=(0, 10)) interact(foo, x=(0, 10))
``` ```
%% Output %% Output
<function __main__.foo(x)> <function __main__.foo(x)>
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Quand on click ce boutton un affichage ce produit ## Quand on click ce boutton un affichage ce produit
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
# sort of based on https://github.com/voila-dashboards/voila-material/issues/18 # sort of based on https://github.com/voila-dashboards/voila-material/issues/18
import ipywidgets as widgets import ipywidgets as widgets
from IPython.display import display from IPython.display import display
output = widgets.Output() output = widgets.Output()
@output.capture(clear_output=False,wait=True) # based on https://github.com/jupyter-widgets/ipywidgets/issues/1846 and https://ipywidgets.readthedocs.io/en/latest/examples/Output%20Widget.html @output.capture(clear_output=False,wait=True) # based on https://github.com/jupyter-widgets/ipywidgets/issues/1846 and https://ipywidgets.readthedocs.io/en/latest/examples/Output%20Widget.html
def sayHello(b): def sayHello(b):
print('Hello world!') print('Hello world!')
run_button = widgets.Button( run_button = widgets.Button(
description = 'run' description = 'run'
) )
run_button.on_click(sayHello) run_button.on_click(sayHello)
display(run_button) display(run_button)
output output
``` ```
%% Output %% Output
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Figure interactive avec matplotlib ## Figure interactive avec matplotlib
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%matplotlib inline %matplotlib inline
from ipywidgets import interact, interactive from ipywidgets import interact, interactive
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
def ma_fonction(a,b,c): def ma_fonction(a,b,c):
plt.figure(2) # ??? plt.figure(2) # ???
x = np.linspace(-10,10, num=1000) x = np.linspace(-10,10, num=1000)
plt.plot(x, a*x**2+b*x+c) plt.plot(x, a*x**2+b*x+c)
plt.ylim(-10,10) plt.ylim(-10,10)
plt.show() plt.show()
interactive_plot = interactive(ma_fonction, a=(-10,10), b=(-20,20), c=(-5,5)) interactive_plot = interactive(ma_fonction, a=(-10,10), b=(-20,20), c=(-5,5))
interactive_plot interactive_plot
``` ```
%% Output %% Output
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Figure interactive avec seaborn
%% Cell type:code id: tags:
``` python
from ipywidgets import interact, interactive
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
# Chargement d'un dataset connu
tips = sns.load_dataset("tips")
tips
@interact(xmax=50,ymax=10, hue=["smoker","sex","day","time","size"], comic=[('Non',False),('Oui',True)])
def f(xmax, ymax, hue, comic):
sns.set()
sns.set(style="darkgrid")
if(comic):
ctx = plt.xkcd()
else:
from contextlib import nullcontext
ctx = nullcontext()
with ctx:
plot = sns.relplot(x="total_bill", y="tip", hue=hue, data=tips)
plot.set(xlim=(0, xmax), ylim=(0, ymax))
```
%% Output
%% Cell type:markdown id: tags:
## Figure interactive avec bokeh ## Figure interactive avec bokeh
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from bokeh.io import show, output_notebook from bokeh.io import show, output_notebook
from bokeh.layouts import column from bokeh.layouts import column
from bokeh.plotting import Figure from bokeh.plotting import Figure
output_notebook() from bokeh.models import Select, CustomJS
from bokeh.layouts import column
output_notebook()
def f(color): def app(doc):
color = Select(title="Color", options=['black', 'green', 'blue', 'red'], value="black")
plot = Figure(x_range=(0, 1), y_range=(0, 1), plot_width=350, plot_height=350) plot = Figure(x_range=(0, 1), y_range=(0, 1), plot_width=350, plot_height=350)
line = plot.line(x=(0,1), y=(0,1), line_color=color, line_width=4) def update(attr, old, new):
show(plot) line = plot.line(x=(0,1), y=(0,1), line_color=color.value, line_width=4)
update(None, None, None)
color = Dropdown( color.on_change('value', update)
options=["black", "green", "blue", "red"], doc.add_root(column(color, plot))
value="black", return doc
description='Color:',
disabled=False,
)
out = interactive_output(f, { "color": color})
display(VBox([color, out])) show(app)
``` ```
%% Output %% Output
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
``` ```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment