File size: 2,855 Bytes
ecd79f1
 
 
874341a
72811f3
874341a
 
72811f3
 
 
874341a
72811f3
 
874341a
72811f3
 
 
 
 
 
874341a
72811f3
 
 
874341a
72811f3
46a4330
874341a
72811f3
 
 
 
 
 
 
 
46a4330
72811f3
 
 
 
 
 
 
 
 
46a4330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72811f3
 
 
 
 
 
 
46a4330
 
 
 
 
980da7c
46a4330
 
72811f3
 
 
 
 
874341a
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
import pandas as pd
import plotly.graph_objects as go

# The URL from your original script
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:
        # Read the CSV from the URL, skipping the metadata at the top
        df = pd.read_csv(CSV_URL, skiprows=9)

        # Clean up the DataFrame
        if 'Unnamed: 2' in df.columns:
            df = df.drop(columns=['Unnamed: 2'])

        # Rename columns for easier use
        df.columns = ['Date', 'Median Sales Price']

        # Create a Plotly figure
        fig = go.Figure()

        # Add the data series to the plot
        fig.add_trace(go.Scatter(x=df['Date'], y=df['Median Sales Price'], mode='lines', name='Median Sales Price'))

        # Update the plot layout for a professional look
        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:
        # If there's an error fetching or plotting the data, return the error message
        # This is useful for debugging connection issues with the URL
        error_message = f"An error occurred: {e}. Please check the console for details."
        print(error_message) # Print full error to console
        # Create an empty plot with an error annotation
        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


# Set up the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("## Median Sales Price Plotter")
    plot_output = gr.Plot()
    run_button = gr.Button("Plot Data")
    
    # Add the data source disclosure at the bottom
    gr.Markdown(
        """
        ---
        *Data provided by the Garden State Multiple Listing Service.*
        """
    )

    # Link the button to the plotting function
    run_button.click(plot_csv, inputs=None, outputs=plot_output)

# Launch the Gradio app
demo.launch(debug=True)