app for renting property
To upload files, please first save the app
import streamlit as st
from sqlalchemy import create_engine, Column, Integer, String, Float, Boolean
from sqlalchemy.orm import Session, DeclarativeBase
import pandas as pd
# Database setup
class Base(DeclarativeBase):
pass
class Property(Base):
__tablename__ = 'properties'
id = Column(Integer, primary_key=True)
title = Column(String)
address = Column(String)
price = Column(Float)
bedrooms = Column(Integer)
bathrooms = Column(Integer)
available = Column(Boolean, default=True)
engine = create_engine("sqlite:///rental.sqlite")
Base.metadata.create_all(bind=engine)
# App
st.title("Property Rental Management")
# Sidebar navigation
page = st.sidebar.selectbox("Select Page", ["View Properties", "Add Property", "Manage Properties"])
if page == "View Properties":
st.header("Available Properties")
with Session(engine) as session:
properties = session.query(Property).filter_by(available=True).all()
if properties:
data = [{
"Title": p.title,
"Address": p.address,
"Price/month": f"${p.price:,.2f}",
"Bedrooms": p.bedrooms,
"Bathrooms": p.bathrooms
} for p in properties]
df = pd.DataFrame(data)
st.table(df)
else:
st.info("No properties available for rent at the moment.")
elif page == "Add Property":
st.header("Add New Property")
with st.form("add_property"):
title = st.text_input("Property Title")
address = st.text_input("Address")
price = st.number_input("Monthly Rent ($)", min_value=0.0, step=100.0)
bedrooms = st.number_input("Number of Bedrooms", min_value=0, step=1)
bathrooms = st.number_input("Number of Bathrooms", min_value=0, step=1)
if st.form_submit_button("Add Property"):
with Session(engine) as session:
new_property = Property(
title=title,
address=address,
price=price,
bedrooms=bedrooms,
bathrooms=bathrooms,
available=True
)
session.add(new_property)
session.commit()
st.success("Property added successfully!")
elif page == "Manage Properties":
st.header("Manage Properties")
with Session(engine) as session:
properties = session.query(Property).all()
if properties:
for prop in properties:
col1, col2 = st.columns([3, 1])
with col1:
st.write(f"**{prop.title}**")
st.write(f"Address: {prop.address}")
st.write(f"Price: ${prop.price:,.2f}/month")
st.write(f"Bedrooms: {prop.bedrooms}, Bathrooms: {prop.bathrooms}")
with col2:
status = "Available" if prop.available else "Rented"
if st.button(f"Mark as {'Rented' if prop.available else 'Available'}", key=f"btn_{prop.id}"):
prop.available = not prop.available
session.commit()
st.rerun()
st.divider()
else:
st.info("No properties in the database.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?