Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ def categorize_rank(r):
|
|
| 17 |
|
| 18 |
def process_data(file):
|
| 19 |
if file is None:
|
| 20 |
-
return None, None, None
|
| 21 |
|
| 22 |
excel = pd.ExcelFile(file.name)
|
| 23 |
first_sheet = excel.sheet_names[0]
|
|
@@ -72,36 +72,35 @@ def process_data(file):
|
|
| 72 |
x='Range',
|
| 73 |
y='Keyword Count',
|
| 74 |
color='Month',
|
| 75 |
-
barmode='group', #
|
| 76 |
text_auto=True,
|
| 77 |
-
title="Keyword Performance Distribution",
|
| 78 |
-
category_orders={"Range": correct_order}
|
| 79 |
)
|
| 80 |
-
# Enable clicking
|
| 81 |
-
fig.update_layout(clickmode='event+select', dragmode=False)
|
| 82 |
else:
|
| 83 |
fig = None
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
def filter_table(full_data,
|
| 88 |
-
if
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
# We can try to infer, but filtering by Range alone is safer and more robust
|
| 99 |
-
pass
|
| 100 |
-
|
| 101 |
-
# Filter Logic: Return keywords that match this range in ANY of the months
|
| 102 |
-
range_cols = [c for c in full_data.columns if "Rank Range" in c]
|
| 103 |
-
filtered_df = full_data[full_data[range_cols].apply(lambda row: clicked_range in row.values, axis=1)]
|
| 104 |
-
return filtered_df
|
| 105 |
|
| 106 |
def reset_table(full_data):
|
| 107 |
return full_data
|
|
@@ -114,28 +113,35 @@ with gr.Blocks() as demo:
|
|
| 114 |
|
| 115 |
with gr.Row():
|
| 116 |
file_input = gr.File(label="Upload SEO Excel", file_types=[".xlsx"])
|
| 117 |
-
|
| 118 |
-
process_btn = gr.Button("📊 Build Dashboard", variant="primary")
|
| 119 |
-
reset_btn = gr.Button("🔄 Reset Filters")
|
| 120 |
|
| 121 |
-
|
|
|
|
| 122 |
|
| 123 |
-
gr.Markdown("### 🔍
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
table_output = gr.DataFrame(interactive=False)
|
| 125 |
|
|
|
|
| 126 |
process_btn.click(
|
| 127 |
fn=process_data,
|
| 128 |
inputs=file_input,
|
| 129 |
-
outputs=[table_output, plot_output, full_data_state]
|
| 130 |
)
|
| 131 |
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
fn=filter_table,
|
| 135 |
-
inputs=[full_data_state],
|
| 136 |
outputs=table_output
|
| 137 |
)
|
| 138 |
|
|
|
|
| 139 |
reset_btn.click(
|
| 140 |
fn=reset_table,
|
| 141 |
inputs=full_data_state,
|
|
@@ -143,4 +149,4 @@ with gr.Blocks() as demo:
|
|
| 143 |
)
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
| 146 |
-
demo.launch(
|
|
|
|
| 17 |
|
| 18 |
def process_data(file):
|
| 19 |
if file is None:
|
| 20 |
+
return None, None, None, gr.update(choices=[]), gr.update(choices=[])
|
| 21 |
|
| 22 |
excel = pd.ExcelFile(file.name)
|
| 23 |
first_sheet = excel.sheet_names[0]
|
|
|
|
| 72 |
x='Range',
|
| 73 |
y='Keyword Count',
|
| 74 |
color='Month',
|
| 75 |
+
barmode='group', # Side-by-side bars
|
| 76 |
text_auto=True,
|
| 77 |
+
title="Keyword Performance Distribution (Grouped)",
|
| 78 |
+
category_orders={"Range": correct_order}
|
| 79 |
)
|
|
|
|
|
|
|
| 80 |
else:
|
| 81 |
fig = None
|
| 82 |
|
| 83 |
+
# Return data and update dropdown choices
|
| 84 |
+
unique_months = list(full_viz_df['Month'].unique()) if not full_viz_df.empty else []
|
| 85 |
+
return (
|
| 86 |
+
final_df,
|
| 87 |
+
fig,
|
| 88 |
+
final_df,
|
| 89 |
+
gr.Dropdown.update(choices=unique_months, value=unique_months[0] if unique_months else None),
|
| 90 |
+
gr.Dropdown.update(choices=correct_order)
|
| 91 |
+
)
|
| 92 |
|
| 93 |
+
def filter_table(full_data, selected_month, selected_range):
|
| 94 |
+
if full_data is None or full_data.empty: return None
|
| 95 |
|
| 96 |
+
# Filter by specific month AND range
|
| 97 |
+
target_col = f"{selected_month}_Rank Range"
|
| 98 |
|
| 99 |
+
if target_col in full_data.columns:
|
| 100 |
+
filtered_df = full_data[full_data[target_col] == selected_range]
|
| 101 |
+
return filtered_df
|
| 102 |
+
|
| 103 |
+
return full_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
def reset_table(full_data):
|
| 106 |
return full_data
|
|
|
|
| 113 |
|
| 114 |
with gr.Row():
|
| 115 |
file_input = gr.File(label="Upload SEO Excel", file_types=[".xlsx"])
|
| 116 |
+
process_btn = gr.Button("📊 Build Dashboard", variant="primary")
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
# The Chart
|
| 119 |
+
plot_output = gr.Plot(label="Keyword Distribution")
|
| 120 |
|
| 121 |
+
gr.Markdown("### 🔍 Filter Keywords")
|
| 122 |
+
with gr.Row():
|
| 123 |
+
month_dropdown = gr.Dropdown(label="Select Month", choices=[])
|
| 124 |
+
range_dropdown = gr.Dropdown(label="Select Rank Range", choices=[])
|
| 125 |
+
filter_btn = gr.Button("Apply Filter")
|
| 126 |
+
reset_btn = gr.Button("Show All")
|
| 127 |
+
|
| 128 |
table_output = gr.DataFrame(interactive=False)
|
| 129 |
|
| 130 |
+
# 1. Process File
|
| 131 |
process_btn.click(
|
| 132 |
fn=process_data,
|
| 133 |
inputs=file_input,
|
| 134 |
+
outputs=[table_output, plot_output, full_data_state, month_dropdown, range_dropdown]
|
| 135 |
)
|
| 136 |
|
| 137 |
+
# 2. Filter Table (Button Click)
|
| 138 |
+
filter_btn.click(
|
| 139 |
fn=filter_table,
|
| 140 |
+
inputs=[full_data_state, month_dropdown, range_dropdown],
|
| 141 |
outputs=table_output
|
| 142 |
)
|
| 143 |
|
| 144 |
+
# 3. Reset Table
|
| 145 |
reset_btn.click(
|
| 146 |
fn=reset_table,
|
| 147 |
inputs=full_data_state,
|
|
|
|
| 149 |
)
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
| 152 |
+
demo.launch()
|