1

I'm trying to build a simple widget application in voilĂ . It is working very well inside the Jupyter notebook, but when I deploy it as a voilĂ  application, every time I change the slider the whole page refreshes.

This is my code:

from bqplot import Figure
from bqplot import pyplot as plt
import numpy as np
import ipywidgets as widgets

def scatter_plot(n):
    fig = plt.figure()
    x = np.random.uniform(-1,1,n)
    y = np.random.uniform(-1,1,n)
    scatter = plt.scatter(x,y,default_size=10)
    plt.show()
slider = widgets.IntSlider(min=0,max=1000,value=10)
widgets.interact(scatter_plot, n=slider)

The code is supposed to show some random points in the grid and the slider changes the number of points. Again, the code works but my only problem is that it keeps refreshing when I change the slider (when it is a voila application).

Mohammad
  • 3,276
  • 2
  • 19
  • 35
Mark
  • 11
  • 1

1 Answers1

1

You were re-creating the figure each time the slider value changed. If you only change the x and y values in the scatter mark, then this is much smoother. Also, if you change the step increment in the slider to 10, then it will make it more responsive.

from bqplot import Figure
from bqplot import pyplot as plt
import numpy as np
import ipywidgets as widgets

fig = plt.figure()
n = 10
x = np.random.uniform(-1,1,n)
y = np.random.uniform(-1,1,n)
scatter = plt.scatter(x,y,default_size=10)
    
def scatter_plot(n):
    scatter.x = np.random.uniform(-1,1,n)
    scatter.y = np.random.uniform(-1,1,n)
    
# plt.show()
slider = widgets.IntSlider(min=0,max=1000,value=10, step = 1)
widgets.interact(scatter_plot, n=slider)
fig
DougR
  • 3,196
  • 1
  • 28
  • 29