Creating an Interactive Real Estate Map with Python and Streamlit
Written on
Chapter 1: Overview of the Project
In this project, a client requested a Python script to develop an interactive web application for showcasing real estate properties across the United States. I opted to utilize the Streamlit and Folium libraries for this task. The client specifically asked for the application to be executable locally on their desktop. The main aim of this tool is to present a map featuring markers for various real estate listings, which are organized in multiple CSV files, providing a visual reference for the customer.
The script starts by importing the essential libraries and loading the CSV files into Pandas dataframes. It also includes functions to obtain latitude and longitude data based on state abbreviations, along with managing user interactions on the map.
Section 1.1: Preparing the Data
After setting up the environment, the script initializes a map using Folium and configures default settings. It leverages the sidebar functionality in Streamlit to introduce filtering options for the properties shown on the map, allowing the user to filter by owner and state.
import streamlit as st
import folium
import pandas as pd
from streamlit_folium import st_folium
st.set_page_config(layout="wide")
# Load the states data
df_states = pd.read_csv('states.csv')
Subsection 1.1.1: Importing Required Libraries
Section 1.2: Adding Filtering Options
The script enables the addition of markers for chosen properties and renders the map within the Streamlit application using the st_folium function. This results in a web application that allows users to explore properties interactively.
Chapter 2: Code Implementation
To begin, I will present the initial version of my script that was submitted to the client, with minor modifications. Following that, I will share a more Pythonic rendition created by ChatGPT.
import streamlit as st
import folium
import pandas as pd
from streamlit_folium import st_folium
# Load CSV files into dataframes
df_states = pd.read_csv('states.csv')
df_i = pd.read_csv('ind_ll.csv')
df_p = pd.read_csv('prologis_ll.csv')
The video titled "How to Build a Raving Fan Club in Real Estate w/ Shannon Milligan" offers insights into creating a supportive network in the real estate sector.
Next, let's examine the second video.
The video "How Much To Charge as a Freelance Developer ($ -$$$)" discusses pricing strategies for freelance developers, providing valuable tips on how to set your rates effectively.
Here is the updated version of the script crafted by ChatGPT, which incorporates various improvements:
import streamlit as st
import folium
import pandas as pd
from streamlit_folium import st_folium
st.set_page_config(layout="wide")
# Load data from CSV files
df_states = pd.read_csv('states.csv')
df_i = pd.read_csv('ind_ll.csv').query('latitude > 0').reset_index(drop=True)
df_p = pd.read_csv('prologis_ll.csv').query('latitude > 0').reset_index(drop=True)
# Function definitions and map initialization
def get_lat_lon(abbrev):
row = df_states[df_states["Abbreviation"] == abbrev]
return tuple(row[["Latitude", "Longitude"]].values[0]) if not row.empty else (None, None)
m = folium.Map(location=[39.063946, -76.802101], zoom_start=5)
# Sidebar filters for owner and state
prologis_owners = df_p['True Owner Name'].unique().tolist()
add_select = st.sidebar.selectbox("Filter Owner", ['All'] + prologis_owners)
prologis_states = ['US'] + sorted(df_p['State'].unique().tolist())
add_state = st.sidebar.selectbox("Filter State", prologis_states)
# Apply filters and update the map
if add_select != 'All':
df_p = df_p[df_p['True Owner Name'] == add_select]
if add_state != 'US':
df_p = df_p[df_p['State'] == add_state]
latitude, longitude = get_lat_lon(add_state)
m = folium.Map(location=[latitude, longitude], zoom_start=10)
# Add markers to the map for each property in the filtered dataset
for _, row in df_p.iterrows():
address = row['Property Address']
city = row['City']
folium.Marker([row['latitude'], row['longitude']], popup=f'{address}, {city}').add_to(m)
# Display the map in the Streamlit app
st_folium(m, width=2400)
This version is refined, removing unnecessary imports and utilizing more efficient coding practices. Each adjustment is documented, providing clarity on the enhancements made.
For further insights, visit PlainEnglish.io and consider subscribing to our weekly newsletter. Follow us on social media for more updates!