|
import gradio as gr |
|
import pandas as pd |
|
import plotly.express as px |
|
|
|
def plot_real_estate(zip): |
|
|
|
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') |
|
|
|
|
|
df = df[df['RegionName'] == int(zip)] |
|
|
|
|
|
df = df.loc[:, '2000-01-31':] |
|
|
|
|
|
df = df.T.reset_index() |
|
df.columns = ['Date', 'Price'] |
|
|
|
|
|
df['Date'] = pd.to_datetime(df['Date']) |
|
|
|
|
|
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() |
|
|
|
|
|
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' |
|
|
|
|
|
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') |
|
|
|
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) |