dibend commited on
Commit
4ed3661
1 Parent(s): b808703

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import plotly.express as px
4
+
5
+ def plot_real_estate(state):
6
+ # Read the CSV file
7
+ 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')
8
+
9
+ # Filter for the given state
10
+ df_state = df[df['State'] == state.upper()]
11
+
12
+ # Extract unique ZIP codes for the state
13
+ zip_codes = df_state['RegionName'].unique()
14
+
15
+ # Prepare an empty figure for plotting
16
+ fig = px.Figure()
17
+
18
+ # Loop through each ZIP code and add a plot line
19
+ for zip_code in zip_codes:
20
+ df_zip = df_state[df_state['RegionName'] == zip_code]
21
+
22
+ # Select the columns with dates and transpose the data
23
+ df_zip = df_zip.loc[:, '2000-01-31':].T.reset_index()
24
+ df_zip.columns = ['Date', 'Price']
25
+
26
+ # Convert 'Date' to datetime
27
+ df_zip['Date'] = pd.to_datetime(df_zip['Date'])
28
+
29
+ # Add a line to the plot for this ZIP code
30
+ fig.add_scatter(x=df_zip['Date'], y=df_zip['Price'], mode='lines', name=str(zip_code))
31
+
32
+ # Update plot layout
33
+ fig.update_layout(title=f'Housing Prices in {state}',
34
+ xaxis_title='Date',
35
+ yaxis_title='Price')
36
+ return fig
37
+
38
+ iface = gr.Interface(fn=plot_real_estate,
39
+ inputs=[gr.components.Textbox(label="State (e.g., NJ for New Jersey)")],
40
+ outputs=gr.Plot())
41
+
42
+ iface.launch(share=False, debug=True)