Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
~\AppData\Local\Temp/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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading