Trailing zeroes

Summary

I have a dashboard that I have created using streamlit and I am facing the issue of trailing zeroes even after applying the the following

Steps to reproduce

Code snippet:

comb_target_ach = [round(float(x),1) for x in comb_target_ach]
comb_target_ach = [round(x,2) for x in comb_target_ach]

on the console, if I print it I get it up to two decimal places but not in streamlit

additionally, if I use this

comb_target_ach = [f"{x:.2f}" for x in comb_target_ach]

it shows upto two decimal places but messes up with the logic for coloring

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

e.g
201.95
91.45
89.23

Explain what you expect to happen when you run the code above.

**201.950000
91.450000
89.230000
**

Hi @Manali_Hedaoo, welcome to our community!

It might be difficult to diagnose without seeing the full code, but consider using Python’s built-in format function to format the numbers as strings:

formatted_numbers = [format(x, ".2f") for x in comb_target_ach]

Let me know if that solves your issue.

Thanks,
Charly

Hi @Charly_Wargnier,
I tried it. It doesn’t seem to work.

def getOneMatchDetails(df):
df=df[1]
main_array = df[‘Match’].split(“vs”)
disb_target=df[‘METRIC 1 Target’].split(“|”)
disb_ach=df[‘METRIC 1’].split(“|”)
coll_target=df[‘METRIC 2 Target’].split(“|”)
coll_ach=df[‘METRIC 2’].split(“|”)
comb_target_ach=df[‘Combined % Target Achieved’].split(“|”)
# comb_target_ach=float(comb_target_ach)
comb_target_ach = [round(float(x),1) for x in comb_target_ach]
# comb_target_ach = [round(x,2) for x in comb_target_ach]
# comb_target_ach = [f"{x:.2f}" for x in comb_target_ach]
formatted_numbers = [format(x, “.2f”) for x in comb_target_ach]
# all_red.append(min(comb_target_ach))
all_green.append(max(comb_target_ach))
for each_element in remove_element(comb_target_ach,max(comb_target_ach)):
all_red.append(each_element)

please find the code snippet

Thanks, @Manali_Hedaoo

Could I please get the complete code for a better diagnosis?

Charly

import streamlit as st
import pandas as pd
import numpy as np
import base64
import xlrd
import openpyxl
import pyautogui
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import base64
from PIL import Image
import time
from selenium.webdriver.support.ui import Select

date_columns = [‘Week Start’, ‘Week End’]
daily = pd.read_excel(‘C:/Users/manalihedaoo/gamification/Dashboard/2023-07-23.xlsx’,
parse_dates=[‘Week Start’,‘Week End’],
# date_parser=excel_to_datetime,
dtype={‘Week Start’: str, ‘Week End’: str})

st.set_page_config(page_title=“Landing Page”, page_icon=“:rocket:”,layout=“wide”)
# st.header(“Challengers”)

def main():
image = Image.open(‘background2.png’)
st.image(image)
custom_css=“”"

.stApp {
background-color: #f0f0f0; /* Dark Grey color *

}
</style>
"""

st.sidebar.title(“Contender”)
if st.sidebar.button(“Challengers”):
st.write(“You clicked ‘Challengers’ button!”)
vertical_column_names = {
“AUTO”: {“Parameter1”:“BUS.NO”,
“Parameter2”:“BUS.NO Ach”,
“Parameter3”:“CDCE%”,
“Parameter4”:“CDCE% Ach”,
“Parameter5”:“Combined Ach%”},
“POCL”: {“Parameter1”:“BUS.NO”,
“Parameter2”:“BUS.NO Ach”,
“Parameter3”:“CDCE%”,
“Parameter4”:“CDCE% Ach”,
“Parameter5”:“Combined Ach%”},
“CAR”: {“Parameter1”:“BUS.NO”,
“Parameter2”:“BUS.NO Ach”,
“Parameter3”:“CDCE%”,
“Parameter4”:“CDCE% Ach”,
“Parameter5”:“Combined Ach%”},
“CV”: {
“Parameter1”: “Bus.Fin Amt”,
“Parameter2”: “BUS.Fin Amt Ach”,
“Parameter3”: “%CD Coll Agst.CD Due”,
“Parameter4”: “%CD Coll Agst. CD Due-Ach”,
“Parameter5”: “Combined Ach%”
},
“FES”: {
“Parameter1”: “BUS.NO”,
“Parameter2”: “BUS.NO Ach”,
“Parameter3”: “%CD Coll Agst.CD Due”,
“Parameter4”: “%CD Coll Agst.CD Due-Ach”,
“Parameter5”: “Combined Ach%”
}
}

#dropdown for verticals
options = ["AUTO", "POCL", "FES", "CAR", "CV"]
all_green=[]
all_red=[]
selected_option = st.selectbox("Select a vertical:", options, key='vertical-dropdown')
st.write("You selected:", selected_option)

# def download_csv():
    # csv = pd.read_csv('output.csv',dtype=str, header=None)
    # b64 = base64.b64encode(csv.encode()).decode()
    # transposed_df = df.T
    # output_file = 'output_challengers.csv'
    # transposed_df.to_csv(output_file, index=False)
    # b64 = base64.b64encode(open(output_file, "rb").read()).decode()
    # href = f'<a href="C:/Users/manalihedaoo/gamification/Dashboard/output_challengers.csv;base64,{b64}" download="output.csv">Download CSV file</a>'
    # return href

def show_challengers_page():
    st.subheader("Challengers Landing Page")
    st.write("This is the Challengers Landing Page.")

def highlight_color(val):
    print (val)
    if val in all_green:
        return 'background-color: green; color: white'
    elif val in all_red :
        return 'background-color: red; color: white'
    elif val.startswith("Match-") or val == "Leading":
         return "background-color: red; color: white"

    
# def highlight_red_cells(val):
#     # Check if the cell content is "Match-1", "Match-2", "Match-3", or "Leading"
#     if val.startswith("Match-") or val == "Leading":
#         return "background-color: blue; color: white"
#     else:
#         return ""
    
def remove_element(arr, element_to_remove):
    # Use list comprehension to create a new array without the specified element
    new_array = [x for x in arr if x != element_to_remove]
    return new_array

def GetCircleDetailforHtmlTable(data,vertical):
    html_table=[]
    # for eachCircle in data:
    #     data3= {
    #             "Circle": [eachCircle[0]],
    #             "BUS NO.": [eachCircle[1]],
    #             "BUS.NO ACH": [eachCircle[2]],
    #             "CDCDE%": [eachCircle[3]],
    #             "CDCE% Ach": [eachCircle[4]],
    #             "Combined Ach %": [eachCircle[5]],
    #             }
    column_names = vertical_column_names.get(vertical, {})
    for eachCircle in data:
        data3 = {
        column_names.get("Circle","Circle"):[eachCircle[0]],
        column_names.get("Parameter1"): [eachCircle[1]],
        column_names.get("Parameter2"): [eachCircle[2]],
        column_names.get("Parameter3"): [eachCircle[3]],
        column_names.get("Parameter4"): [eachCircle[4]],
        column_names.get("Parameter5"): [eachCircle[5]],
    }
        df3 = pd.DataFrame(data3) 
        writetoCsv(df3)
        df4=df3.T
        df4=df4.reset_index()
        df4.columns=df4.iloc[0]
        df4=df4[1:]
        html_table.append(df4) 
    return html_table

def writetoCsv(df):
    # Save the DataFrames to a CSV files
    transposed_df = df.T
    transposed_df=transposed_df.reset_index()
    transposed_df.columns = transposed_df.iloc[0]
    transposed_df = transposed_df[1:]
    # transposed_df.columns=['a']
    output_file = 'output_challengers.csv'
    # Use 'index=False' to exclude the index column from being written to the CSV
    transposed_df.to_csv(output_file, mode='a', index=False)

def getOneMatchDetails(df):
    df=df[1]
    main_array = df['Match'].split("vs")
    disb_target=df['METRIC 1 Target'].split("|")
    disb_ach=df['METRIC 1'].split("|")
    coll_target=df['METRIC 2 Target'].split("|")
    coll_ach=df['METRIC 2'].split("|")
    comb_target_ach=df['Combined % Target Achieved'].split("|")
comb_target_ach = [round(float(x),1) for x in comb_target_ach]
    comb_target_ach = [round(x) for x in comb_target_ach]

all_green.append(max(comb_target_ach))
    for each_element in remove_element(comb_target_ach,max(comb_target_ach)):
         all_red.append(each_element)

    final_array=[]
    for index in range(len(main_array)):
        # final_array.append([main_array[index],disb_target[index],disb_ach[index],coll_target[index],coll_ach[index],comb_target_ach[index],df['Match'],df['Leading']])
        match_data = [
        main_array[index],
        disb_target[index],
        disb_ach[index],
        coll_target[index],
        coll_ach[index],
        comb_target_ach[index],
        df['Match'],
        df['Leading']
        ]
        final_array.append(match_data)
        all_green.append(df['Leading'])
    return final_array

vertical = selected_option #dropdown 
auto_match=daily[(daily.Vertical==vertical)] 
final_array =[]
for each_row in auto_match.iterrows():
    new_array=getOneMatchDetails(each_row)
    final_array.append(new_array)

def getStartEndWeek(df,vertical):
    df_vertical=df[df.Vertical==vertical]
    start_date=pd.to_datetime(df_vertical['Week Start'].iloc[0]).strftime('%m-%d-%Y')
    end_date=pd.to_datetime(df_vertical['Week End'].iloc[0]).strftime('%m-%d-%Y')
    return start_date,end_date
 start_date,end_date=getStartEndWeek(daily,vertical)
data = {
    "Week-1": ["Week Start", "Week End"],
    vertical: [start_date,end_date],
}

counter = 1
num_matches = len(final_array)
columns = st.columns(num_matches)
# col_width = int(12 / num_matches)

for eachCircle in final_array:
    match='Match - '+ str(counter)
    counter=counter+1
    data2= {
    match: [eachCircle[0][6]],
    "Leading": [eachCircle[0][7]]}
    df2 = pd.DataFrame(data2)
    writetoCsv(df2)
    df6=df2.T
    df6=df6.reset_index()
    df6.columns=df6.iloc[0]
    df6=df6[1:]

with columns[counter - 2]: # Subtract 2 to make the counter zero-indexed
# st.table(df2)
styled_df = df2.style.applymap(highlight_color)

 st.write(styled_df, unsafe_allow_html=True) 
 html_content=GetCircleDetailforHtmlTable(eachCircle,vertical)
    for each_table in html_content:
        with columns[counter - 2]:
            # st.table(each_table)
            styled_df = each_table.style.applymap(highlight_color)
            
st.write(styled_df, unsafe_allow_html=True)
st.markdown(custom_css, unsafe_allow_html=True)
if __name__ == "__main__":
 main()

Thanks,

Can you try keeping the numbers as floating-point numbers

comb_target_ach = [round(float(x), 2) for x in comb_target_ach]
formatted_numbers = [f"{x:.2f}" for x in comb_target_ach]

Thanks,
Charly

Hi @Manali_Hedaoo,

If you could please send us the GitHub repo along with your CSV files, I’ll take another look and eventually pass them on to some other colleagues for review.

Best wishes,
Charly

Hi @Charly_Wargnier,
(https://github.com/manalihedaoo/q2)

Github repo does not exist or is private…

1 Like

Could you please make the repo public or accessible if possible, @Manali_Hedaoo?

Thank you,
Charly

Could you please try now

Hi,
Any update?

Apologies for the delay in answering this, and thank you for giving us access to the repo! :pray:

You may want to try using the str.rstrip method to remove trailing zeros and the decimal point, if necessary:

comb_target_ach = [round(float(x), 2) for x in comb_target_ach]
formatted_numbers = [f"{x:.2f}".rstrip('0').rstrip('.') for x in comb_target_ach]

Hopefully, that will work. Let us know if it doesn’t :slight_smile:

Best,
Charly

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.