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.
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)