Update app.py
Browse files
app.py
CHANGED
|
@@ -56,23 +56,21 @@ 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('Range')
|
| 63 |
|
| 64 |
return final_df, full_viz_df, final_df
|
| 65 |
|
| 66 |
def filter_table(full_data, selection_data: gr.SelectData):
|
| 67 |
-
#
|
| 68 |
-
# selection_data.index contains the index of the month (color)
|
| 69 |
clicked_range = selection_data.value[0]
|
| 70 |
|
| 71 |
-
#
|
| 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
|
| 76 |
filtered_df = full_data[full_data[range_cols].apply(lambda row: clicked_range in row.values, axis=1)]
|
| 77 |
return filtered_df
|
| 78 |
|
|
@@ -83,7 +81,7 @@ 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
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
file_input = gr.File(label="Upload SEO Excel", file_types=[".xlsx"])
|
|
@@ -91,14 +89,13 @@ with gr.Blocks() as demo:
|
|
| 91 |
process_btn = gr.Button("📊 Build Dashboard", variant="primary")
|
| 92 |
reset_btn = gr.Button("🔄 Reset Table Filters")
|
| 93 |
|
| 94 |
-
#
|
| 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 |
|
|
@@ -111,7 +108,6 @@ with gr.Blocks() as demo:
|
|
| 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,4 +121,5 @@ with gr.Blocks() as demo:
|
|
| 125 |
)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
|
|
|
| 128 |
demo.launch(theme=gr.themes.Soft())
|
|
|
|
| 56 |
|
| 57 |
full_viz_df = pd.concat(viz_data)
|
| 58 |
|
| 59 |
+
# Define and sort 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(['Month', 'Range'])
|
| 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 contains the [Range, Keyword Count]
|
|
|
|
| 68 |
clicked_range = selection_data.value[0]
|
| 69 |
|
| 70 |
+
# Since we want to find keywords in this range across ANY month in the dataset
|
|
|
|
| 71 |
range_cols = [c for c in full_data.columns if "Rank Range" in c]
|
| 72 |
|
| 73 |
+
# Filter rows where the clicked range appears in at least one month's column
|
| 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 |
full_data_state = gr.State()
|
| 82 |
|
| 83 |
gr.Markdown("# 🚀 SEO Market Intelligence Dashboard")
|
| 84 |
+
gr.Markdown("### Interactive Analysis: Click a bar below to filter the table by rank range.")
|
| 85 |
|
| 86 |
with gr.Row():
|
| 87 |
file_input = gr.File(label="Upload SEO Excel", file_types=[".xlsx"])
|
|
|
|
| 89 |
process_btn = gr.Button("📊 Build Dashboard", variant="primary")
|
| 90 |
reset_btn = gr.Button("🔄 Reset Table Filters")
|
| 91 |
|
| 92 |
+
# REMOVED 'vertical' to fix the TypeError
|
| 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 |
|
|
|
|
| 108 |
outputs=[table_output, plot_output, full_data_state]
|
| 109 |
)
|
| 110 |
|
|
|
|
| 111 |
plot_output.select(
|
| 112 |
fn=filter_table,
|
| 113 |
inputs=[full_data_state],
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
+
# Standard launch with theme support
|
| 125 |
demo.launch(theme=gr.themes.Soft())
|