alpha / app.py
don770omr's picture
Update app.py
353f887 verified
import streamlit as st
import pandas as pd
from fractions import Fraction
from utils import calculate_reduced_dimensions, calculate_area_and_frame, generate_pdf
from convertion import decimal_to_fraction
# Reduction values for cutting (subtracted from dimensions) default in inches
object_item_value = {
'shutter': {'Door': {'height': 3, 'width': 0.5},
'Window': {'height': 3, 'width': 0.5}},
'glass': {'Door': {'height': 3, 'width': 0.5},
'Window': {'height': 3, 'width': 0.5}},
'divider_sh': {'Door': {'height': 1, 'width': 2.0},
'Window': {'height': 1, 'width': 2.0}},
'divider_gl': {'Door': {'height': 1, 'width': 2.0},
'Window': {'height': 1, 'width': 2.0}},
}
lst_frac = ['0', '1/16', '1/8', '3/16', '1/4', '5/16', '3/8', '7/16', '1/2',
'9/16', '5/8', '11/16', '3/4', '13/16', '7/8', '15/16']
# Streamlit App UI
st.title("UPVC Door/Window Dim. Calculator")
# Page navigation setup
if 'page' not in st.session_state:
st.session_state.page = 0 # Start with page 1
# First Page: Set all parameters
if st.session_state.page == 0:
st.header("Set Parameters")
# Number of items input
col01, col02 = st.columns([2, 2])
with col01:
num_items = st.number_input('Enter the number of items to process:', min_value=1,max_value=20, value=1, step=1)
with col02:
unit = st.selectbox(f'Select the unit of measurement:', ('Inches', 'Feet'))
# Default values
i = 0
reduction_h_shttr = object_item_value['shutter']['Door']['height']
reduction_w_shttr = object_item_value['shutter']['Door']['width']
divider_h_shttr = object_item_value['divider_sh']['Door']['height']
divider_w_shttr = object_item_value['divider_sh']['Door']['width']
reduction_h_glass = object_item_value['glass']['Door']['height']
reduction_w_glass = object_item_value['glass']['Door']['width']
divider_w_glass = object_item_value['divider_gl']['Door']['width']
# Get inputs for reductions
col05, col06, col07 = st.columns([2, 2, 2])
with col05:
st.text('Shutter - Reduction Height:')
with col06:
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)
with col07:
reduction_h_shttr_new_frac = st.selectbox('', lst_frac, key=f"reduction_h_shttr_f_{i}")
reduction_h_shttr_new = reduction_h_shttr_new_num + float(Fraction(reduction_h_shttr_new_frac))
col08, col09, col010 = st.columns([2, 2, 2])
default_s_w = 8 if reduction_w_shttr < 1 else 0
with col08:
st.text('Shutter - Reduction Width:')
with col09:
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)
with col010:
reduction_w_shttr_new_frac = st.selectbox('', lst_frac,index = default_s_w, key=f"reduction_w_shttr_f_{i}")
reduction_w_shttr_new = reduction_w_shttr_new_num + float(Fraction(reduction_w_shttr_new_frac))
col011, col012, col013 = st.columns([2, 2, 2])
default_s_d_1 = 15 if divider_w_shttr == 0 else 0
with col011:
st.text('Divider - Width - Shutter:')
with col012:
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)
with col013:
divider_w_shttr_new_frac = st.selectbox('', lst_frac,index = default_s_d_1 , key=f"divider_w_shttr_f_{i}")
divider_w_shttr_new = divider_w_shttr_new_num + float(Fraction(divider_w_shttr_new_frac))
col014, col015, col016 = st.columns([2, 2, 2])
with col014:
st.text('Glass - Reduction Height:')
with col015:
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)
with col016:
reduction_h_glass_new_frac = st.selectbox('', lst_frac, key=f"reduction_h_g_f_{i}")
reduction_h_glass_new = reduction_h_glass_new_num + float(Fraction(reduction_h_glass_new_frac))
col017, col018, col019 = st.columns([2, 2, 2])
default_g_w = 8 if reduction_w_glass < 1 else 0
with col017:
st.text('Glass - Reduction Width:')
with col018:
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)
with col019:
reduction_w_glass_new_frac = st.selectbox('', lst_frac,index = default_g_w, key=f"reduction_w_g_f_{i}")
reduction_w_glass_new = reduction_w_glass_new_num + float(Fraction(reduction_w_glass_new_frac))
col020, col021, col022 = st.columns([2, 2, 2])
default_d_w = 15 if divider_w_glass == 0 else 0
with col020:
st.text('Divider - Width - Glass:')
with col021:
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)
with col022:
divider_w_glass_new_frac = st.selectbox('', lst_frac,index = default_d_w, key=f"divider_w_g_f_{i}")
divider_w_glass_new = divider_w_glass_new_num + float(Fraction(divider_w_glass_new_frac))
# Save parameters for next page
if st.button("Next"):
st.session_state.page = 1
st.session_state.params = {
'num_items': num_items,
'unit': unit,
'reduction_h_shttr_new': reduction_h_shttr_new,
'reduction_w_shttr_new': reduction_w_shttr_new,
'divider_w_shttr_new': divider_w_shttr_new,
'reduction_h_glass_new': reduction_h_glass_new,
'reduction_w_glass_new': reduction_w_glass_new,
'divider_w_glass_new': divider_w_glass_new,
}
st.rerun()
# Second Page: Display results and calculations
elif st.session_state.page == 1:
st.header("Calculate and Display Results")
# Retrieve saved parameters from session state
num_items = st.session_state.params['num_items']
unit = st.session_state.params['unit']
reduction_h_shttr_new = st.session_state.params['reduction_h_shttr_new']
reduction_w_shttr_new = st.session_state.params['reduction_w_shttr_new']
divider_w_shttr_new = st.session_state.params['divider_w_shttr_new']
reduction_h_glass_new = st.session_state.params['reduction_h_glass_new']
reduction_w_glass_new = st.session_state.params['reduction_w_glass_new']
divider_w_glass_new = st.session_state.params['divider_w_glass_new']
# Initialize a list to hold all results
combined_results = []
# Loop over the number of items to capture input and perform calculations
num_items = st.number_input('Enter the number of items to process:', min_value=1,max_value = 20, value=num_items, step=1)
for i in range(num_items):
option = st.selectbox(f'Select item {i+1}:', ('Door', 'Window'), key=f"item_{i}")
# Ensure the 'shutter', 'glass', 'divider_sh', 'divider_gl' dictionaries are updated with the correct values
if option not in object_item_value['shutter']:
object_item_value['shutter'][option] = {'height': reduction_h_shttr_new, 'width': reduction_w_shttr_new}
if option not in object_item_value['glass']:
object_item_value['glass'][option] = {'height': reduction_h_glass_new, 'width': reduction_w_glass_new}
if option not in object_item_value['divider_sh']:
object_item_value['divider_sh'][option] = {'height': divider_w_shttr_new, 'width': divider_w_shttr_new}
if option not in object_item_value['divider_gl']:
object_item_value['divider_gl'][option] = {'height': divider_w_glass_new, 'width': divider_w_glass_new}
# Now safely update the dictionaries
object_item_value['shutter'][option]['height'] = reduction_h_shttr_new
object_item_value['shutter'][option]['width'] = reduction_w_shttr_new
object_item_value['divider_sh'][option]['width'] = divider_w_shttr_new
object_item_value['glass'][option]['height'] = reduction_h_glass_new
object_item_value['glass'][option]['width'] = reduction_w_glass_new
object_item_value['divider_gl'][option]['width'] = divider_w_glass_new
# Input fields for dimensions (width and height)
col12, col13, col15, col16 = st.columns([2, 2, 2, 2])
default_i_v = 24 if unit == "Inches" else 2
with col12:
height = st.number_input(f'Height for Item {i+1}:', key=f"height_{i}", min_value=0, value=default_i_v, step=1)
with col13:
frac_h = st.selectbox('', lst_frac, key=f"frac_height_{i}")
height += float(Fraction(frac_h))
with col15:
width = st.number_input(f'Width for Item {i+1}:', key=f"width_{i}", min_value=0, value=default_i_v, step=1)
with col16:
frac_w = st.selectbox('', lst_frac, key=f"frac_width_{i}")
width += float(Fraction(frac_w))
reduced_shutter_width, reduced_shutter_height,reduced_shutter_area = calculate_reduced_dimensions(width, height, unit, 'shutter', object_item_value, item_type=option)
reduced_glass_width, reduced_glass_height,reduced_glass_area = calculate_reduced_dimensions(width, height, unit, 'glass', object_item_value, item_type=option)
# Prepare the results dictionary for the combined DataFrame
results = {
'Sl no': i + 1,
'Type': option,
'Outer Frame': f"{decimal_to_fraction(height)}\" | {decimal_to_fraction(width)}\"",
'Shutter': f"{decimal_to_fraction(reduced_shutter_height)}\" | {decimal_to_fraction(reduced_shutter_width)}\"",
'Glass': f"{decimal_to_fraction(reduced_glass_height)}\" | {decimal_to_fraction(reduced_glass_width)}\"",
'Area': f"{round(calculate_area_and_frame(width, height, unit, option)[0], 2)} Sq. Feet"
}
combined_results.append(results)
# Combine all results into one DataFrame
df_combined = pd.DataFrame(combined_results)
# Display the combined DataFrame
st.write(df_combined.drop('Sl no', axis=1))
# Generate PDF with the combined results
pdf_data = generate_pdf(df_combined, 'Alpa Pvt Ltd', '#123 HBR Layout', unit)
# PDF download button
# Buttons for Back and Print
col1, col2 = st.columns([2, 2])
with col1:
if st.button("Back"):
st.session_state.page = 0
st.rerun() # Go back to the first page
with col2:
pdf_button = st.download_button(
label="Download PDF",
data=pdf_data,
file_name="upvc_calculation_results.pdf",
mime="application/pdf"
)