import streamlit as st def generate_slab_data(): # Inject custom CSS for reducing the gap st.markdown( """ """, unsafe_allow_html=True, ) # Initialize session state for each slab for i in range(1, 4): if f"range_{i}" not in st.session_state: st.session_state[f"range_{i}"] = "" if f"commercial_{i}" not in st.session_state: st.session_state[f"commercial_{i}"] = 0.0 # Function to get input for each slab def get_slab_input(index): # Header row: Align Slab and Commercial Value headers col1, col2 = st.columns([3, 2]) # Adjust column width ratio for alignment with col1: slab_header = f"""

Slab {index}

""" st.markdown(slab_header, unsafe_allow_html=True) with col2: commercial_header = f"""

Commercial Value

""" st.markdown(commercial_header, unsafe_allow_html=True) # Input row: Align Slab range and Commercial Value input fields col3, col4 = st.columns([3, 2]) # Adjust column widths for alignment with col3: slab_range = st.text_input( f"Enter range for Slab {index} (e.g., 0-50000)", value=st.session_state[f"range_{index}"], key=f"range_input_{index}", on_change=lambda: update_session_state(f"range_{index}", st.session_state[f"range_input_{index}"]) ) with col4: commercial_value = st.number_input( f"", min_value=0.0, step=0.1, value=st.session_state[f"commercial_{index}"], key=f"commercial_input_{index}", on_change=lambda: update_session_state(f"commercial_{index}", round(st.session_state[f"commercial_input_{index}"], 2)) ) # Generate formatted slab string if inputs are valid return f"{index}_{slab_range.replace('-', '_')}_{round(commercial_value, 2)}" if slab_range and commercial_value > 0 else "" # Update session state function def update_session_state(key, value): st.session_state[key] = value # Generate inputs dynamically for each slab slabs = [] for i in range(1, 4): slabs.append(get_slab_input(i)) # Generate and display the output string slab_data = " | ".join(filter(None, slabs)) if slab_data: plan_info_header = """

Slab data:

""" # Format slab_data with the same gradient style formatted_slab_data = f"""

{slab_data}

""" # Display the header and formatted slab data st.markdown(plan_info_header, unsafe_allow_html=True) st.markdown(formatted_slab_data, unsafe_allow_html=True) return slab_data else: st.warning("Please fill in all required fields for at least one slab!") # Call the function generate_slab_data()