don770omr/alpha
1
1import streamlit as st2import pandas as pd3from fractions import Fraction 4from utils import calculate_reduced_dimensions, calculate_area_and_frame, generate_pdf5from convertion import decimal_to_fraction6 7# Reduction values for cutting (subtracted from dimensions) default in inches8object_item_value = {9 'shutter': {'Door': {'height': 3, 'width': 0.5},10 'Window': {'height': 3, 'width': 0.5}},11 'glass': {'Door': {'height': 3, 'width': 0.5},12 'Window': {'height': 3, 'width': 0.5}},13 'divider_sh': {'Door': {'height': 1, 'width': 2.0},14 'Window': {'height': 1, 'width': 2.0}},15 'divider_gl': {'Door': {'height': 1, 'width': 2.0},16 'Window': {'height': 1, 'width': 2.0}},17}18 19lst_frac = ['0', '1/16', '1/8', '3/16', '1/4', '5/16', '3/8', '7/16', '1/2', 20 '9/16', '5/8', '11/16', '3/4', '13/16', '7/8', '15/16']21 22# Streamlit App UI23st.title("UPVC Door/Window Dim. Calculator")24 25# Page navigation setup26if 'page' not in st.session_state:27 st.session_state.page = 0 # Start with page 128 29# First Page: Set all parameters30if st.session_state.page == 0:31 st.header("Set Parameters")32 33 # Number of items input34 col01, col02 = st.columns([2, 2])35 with col01:36 num_items = st.number_input('Enter the number of items to process:', min_value=1,max_value=20, value=1, step=1)37 with col02:38 unit = st.selectbox(f'Select the unit of measurement:', ('Inches', 'Feet'))39 40 # Default values41 i = 042 reduction_h_shttr = object_item_value['shutter']['Door']['height']43 reduction_w_shttr = object_item_value['shutter']['Door']['width']44 divider_h_shttr = object_item_value['divider_sh']['Door']['height']45 divider_w_shttr = object_item_value['divider_sh']['Door']['width']46 reduction_h_glass = object_item_value['glass']['Door']['height']47 reduction_w_glass = object_item_value['glass']['Door']['width']48 divider_w_glass = object_item_value['divider_gl']['Door']['width']49 50 # Get inputs for reductions51 col05, col06, col07 = st.columns([2, 2, 2])52 with col05:53 st.text('Shutter - Reduction Height:')54 with col06:55 reduction_h_shttr_new_num = st.number_input('', key=f"reduction_h_shttr_n_{i}", min_value=0, value=int(reduction_h_shttr), step=1)56 with col07:57 reduction_h_shttr_new_frac = st.selectbox('', lst_frac, key=f"reduction_h_shttr_f_{i}")58 reduction_h_shttr_new = reduction_h_shttr_new_num + float(Fraction(reduction_h_shttr_new_frac))59 60 col08, col09, col010 = st.columns([2, 2, 2])61 default_s_w = 8 if reduction_w_shttr < 1 else 062 with col08:63 st.text('Shutter - Reduction Width:')64 with col09:65 reduction_w_shttr_new_num = st.number_input('', key=f"reduction_w_shttr_n_{i}", min_value=0, value=int(reduction_w_shttr), step=1)66 with col010:67 reduction_w_shttr_new_frac = st.selectbox('', lst_frac,index = default_s_w, key=f"reduction_w_shttr_f_{i}")68 reduction_w_shttr_new = reduction_w_shttr_new_num + float(Fraction(reduction_w_shttr_new_frac))69 70 col011, col012, col013 = st.columns([2, 2, 2])71 default_s_d_1 = 15 if divider_w_shttr == 0 else 072 with col011:73 st.text('Divider - Width - Shutter:')74 with col012:75 divider_w_shttr_new_num = st.number_input('', key=f"divider_w_shttr_n_{i}", min_value=0, value=int(divider_w_shttr), step=1)76 with col013:77 divider_w_shttr_new_frac = st.selectbox('', lst_frac,index = default_s_d_1 , key=f"divider_w_shttr_f_{i}")78 divider_w_shttr_new = divider_w_shttr_new_num + float(Fraction(divider_w_shttr_new_frac))79 80 col014, col015, col016 = st.columns([2, 2, 2])81 with col014:82 st.text('Glass - Reduction Height:')83 with col015:84 reduction_h_glass_new_num = st.number_input('', key=f"reduction_h_g_n_{i}", min_value=0, value=int(reduction_h_glass), step=1)85 with col016:86 reduction_h_glass_new_frac = st.selectbox('', lst_frac, key=f"reduction_h_g_f_{i}")87 reduction_h_glass_new = reduction_h_glass_new_num + float(Fraction(reduction_h_glass_new_frac))88 89 col017, col018, col019 = st.columns([2, 2, 2])90 default_g_w = 8 if reduction_w_glass < 1 else 091 with col017:92 st.text('Glass - Reduction Width:')93 with col018:94 reduction_w_glass_new_num = st.number_input('', key=f"reduction_w_g_n_{i}", min_value=0, value=int(reduction_w_glass), step=1)95 with col019:96 reduction_w_glass_new_frac = st.selectbox('', lst_frac,index = default_g_w, key=f"reduction_w_g_f_{i}")97 reduction_w_glass_new = reduction_w_glass_new_num + float(Fraction(reduction_w_glass_new_frac))98 99 col020, col021, col022 = st.columns([2, 2, 2])100 default_d_w = 15 if divider_w_glass == 0 else 0101 with col020:102 st.text('Divider - Width - Glass:')103 with col021:104 divider_w_glass_new_num = st.number_input('', key=f"divider_w_g_n_{i}", min_value=0, value=int(divider_w_glass), step=1)105 with col022:106 divider_w_glass_new_frac = st.selectbox('', lst_frac,index = default_d_w, key=f"divider_w_g_f_{i}")107 divider_w_glass_new = divider_w_glass_new_num + float(Fraction(divider_w_glass_new_frac))108 # Save parameters for next page109 if st.button("Next"):110 st.session_state.page = 1111 st.session_state.params = {112 'num_items': num_items,113 'unit': unit,114 'reduction_h_shttr_new': reduction_h_shttr_new,115 'reduction_w_shttr_new': reduction_w_shttr_new,116 'divider_w_shttr_new': divider_w_shttr_new,117 'reduction_h_glass_new': reduction_h_glass_new,118 'reduction_w_glass_new': reduction_w_glass_new,119 'divider_w_glass_new': divider_w_glass_new,120 }121 st.rerun()122 123# Second Page: Display results and calculations124elif st.session_state.page == 1:125 st.header("Calculate and Display Results")126 127 # Retrieve saved parameters from session state128 num_items = st.session_state.params['num_items']129 unit = st.session_state.params['unit']130 reduction_h_shttr_new = st.session_state.params['reduction_h_shttr_new']131 reduction_w_shttr_new = st.session_state.params['reduction_w_shttr_new']132 divider_w_shttr_new = st.session_state.params['divider_w_shttr_new']133 reduction_h_glass_new = st.session_state.params['reduction_h_glass_new']134 reduction_w_glass_new = st.session_state.params['reduction_w_glass_new']135 divider_w_glass_new = st.session_state.params['divider_w_glass_new']136 137 # Initialize a list to hold all results138 combined_results = []139 140 # Loop over the number of items to capture input and perform calculations141 num_items = st.number_input('Enter the number of items to process:', min_value=1,max_value = 20, value=num_items, step=1)142 143 for i in range(num_items):144 option = st.selectbox(f'Select item {i+1}:', ('Door', 'Window'), key=f"item_{i}")145 146 # Ensure the 'shutter', 'glass', 'divider_sh', 'divider_gl' dictionaries are updated with the correct values147 if option not in object_item_value['shutter']:148 object_item_value['shutter'][option] = {'height': reduction_h_shttr_new, 'width': reduction_w_shttr_new}149 150 if option not in object_item_value['glass']:151 object_item_value['glass'][option] = {'height': reduction_h_glass_new, 'width': reduction_w_glass_new}152 153 if option not in object_item_value['divider_sh']:154 object_item_value['divider_sh'][option] = {'height': divider_w_shttr_new, 'width': divider_w_shttr_new}155 156 if option not in object_item_value['divider_gl']:157 object_item_value['divider_gl'][option] = {'height': divider_w_glass_new, 'width': divider_w_glass_new}158 159 # Now safely update the dictionaries160 object_item_value['shutter'][option]['height'] = reduction_h_shttr_new161 object_item_value['shutter'][option]['width'] = reduction_w_shttr_new162 object_item_value['divider_sh'][option]['width'] = divider_w_shttr_new163 object_item_value['glass'][option]['height'] = reduction_h_glass_new164 object_item_value['glass'][option]['width'] = reduction_w_glass_new165 object_item_value['divider_gl'][option]['width'] = divider_w_glass_new166 167 # Input fields for dimensions (width and height)168 col12, col13, col15, col16 = st.columns([2, 2, 2, 2])169 default_i_v = 24 if unit == "Inches" else 2170 with col12: 171 height = st.number_input(f'Height for Item {i+1}:', key=f"height_{i}", min_value=0, value=default_i_v, step=1)172 173 with col13: 174 frac_h = st.selectbox('', lst_frac, key=f"frac_height_{i}")175 height += float(Fraction(frac_h))176 177 with col15: 178 width = st.number_input(f'Width for Item {i+1}:', key=f"width_{i}", min_value=0, value=default_i_v, step=1)179 180 with col16:181 frac_w = st.selectbox('', lst_frac, key=f"frac_width_{i}")182 width += float(Fraction(frac_w))183 184 reduced_shutter_width, reduced_shutter_height,reduced_shutter_area = calculate_reduced_dimensions(width, height, unit, 'shutter', object_item_value, item_type=option)185 reduced_glass_width, reduced_glass_height,reduced_glass_area = calculate_reduced_dimensions(width, height, unit, 'glass', object_item_value, item_type=option)186 187 # Prepare the results dictionary for the combined DataFrame188 results = {189 'Sl no': i + 1,190 'Type': option,191 'Outer Frame': f"{decimal_to_fraction(height)}\" | {decimal_to_fraction(width)}\"",192 'Shutter': f"{decimal_to_fraction(reduced_shutter_height)}\" | {decimal_to_fraction(reduced_shutter_width)}\"",193 'Glass': f"{decimal_to_fraction(reduced_glass_height)}\" | {decimal_to_fraction(reduced_glass_width)}\"",194 'Area': f"{round(calculate_area_and_frame(width, height, unit, option)[0], 2)} Sq. Feet"195 }196 197 combined_results.append(results)198 199 # Combine all results into one DataFrame200 df_combined = pd.DataFrame(combined_results)201 202 # Display the combined DataFrame203 st.write(df_combined.drop('Sl no', axis=1))204 205 # Generate PDF with the combined results206 pdf_data = generate_pdf(df_combined, 'Alpa Pvt Ltd', '#123 HBR Layout', unit)207 208 # PDF download button209 210 211 # Buttons for Back and Print212 col1, col2 = st.columns([2, 2])213 with col1:214 if st.button("Back"):215 st.session_state.page = 0 216 st.rerun() # Go back to the first page217 218 with col2:219 pdf_button = st.download_button(220 label="Download PDF",221 data=pdf_data,222 file_name="upvc_calculation_results.pdf",223 mime="application/pdf"224 )225 