dibend's picture
Update app.py
266757a
import gradio as gr
import pandas as pd
import plotly.express as px
def plot_real_estate(zip):
# 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')
# Extract the data for zip code
df = df[df['RegionName'] == int(zip)]
# Select the columns with dates
df = df.loc[:, '2000-01-31':]
# Transpose the data
df = df.T.reset_index()
df.columns = ['Date', 'Price']
# Convert 'Date' to datetime
df['Date'] = pd.to_datetime(df['Date'])
# Compute the moving averages
df_ma3 = df['Price'].rolling(3).mean()
df_ma6 = df['Price'].rolling(6).mean()
df_ma12 = df['Price'].rolling(12).mean()
df_ma24 = df['Price'].rolling(24).mean()
# Plot the price data
fig = px.line(df, x='Date', y='Price', title=f'Housing Prices for Zip Code {zip}')
fig['data'][0]['showlegend'] = True
fig['data'][0]['name'] = 'Price'
# Plot the moving averages
fig.add_scatter(x=df['Date'], y=df_ma3, mode='lines', name='3-Month MA')
fig.add_scatter(x=df['Date'], y=df_ma6, mode='lines', name='6-Month MA')
fig.add_scatter(x=df['Date'], y=df_ma12, mode='lines', name='12-Month MA')
fig.add_scatter(x=df['Date'], y=df_ma24, mode='lines', name='24-Month MA')
# Add axis labels
fig.update_layout(xaxis_title='Date', yaxis_title='Price')
return fig
iface = gr.Interface(fn=plot_real_estate,
inputs=[gr.components.Textbox(label="Zip Code")],
outputs=gr.Plot())
iface.launch(share=False, debug=True)