Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import plotly.express as px
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
|
|
|
|
| 6 |
CSV_URL = "https://gardenstatemls.stats.showingtime.com/infoserv/s-v1/kpou-Asg"
|
| 7 |
|
| 8 |
-
def plot_csv(
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
fig.add_trace(go.Scatter(x=df[df.columns[0]], y=df[col], mode='lines', name=col))
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
demo.launch(debug=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
|
| 5 |
+
# The URL from your original script
|
| 6 |
CSV_URL = "https://gardenstatemls.stats.showingtime.com/infoserv/s-v1/kpou-Asg"
|
| 7 |
|
| 8 |
+
def plot_csv():
|
| 9 |
+
"""
|
| 10 |
+
Reads data from the specified URL, processes it, and creates a plot.
|
| 11 |
|
| 12 |
+
This function skips the first 9 rows of the CSV, which contain metadata,
|
| 13 |
+
and removes any empty columns before plotting the data.
|
|
|
|
| 14 |
|
| 15 |
+
Returns:
|
| 16 |
+
A Plotly figure object showing the median sales price over time.
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
# Read the CSV from the URL, skipping the metadata at the top
|
| 20 |
+
df = pd.read_csv(CSV_URL, skiprows=9)
|
| 21 |
|
| 22 |
+
# Clean up the DataFrame
|
| 23 |
+
if 'Unnamed: 2' in df.columns:
|
| 24 |
+
df = df.drop(columns=['Unnamed: 2'])
|
| 25 |
|
| 26 |
+
# Rename columns for easier use
|
| 27 |
+
df.columns = ['Date', 'Median Sales Price']
|
| 28 |
|
| 29 |
+
# Create a Plotly figure
|
| 30 |
+
fig = go.Figure()
|
| 31 |
+
|
| 32 |
+
# Add the data series to the plot
|
| 33 |
+
fig.add_trace(go.Scatter(x=df['Date'], y=df['Median Sales Price'], mode='lines', name='Median Sales Price'))
|
| 34 |
+
|
| 35 |
+
# Update the plot layout for a professional look
|
| 36 |
+
fig.update_layout(
|
| 37 |
+
title="Median Sales Price - Entire MLS",
|
| 38 |
+
xaxis_title="Date",
|
| 39 |
+
yaxis_title="Median Sales Price ($)",
|
| 40 |
+
showlegend=True
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
return fig
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
# If there's an error fetching or plotting the data, return the error message
|
| 47 |
+
return str(e)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Set up the Gradio interface
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
gr.Markdown("## Median Sales Price Plotter")
|
| 53 |
+
plot_output = gr.Plot()
|
| 54 |
+
run_button = gr.Button("Plot Data")
|
| 55 |
+
|
| 56 |
+
# Link the button to the plotting function
|
| 57 |
+
run_button.click(plot_csv, inputs=None, outputs=plot_output)
|
| 58 |
+
|
| 59 |
+
# Launch the Gradio app
|
| 60 |
demo.launch(debug=True)
|