dibend's picture
Update app.py
cbd9d72
import gradio as gr
import pandas as pd
import plotly.graph_objects as go
def plot_real_estate(state):
# Read the CSV file
df = pd.read_csv('https://files.zillowstatic.com/research/public_csvs/zhvi/Zip_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv')
# Filter for the given state
df = df[df['State'] == state.upper()]
# Initialize a Plotly graph object
fig = go.Figure()
# Loop through each ZIP code in the state
for zip_code in df['RegionName'].unique():
df_zip = df[df['RegionName'] == zip_code]
# Select the columns with dates and transpose the data
df_zip = df_zip.loc[:, '2000-01-31':].T.reset_index()
df_zip.columns = ['Date', 'Price']
# Convert 'Date' to datetime
df_zip['Date'] = pd.to_datetime(df_zip['Date'])
# Plot the data for each ZIP code
fig.add_trace(go.Scatter(x=df_zip['Date'], y=df_zip['Price'], mode='lines', name=str(zip_code)))
# Update plot layout
fig.update_layout(title=f'Housing Prices in {state}',
xaxis_title='Date',
yaxis_title='Price')
return fig
iface = gr.Interface(fn=plot_real_estate,
inputs=[gr.components.Textbox(label="State (e.g., 'NJ' for New Jersey)")],
outputs=gr.Plot())
iface.launch(share=False, debug=True)