|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
import plotly.graph_objects as go |
|
|
|
|
|
|
|
|
CSV_URL = "https://gardenstatemls.stats.showingtime.com/infoserv/s-v1/kpou-Asg" |
|
|
|
|
|
def plot_csv(): |
|
|
""" |
|
|
Reads data from the specified URL, processes it, and creates a plot. |
|
|
|
|
|
This function skips the first 9 rows of the CSV, which contain metadata, |
|
|
and removes any empty columns before plotting the data. |
|
|
|
|
|
Returns: |
|
|
A Plotly figure object showing the median sales price over time. |
|
|
""" |
|
|
try: |
|
|
|
|
|
df = pd.read_csv(CSV_URL, skiprows=9) |
|
|
|
|
|
|
|
|
if 'Unnamed: 2' in df.columns: |
|
|
df = df.drop(columns=['Unnamed: 2']) |
|
|
|
|
|
|
|
|
df.columns = ['Date', 'Median Sales Price'] |
|
|
|
|
|
|
|
|
fig = go.Figure() |
|
|
|
|
|
|
|
|
fig.add_trace(go.Scatter(x=df['Date'], y=df['Median Sales Price'], mode='lines', name='Median Sales Price')) |
|
|
|
|
|
|
|
|
fig.update_layout( |
|
|
title="Median Sales Price - Entire MLS", |
|
|
xaxis_title="Date", |
|
|
yaxis_title="Median Sales Price ($)", |
|
|
showlegend=True |
|
|
) |
|
|
|
|
|
return fig |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
|
|
|
error_message = f"An error occurred: {e}. Please check the console for details." |
|
|
print(error_message) |
|
|
|
|
|
fig = go.Figure() |
|
|
fig.update_layout( |
|
|
title="Error Loading Data", |
|
|
xaxis={"visible": False}, |
|
|
yaxis={"visible": False}, |
|
|
annotations=[ |
|
|
{ |
|
|
"text": "Could not load data from the source URL.<br>Please check the connection or URL.", |
|
|
"xref": "paper", |
|
|
"yref": "paper", |
|
|
"showarrow": False, |
|
|
"font": { |
|
|
"size": 16 |
|
|
} |
|
|
} |
|
|
] |
|
|
) |
|
|
return fig |
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("## Median Sales Price Plotter") |
|
|
plot_output = gr.Plot() |
|
|
run_button = gr.Button("Plot Data") |
|
|
|
|
|
|
|
|
gr.Markdown( |
|
|
""" |
|
|
--- |
|
|
*Data provided by the Garden State Multiple Listing Service.* |
|
|
""" |
|
|
) |
|
|
|
|
|
|
|
|
run_button.click(plot_csv, inputs=None, outputs=plot_output) |
|
|
|
|
|
|
|
|
demo.launch(debug=True) |