Update app.py
Browse files
app.py
CHANGED
|
@@ -23,6 +23,7 @@ def process_data(file):
|
|
| 23 |
first_sheet = excel.sheet_names[0]
|
| 24 |
df = pd.read_excel(file.name, sheet_name=first_sheet)
|
| 25 |
|
|
|
|
| 26 |
df.columns = df.columns.map(lambda x: str(x).strip())
|
| 27 |
non_date_cols = ["Keyword", "Avg. monthly searches"]
|
| 28 |
available_non_date = [c for c in non_date_cols if c in df.columns]
|
|
@@ -40,6 +41,7 @@ def process_data(file):
|
|
| 40 |
df.rename(columns=rename_dict, inplace=True)
|
| 41 |
date_cols = list(rename_dict.values())
|
| 42 |
|
|
|
|
| 43 |
final_df = df[available_non_date].copy()
|
| 44 |
viz_data = []
|
| 45 |
|
|
@@ -56,21 +58,27 @@ def process_data(file):
|
|
| 56 |
|
| 57 |
full_viz_df = pd.concat(viz_data)
|
| 58 |
|
| 59 |
-
#
|
| 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(['
|
| 63 |
|
| 64 |
return final_df, full_viz_df, final_df
|
| 65 |
|
| 66 |
def filter_table(full_data, selection_data: gr.SelectData):
|
| 67 |
-
# selection_data.value
|
|
|
|
|
|
|
|
|
|
| 68 |
clicked_range = selection_data.value[0]
|
| 69 |
|
| 70 |
-
#
|
| 71 |
range_cols = [c for c in full_data.columns if "Rank Range" in c]
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
| 74 |
filtered_df = full_data[full_data[range_cols].apply(lambda row: clicked_range in row.values, axis=1)]
|
| 75 |
return filtered_df
|
| 76 |
|
|
@@ -81,7 +89,7 @@ with gr.Blocks() as demo:
|
|
| 81 |
full_data_state = gr.State()
|
| 82 |
|
| 83 |
gr.Markdown("# 🚀 SEO Market Intelligence Dashboard")
|
| 84 |
-
gr.Markdown("###
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
file_input = gr.File(label="Upload SEO Excel", file_types=[".xlsx"])
|
|
@@ -89,14 +97,14 @@ with gr.Blocks() as demo:
|
|
| 89 |
process_btn = gr.Button("📊 Build Dashboard", variant="primary")
|
| 90 |
reset_btn = gr.Button("🔄 Reset Table Filters")
|
| 91 |
|
| 92 |
-
#
|
|
|
|
| 93 |
plot_output = gr.BarPlot(
|
| 94 |
x="Range",
|
| 95 |
y="Keyword Count",
|
| 96 |
color="Month",
|
| 97 |
title="Keyword Performance Distribution",
|
| 98 |
-
tooltip=["Month", "Range", "Keyword Count"]
|
| 99 |
-
group="Month"
|
| 100 |
)
|
| 101 |
|
| 102 |
gr.Markdown("### 🔍 Keywords List")
|
|
@@ -121,5 +129,4 @@ with gr.Blocks() as demo:
|
|
| 121 |
)
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
-
# Standard launch with theme support
|
| 125 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 23 |
first_sheet = excel.sheet_names[0]
|
| 24 |
df = pd.read_excel(file.name, sheet_name=first_sheet)
|
| 25 |
|
| 26 |
+
# Clean and Standardize Columns
|
| 27 |
df.columns = df.columns.map(lambda x: str(x).strip())
|
| 28 |
non_date_cols = ["Keyword", "Avg. monthly searches"]
|
| 29 |
available_non_date = [c for c in non_date_cols if c in df.columns]
|
|
|
|
| 41 |
df.rename(columns=rename_dict, inplace=True)
|
| 42 |
date_cols = list(rename_dict.values())
|
| 43 |
|
| 44 |
+
# Build Visualization Data
|
| 45 |
final_df = df[available_non_date].copy()
|
| 46 |
viz_data = []
|
| 47 |
|
|
|
|
| 58 |
|
| 59 |
full_viz_df = pd.concat(viz_data)
|
| 60 |
|
| 61 |
+
# Sort SEO buckets correctly
|
| 62 |
sort_order = ["01–03", "04–10", "11–20", "21–30", "31–40", "41–50", "51–100", "100+", "Not Talking (Intent Not Correct)"]
|
| 63 |
full_viz_df['Range'] = pd.Categorical(full_viz_df['Range'], categories=sort_order, ordered=True)
|
| 64 |
+
full_viz_df = full_viz_df.sort_values(['Range', 'Month'])
|
| 65 |
|
| 66 |
return final_df, full_viz_df, final_df
|
| 67 |
|
| 68 |
def filter_table(full_data, selection_data: gr.SelectData):
|
| 69 |
+
# When clicking a bar, selection_data.value[0] is the X-axis value (The Range, e.g., "01-03")
|
| 70 |
+
if not selection_data or not selection_data.value:
|
| 71 |
+
return full_data
|
| 72 |
+
|
| 73 |
clicked_range = selection_data.value[0]
|
| 74 |
|
| 75 |
+
# Filter Logic: Find rows where this range appears in ANY month column
|
| 76 |
range_cols = [c for c in full_data.columns if "Rank Range" in c]
|
| 77 |
|
| 78 |
+
if not range_cols:
|
| 79 |
+
return full_data
|
| 80 |
+
|
| 81 |
+
# We filter the master dataframe to show keywords that have this rank in any tracked month
|
| 82 |
filtered_df = full_data[full_data[range_cols].apply(lambda row: clicked_range in row.values, axis=1)]
|
| 83 |
return filtered_df
|
| 84 |
|
|
|
|
| 89 |
full_data_state = gr.State()
|
| 90 |
|
| 91 |
gr.Markdown("# 🚀 SEO Market Intelligence Dashboard")
|
| 92 |
+
gr.Markdown("### Click a bar segment below to filter the keyword list.")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
file_input = gr.File(label="Upload SEO Excel", file_types=[".xlsx"])
|
|
|
|
| 97 |
process_btn = gr.Button("📊 Build Dashboard", variant="primary")
|
| 98 |
reset_btn = gr.Button("🔄 Reset Table Filters")
|
| 99 |
|
| 100 |
+
# FIX: Removed 'group' and 'vertical' arguments.
|
| 101 |
+
# 'color' handles the segmentation automatically.
|
| 102 |
plot_output = gr.BarPlot(
|
| 103 |
x="Range",
|
| 104 |
y="Keyword Count",
|
| 105 |
color="Month",
|
| 106 |
title="Keyword Performance Distribution",
|
| 107 |
+
tooltip=["Month", "Range", "Keyword Count"]
|
|
|
|
| 108 |
)
|
| 109 |
|
| 110 |
gr.Markdown("### 🔍 Keywords List")
|
|
|
|
| 129 |
)
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
|
|
|
| 132 |
demo.launch(theme=gr.themes.Soft())
|