Skip to content
Advertisement

How could I use and manipulate float numbers in PyPanel using textbox widgets instead of sliders?

The Problem: I’m trying to get a float number user input indicator box like this which I designed on LabView in Python using PyPanel to eventually make a dashboard with multiple similar boxes (etc.).

Background: I’m trying to make a user friendly dashboard using PyPanel. I am new to dashboards but have some experience in running calculations and modelling in Python using Jupyter Notebook.

PyPanel only strictly allows float numbers to be inputted by a user into a slider as per this picture with code provided below:

import panel as pn
pn.extension()
float_slider = pn.widgets.FloatSlider(name='Float Slider', start=0, end=3.141, step=0.01, value=1.57)
float_slider

PyPanel also allows a textbox for strings but not numbers as seen in the code below:

import panel as pn
pn.extension()
x = pn.widgets.TextInput(name='SiO2_[wt%]',value='')
pn.Column(x)

Which can then be called on using display(x.value) but the output is in a string (i.e. ‘51.85’) which means I cannot do operations with it. When I try;

  • multiplying (i.e. display(x.value*2)) I get '51.8551.85'.

  • adding (i.e display(x.value+2)) I get the error:

TypeError                                 Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_26156/2620765455.py in <module>
----> 1 display(x.value+2)

TypeError: can only concatenate str (not "int") to str

The Question: How can I get a similar user-input widget to this number textbox that I’ve originally used (mentioned in the opening “Problem” statement) using PyPanel to accept user-inputted floating numbers (e.g. 51.85)? Does it involve some manipulation of PyPanel’s textbox widget and, if so, what can be done to make basic operations possible from user-inputted numbers?

Advertisement

Answer

Save yourself a headache and just use the string input and convert str to float with float()

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement