12

In jupyter,I have the following code to create a slider control for my variable r using ipywidgets.

from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets

r = 0.9992
def sl(r=0.9992):
    global ar
    ar = r

interact(sl, r=(0.999, 1, 0.0001))

It works but the problem is that the display of slider value is rounded to 2 decimal. As a result, the display always show 1.00.

enter image description here

Is there any way to display its actual value?

J_yang
  • 2,672
  • 8
  • 32
  • 61

3 Answers3

8

Since the version 6.x of ipywidgets, FloatSlider contains readout_format attribute. Just set it to your desired precision, e.g. like this:

widgets.FloatSlider(value=7.5, min=0, max=10.0, 
                    step=0.001, readout_format='.3f')
Paloha
  • 558
  • 6
  • 14
0

Found solution here

Values are somehow computed on the way and maybe it's not possible to do it with FloatSlider. You can use SelectionSlider instead of Float.

my_values = [i / 1000 for i in range(10)]  # [0.001, 0.002, ...] Or use numpy to list

SelectionSlider(options=[("%g"%i,i) for i in my_values])

Widget

Daniel Malachov
  • 1,604
  • 1
  • 10
  • 13
-1

Your code works if your function has a return value and it displays the precision you want to see in a label below (as in image)

mostly from info @ http://test-widgets.readthedocs.io/en/latest/examples/Using%20Interact.html

from ipywidgets import *
from IPython.display import display

def SL(r):
    return r

interact(SL,r=widgets.FloatSlider(value=0.9992,min=0.9990,max=1.0000,step=0.0001,description='Precision:',))

the slider result with precision displayed below

eric_camplin
  • 508
  • 4
  • 6