-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
154 lines (134 loc) · 7.84 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#Import Python Libraries
import pandas as pd
import numpy as np
import datetime as dt
import folium
import geopandas as gpd
import streamlit as st
from streamlit_folium import folium_static
@st.cache_data
def read_csv(path):
return pd.read_csv(path, compression='gzip', sep='\t', quotechar='"')
housing_price_df = read_csv('./input/state_market_tracker.tsv000.gz')
housing_price_df = housing_price_df[['period_begin','period_end','period_duration','property_type','median_sale_price','median_sale_price_yoy','homes_sold','state_code']]
housing_price_df = housing_price_df[(housing_price_df['period_begin']>='2020-01-01') & (housing_price_df['period_begin']<='2023-07-01')]
@st.cache_data
def read_file(path):
return gpd.read_file(path)
#Read the geojson file
gdf = read_file('./input/georef-united-states-of-america-state.geojson')
#Merge the housing market data and geojson file into one dataframe
df_final = gdf.merge(housing_price_df, left_on="ste_stusps_code", right_on="state_code", how="outer").reset_index(drop=True) #, inplace=True)
df_final = df_final.drop(['ste_code','ste_name','ste_area_code','ste_type','ste_fp_code'], axis=1)
df_final = df_final.rename(columns={'period_begin':"Period",'property_type':"Type of Property",'median_sale_price':"Median Sale Price",
'median_sale_price_yoy':"Median Sale Price YoY",'homes_sold':"Homes Sold",'state_code':"State"})
#### MUST KEEP AS FLOATS, NO INT
# df_final["Median Sale Price"] = df_final["Median Sale Price"].astype(int)
# df_final["Median Sale Price YoY"] = df_final["Median Sale Price YoY"].astype(int)
# df_final["Homes Sold"] = df_final["Homes Sold"].astype(int)
#### MUST KEEP AS FLOATS, NO INT
df_final["Month"] = pd.to_datetime(df_final["Period"], format='%Y-%m-%d').dt.to_period('M')
#df_final["Month"] = df_final["Month"].astype(str) ##(int) ####pd.to_datetime(df_final['Month'], format='%b %Y')
#Add sidebar to the app
####st.sidebar.markdown("# Redfin Housing Data")
##st.sidebar.markdown("## July 2022 to July 2023")
#st.sidebar.title("Site Information")
st.sidebar.title("Developed at Artificial Intelligentsia, LLC")
#st.sidebar.markdown("# Developed by Artificial Intelligentsia, LLC")
st.sidebar.markdown("## Contact us for Competitive Intelligence Solutions")
#st.sidebar.markdown("### For Your Business")
st.sidebar.markdown("### https://artificialintelligentsia.com/")
st.sidebar.markdown(":blue[This app was built using Python and Streamlit to represent activity visualizations for the United States real estate data, public data sourced from Redfin.]")
st.sidebar.markdown(":gray[Developer: Robert W Schell, Data Scientist, Artificial Intelligentsia, LLC]")
st.sidebar.markdown(":gray[Github: https://github.com/schellrw/]")
st.sidebar.markdown(":gray[LinkedIn: https://linkedin.com/in/schellr/]")
st.sidebar.markdown(":gray[All data provided by Redfin, a national real estate brokerage firm: https://www.redfin.com/news/data-center/]")
st.sidebar.markdown(":gray[Public site repository: https://github.com/schellrw/streamlit_redfin]")
st.sidebar.markdown(":gray[MIT License]")
st.sidebar.markdown(":gray[Copyright (c) 2023 Artificial Intelligentsia, LLC]")
# st.sidebar.markdown("Email: [email protected]")
#Add title and subtitle to the main interface of the app
st.title("Redfin U.S. Real Estate Heatmap")
#st.markdown("## Competitive intelligence and easy-to-use technology platforms are just a click away.") # Hover over the map to view more details.")
####st.subheader("Your company's information paired with our market research and technology solutions are exaclty what you've been looking for.") ##] :red[exaclty what you've been looking for.]")
st.subheader("Actionable insights and opportunities for your business to :red[Pre Dominate the Competition]", divider="rainbow") #:blue[the competition.]", divider="rainbow")
#st.subheader(":red[Pre-Dominantly]", divider="rainbow")
# st.subheader('''
# :orange[### *What markets are you in? What metrics matter to you? How do you want your business to grow? How far do you want to go?*]''')
st.subheader("Let us be the competitive intelligence company that helps you find the best way to win!", divider="rainbow")
####st.subheader(":red[AND KEEP WINNING!]", divider="rainbow") #":green bc[Increase revenue, attract/retain customers, ")
st.markdown("### https://artificialintelligentsia.com/")
# st.markdown("##### MIT License Copyright (c) 2023 Robert W Schell")
#Create three columns/filters
col1, col2, col3 = st.columns(3) ########), col4 = st.columns(4)
with col1:
period_list = df_final['Month'].unique().tolist()
period_list.sort(reverse=True)
year_month = st.selectbox("Select Year-Month", period_list, index=0)
# with col2:
# state_list = df_final['State'].astype(str).unique().tolist()
# state_sorted = sorted(state_list) # state_list.sort(reverse=False)
# state = st.selectbox("Select State", state_sorted, index=0)
with col2:
prop_type = st.selectbox("View by Property Type", ['All Residential','Single Family Residential','Townhouse',
'Condo/Co-op','Single Units Only','Multi-Family (2-4 Unit)'], index=0)
with col3:
metrics = st.selectbox("Select Housing Metrics", ["Median Sale Price","Median Sale Price YoY", "Homes Sold"], index=0)
# update data frame based on user selections
df_final = df_final[df_final["Month"]==year_month]
#df_final = df_final[df_final["State"]==state]
df_final = df_final[df_final["Type of Property"]==prop_type]
df_final = df_final[["Month", 'ste_stusps_code', "Type of Property", metrics,'geometry']] #,'ste_stusps_code']]
# @st.cache_data
# def write_df(df):
# dfx = st.write(df)
# return dfx
# # Output write info
# df_io = write_df(df_final)
# df_io
#Initiate a folium map
m = folium.Map(location=[40, -96], zoom_start=4,tiles=None)
folium.TileLayer('CartoDB positron', name="Light Map", control=False).add_to(m)
## Other map layers:
##folium.TileLayer('DarkMatter',name="Dark Map",control=False).add_to(m) #### folium.TileLayer('OpenStreetMap').add_to(m)
#Plot Choropleth map using folium
choropleth1 = folium.Choropleth(
geo_data='./input/georef-united-states-of-america-state.geojson', ##df_final.to_json(), #### # Geojson file for the United States
name="Choropleth (Heat Map) of U.S. Housing Prices",
data=df_final, # df from the data preparation and user selection
columns=['ste_stusps_code', metrics], # Or "State" now is key? # 'state code' and 'metrics' to get the median sales price for each state
key_on='feature.properties.ste_stusps_code', # key in the geojson file that we use to grab each state boundary layers
fill_color='YlGn',
nan_fill_color="White",
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Housing Market Metrics',
highlight=True,
line_color='black').geojson.add_to(m)
folium_static(m)
#Add tooltips to the map
geojson1 = folium.features.GeoJson(
data=df_final['geometry'],
name='United States Housing Prices',
smooth_factor=2,
style_function=lambda x: {'color':'black','fillColor':'transparent','weight':0.5},
tooltip=folium.features.GeoJsonTooltip(
fields=["Month",
"state_code",
metrics,],
aliases=['Period',
'ste_stusps_code',
metrics+':'],
localize=True,
sticky=False,
labels=True,
style="""
background-color: #F0EFEF;
border: 2px solid black;
border-radius: 3px;
box-shadow: 3px;
""",
max_width=800,),
highlight_function=lambda x: {'weight':3,'fillColor':'grey'},
).add_to(m)
#st.write(df_final)