Avaiga / taipy-gui

Graphical User Interface generator for Taipy
Apache License 2.0
60 stars 20 forks source link

BUG-Format not applied for text control #574

Closed FlorianJacta closed 1 year ago

FlorianJacta commented 1 year ago

Description The format is not applied to the number for the text control.

How to reproduce

from taipy import Gui 
import numpy as np
import pandas as pd

data = pd.DataFrame({'values':[0,0,1]})

Gui("<|{np.mean(data['values'])}|text|format=%.2f|>").run()

Expected behavior We should get 0.33 for the displayed number and not 0.3333333...

Runtime environment Please specify relevant indications.

aazimh commented 1 year ago

Just came here searching for something similar. This is what I have:

<|{total_pred}|text|class_name=test|format=%,.0f|>

The value of total_pred is 8075105.59837587 and it displays like this.

i.e. it rounds to 3 decimal places when I need it to be none. I might be doing something wrong in terms of the format string but open to comments.

I did notice that it works as it should when the raw number in question does not have a decimal component (i.e. is 45252.0 for example). If the decimal becomes non-zero, then it doesn't work.

I'm on Taipy 2.0.0.

aazimh commented 1 year ago

I did a bit more fiddling and now it appears to work the other way (not sure what I changed specifically, but I now cast all my values as floats).

It seems to be linked to the type of value you pass to the text element - if it's an integer and you apply a number format, it doesn't seem to work properly, but if you cast the same value as a float and apply a number format then it works fine. To be fair the documentation mentions floating point values specifically, but I just expected there to be some exception handling that would work with integers.

This may not be related to the initial issue (since that is not an integer) so apologies if I hijacked it, just hoping my experience is useful in debugging.

FredLL-Avaiga commented 1 year ago

Indeed <|{float(np.mean(data['values']))}|text|format=%.2f|> gives the expected result

FredLL-Avaiga commented 1 year ago

Hi @Aazimh, the package used to format the value in the browser is sprinf-js. The format %,.0f is not valid for sprintf-js but %.0f works (I beleive you should have a warning in the browser console). The datatype is calculated at render from the value but int or float should be fine. The original question shows that we do not support (yet ?) numpy.float64 A quick workaround would be (as you discovered ?) to cast the result to a python float

aazimh commented 1 year ago

Hey @FredLL-Avaiga - thanks for explaining what was going on, very interesting - I had no idea there was a material difference between a python float and numpy float :)

On the formatting, thank you for linking the library, I had spent a lot of time trying to find relevant documentation on what format placeholders to use. Strangely enough, the extra , I have in my format string works (it adds commas as thousand separators, which is what I was going for). So don't know if that's just a happy accident or maybe has been added to the library recently.

Thanks again for taking the time to add this context, appreciate it!