dibend commited on
Commit
72811f3
·
verified ·
1 Parent(s): 46db2be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -15
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(area):
9
- df = pd.read_csv(CSV_URL)
10
- fig = go.Figure()
11
 
12
- # Add full data traces
13
- for col in df.columns[1:]:
14
- fig.add_trace(go.Scatter(x=df[df.columns[0]], y=df[col], mode='lines', name=col))
15
 
16
- # Add filtered trace if area is specified
17
- if area and 'Area' in df.columns:
18
- filtered_df = df[df['Area'].str.contains(area, case=False, na=False)]
19
- for col in filtered_df.columns[1:]:
20
- fig.add_trace(go.Scatter(x=filtered_df[filtered_df.columns[0]], y=filtered_df[col], mode='lines+markers', name=f"Filtered: {col} ({area})"))
 
21
 
22
- fig.update_layout(title="Full CSV Data Plot with Area Filter Overlay")
23
- return fig
 
24
 
25
- with gr.Blocks() as demo: gr.Markdown("## Interactive CSV Plotter (Full Plot + Area Filter Overlay)") area_input = gr.Textbox(label="Filter Area", placeholder="Enter area name to highlight") plot_output = gr.Plot() run_button = gr.Button("Plot") run_button.click(plot_csv, inputs=area_input, outputs=plot_output)
 
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)