Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
-
import plotly.express as px
|
| 4 |
import io
|
| 5 |
|
| 6 |
# Helper function for categorization
|
|
@@ -45,11 +44,9 @@ def process_data(file):
|
|
| 45 |
viz_data = []
|
| 46 |
|
| 47 |
for col in date_cols:
|
| 48 |
-
rank_numeric_col = f"{col}_Rank_Numeric"
|
| 49 |
range_col_name = f"{col}_Rank Range"
|
| 50 |
-
|
| 51 |
raw_vals = pd.to_numeric(df[col], errors="coerce")
|
| 52 |
-
final_df[
|
| 53 |
final_df[range_col_name] = raw_vals.apply(categorize_rank)
|
| 54 |
|
| 55 |
counts = final_df[range_col_name].value_counts().reset_index()
|
|
@@ -58,41 +55,35 @@ def process_data(file):
|
|
| 58 |
viz_data.append(counts)
|
| 59 |
|
| 60 |
full_viz_df = pd.concat(viz_data)
|
|
|
|
|
|
|
| 61 |
sort_order = ["01–03", "04–10", "11–20", "21–30", "31–40", "41–50", "51–100", "100+", "Not Talking (Intent Not Correct)"]
|
| 62 |
full_viz_df['Range'] = pd.Categorical(full_viz_df['Range'], categories=sort_order, ordered=True)
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
full_viz_df, x='Range', y='Keyword Count', color='Month',
|
| 66 |
-
title="Keyword Performance Distribution Across All Months",
|
| 67 |
-
text_auto=True, barmode='group',
|
| 68 |
-
labels={'Range': 'Rank Range', 'Keyword Count': 'Number of Keywords'}
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
return final_df, fig, final_df
|
| 72 |
|
| 73 |
def filter_table(full_data, selection_data: gr.SelectData):
|
| 74 |
-
#
|
| 75 |
-
#
|
| 76 |
-
clicked_range = selection_data.value
|
| 77 |
-
# SelectData for Plotly provides a dictionary in .extra for legend details
|
| 78 |
-
clicked_month = selection_data.extra.get('label')
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
return full_data
|
| 86 |
|
| 87 |
def reset_table(full_data):
|
| 88 |
return full_data
|
| 89 |
|
| 90 |
-
# FIX 1: Removed theme from Blocks constructor
|
| 91 |
with gr.Blocks() as demo:
|
| 92 |
full_data_state = gr.State()
|
| 93 |
|
| 94 |
gr.Markdown("# 🚀 SEO Market Intelligence Dashboard")
|
| 95 |
-
gr.Markdown("### Click a bar in the chart to filter the keyword list
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
file_input = gr.File(label="Upload SEO Excel", file_types=[".xlsx"])
|
|
@@ -100,7 +91,16 @@ with gr.Blocks() as demo:
|
|
| 100 |
process_btn = gr.Button("📊 Build Dashboard", variant="primary")
|
| 101 |
reset_btn = gr.Button("🔄 Reset Table Filters")
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
gr.Markdown("### 🔍 Keywords List")
|
| 106 |
table_output = gr.DataFrame(interactive=False)
|
|
@@ -111,7 +111,7 @@ with gr.Blocks() as demo:
|
|
| 111 |
outputs=[table_output, plot_output, full_data_state]
|
| 112 |
)
|
| 113 |
|
| 114 |
-
#
|
| 115 |
plot_output.select(
|
| 116 |
fn=filter_table,
|
| 117 |
inputs=[full_data_state],
|
|
@@ -125,5 +125,4 @@ with gr.Blocks() as demo:
|
|
| 125 |
)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
-
# FIX 3: Moved theme to launch()
|
| 129 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import io
|
| 4 |
|
| 5 |
# Helper function for categorization
|
|
|
|
| 44 |
viz_data = []
|
| 45 |
|
| 46 |
for col in date_cols:
|
|
|
|
| 47 |
range_col_name = f"{col}_Rank Range"
|
|
|
|
| 48 |
raw_vals = pd.to_numeric(df[col], errors="coerce")
|
| 49 |
+
final_df[f"{col}_Rank_Numeric"] = raw_vals.fillna(0).astype(int)
|
| 50 |
final_df[range_col_name] = raw_vals.apply(categorize_rank)
|
| 51 |
|
| 52 |
counts = final_df[range_col_name].value_counts().reset_index()
|
|
|
|
| 55 |
viz_data.append(counts)
|
| 56 |
|
| 57 |
full_viz_df = pd.concat(viz_data)
|
| 58 |
+
|
| 59 |
+
# Sort order for SEO buckets
|
| 60 |
sort_order = ["01–03", "04–10", "11–20", "21–30", "31–40", "41–50", "51–100", "100+", "Not Talking (Intent Not Correct)"]
|
| 61 |
full_viz_df['Range'] = pd.Categorical(full_viz_df['Range'], categories=sort_order, ordered=True)
|
| 62 |
+
full_viz_df = full_viz_df.sort_values('Range')
|
| 63 |
|
| 64 |
+
return final_df, full_viz_df, final_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
def filter_table(full_data, selection_data: gr.SelectData):
|
| 67 |
+
# For gr.BarPlot, selection_data.value contains the [x, y] values
|
| 68 |
+
# selection_data.index contains the index of the month (color)
|
| 69 |
+
clicked_range = selection_data.value[0]
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# We find the month by looking at the specific range columns
|
| 72 |
+
# This searches for the range in all Month columns
|
| 73 |
+
range_cols = [c for c in full_data.columns if "Rank Range" in c]
|
| 74 |
|
| 75 |
+
# Filter: Show rows where the clicked range appears in ANY month
|
| 76 |
+
filtered_df = full_data[full_data[range_cols].apply(lambda row: clicked_range in row.values, axis=1)]
|
| 77 |
+
return filtered_df
|
|
|
|
| 78 |
|
| 79 |
def reset_table(full_data):
|
| 80 |
return full_data
|
| 81 |
|
|
|
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
full_data_state = gr.State()
|
| 84 |
|
| 85 |
gr.Markdown("# 🚀 SEO Market Intelligence Dashboard")
|
| 86 |
+
gr.Markdown("### Click a bar in the chart to filter the keyword list below.")
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
file_input = gr.File(label="Upload SEO Excel", file_types=[".xlsx"])
|
|
|
|
| 91 |
process_btn = gr.Button("📊 Build Dashboard", variant="primary")
|
| 92 |
reset_btn = gr.Button("🔄 Reset Table Filters")
|
| 93 |
|
| 94 |
+
# FIX: Using gr.BarPlot which supports .select()
|
| 95 |
+
plot_output = gr.BarPlot(
|
| 96 |
+
x="Range",
|
| 97 |
+
y="Keyword Count",
|
| 98 |
+
color="Month",
|
| 99 |
+
title="Keyword Performance Distribution",
|
| 100 |
+
tooltip=["Month", "Range", "Keyword Count"],
|
| 101 |
+
vertical=True,
|
| 102 |
+
group="Month"
|
| 103 |
+
)
|
| 104 |
|
| 105 |
gr.Markdown("### 🔍 Keywords List")
|
| 106 |
table_output = gr.DataFrame(interactive=False)
|
|
|
|
| 111 |
outputs=[table_output, plot_output, full_data_state]
|
| 112 |
)
|
| 113 |
|
| 114 |
+
# Now .select() will work because plot_output is a BarPlot
|
| 115 |
plot_output.select(
|
| 116 |
fn=filter_table,
|
| 117 |
inputs=[full_data_state],
|
|
|
|
| 125 |
)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
|
|
|
| 128 |
demo.launch(theme=gr.themes.Soft())
|