File size: 1,356 Bytes
4ed3661
 
cbd9d72
4ed3661
 
 
 
 
 
cbd9d72
4ed3661
cbd9d72
 
4ed3661
cbd9d72
 
 
4ed3661
 
 
 
 
 
 
 
cbd9d72
 
4ed3661
 
 
 
 
cbd9d72
4ed3661
 
 
cbd9d72
4ed3661
 
 
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
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)