Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,10 @@ from script import getSearchResult, getClustersWithGraph, compare_clusters
|
|
7 |
|
8 |
app = marimo.App()
|
9 |
|
10 |
-
# Cell 1:
|
11 |
@app.cell
|
12 |
def cell_config(mo):
|
13 |
-
|
14 |
api_key = os.environ.get("GOOGLE_API_KEY", "")
|
15 |
cse_id = os.environ.get("GOOGLE_CSE_ID", "")
|
16 |
country = mo.ui.text(label="Country Code (e.g. UK)", value="UK")
|
@@ -18,12 +18,6 @@ def cell_config(mo):
|
|
18 |
database= mo.ui.text(label="SQLite DB Name", value="data.db")
|
19 |
serp_table = mo.ui.text(label="SERP Table", value="keywords_serps")
|
20 |
cluster_table= mo.ui.text(label="Cluster Table", value="keyword_clusters")
|
21 |
-
run_button = mo.ui.button(label="Run Clustering Comparison")
|
22 |
-
return csv_upload, api_key, cse_id, country, language, database, serp_table, cluster_table, run_button
|
23 |
-
|
24 |
-
# Cell 2: Timestamp Selectors
|
25 |
-
@app.cell
|
26 |
-
def cell_timestamps(mo):
|
27 |
timestamp_options = [
|
28 |
("Latest available (max)", "max"),
|
29 |
("March 2024 core update (2024-04-26 00:00:00.000000)", "2024-04-26 00:00:00.000000"),
|
@@ -33,35 +27,32 @@ def cell_timestamps(mo):
|
|
33 |
("March 2025 core update (2025-04-03 00:00:00.000000)", "2025-04-03 00:00:00.000000"),
|
34 |
("June 2025 core update (2025-07-24 00:00:00.000000)", "2025-07-24 00:00:00.000000"),
|
35 |
]
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
39 |
|
40 |
-
# Cell
|
41 |
@app.cell
|
42 |
-
def
|
43 |
-
if
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
|
49 |
-
#
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
# Compare
|
60 |
-
movement = compare_clusters(clusters1, clusters2)
|
61 |
-
return fig1, fig2, movement
|
62 |
-
return None, None, None
|
63 |
|
64 |
-
# Cell
|
65 |
@app.cell
|
66 |
def cell_display_graphs(fig1, fig2):
|
67 |
if fig1 is not None and fig2 is not None:
|
@@ -69,10 +60,11 @@ def cell_display_graphs(fig1, fig2):
|
|
69 |
display(fig2)
|
70 |
return None
|
71 |
|
72 |
-
# Cell
|
73 |
@app.cell
|
74 |
def cell_display_movement(movement):
|
75 |
if movement is not None:
|
|
|
76 |
movement.sort_values(by="searchTerms", inplace=True)
|
77 |
movement.reset_index(drop=True, inplace=True)
|
78 |
return movement
|
|
|
7 |
|
8 |
app = marimo.App()
|
9 |
|
10 |
+
# Cell 1: Configuration Inputs
|
11 |
@app.cell
|
12 |
def cell_config(mo):
|
13 |
+
csv_file = mo.ui.file(label="Upload Keyword CSV (1 column)")
|
14 |
api_key = os.environ.get("GOOGLE_API_KEY", "")
|
15 |
cse_id = os.environ.get("GOOGLE_CSE_ID", "")
|
16 |
country = mo.ui.text(label="Country Code (e.g. UK)", value="UK")
|
|
|
18 |
database= mo.ui.text(label="SQLite DB Name", value="data.db")
|
19 |
serp_table = mo.ui.text(label="SERP Table", value="keywords_serps")
|
20 |
cluster_table= mo.ui.text(label="Cluster Table", value="keyword_clusters")
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
timestamp_options = [
|
22 |
("Latest available (max)", "max"),
|
23 |
("March 2024 core update (2024-04-26 00:00:00.000000)", "2024-04-26 00:00:00.000000"),
|
|
|
27 |
("March 2025 core update (2025-04-03 00:00:00.000000)", "2025-04-03 00:00:00.000000"),
|
28 |
("June 2025 core update (2025-07-24 00:00:00.000000)", "2025-07-24 00:00:00.000000"),
|
29 |
]
|
30 |
+
ts1 = mo.ui.dropdown(timestamp_options, label="Timestamp 1")
|
31 |
+
ts2 = mo.ui.dropdown(timestamp_options, label="Timestamp 2")
|
32 |
+
run_btn = mo.ui.button(label="Run Comparison")
|
33 |
+
return csv_file, api_key, cse_id, country, language, database, serp_table, cluster_table, ts1, ts2, run_btn
|
34 |
|
35 |
+
# Cell 2: Fetch, Cluster, Compare
|
36 |
@app.cell
|
37 |
+
def cell_process(csv_file, api_key, cse_id, country, language, database, serp_table, cluster_table, ts1, ts2, run_btn):
|
38 |
+
if not (run_btn.clicked and csv_file and csv_file.value):
|
39 |
+
return None, None, None
|
40 |
+
# Load keywords
|
41 |
+
df_kw = pd.read_csv(io.BytesIO(csv_file.value.read()))
|
42 |
+
keywords = df_kw.iloc[:, 0].tolist()
|
43 |
|
44 |
+
# First timestamp
|
45 |
+
getSearchResult(keywords, language, country, api_key, cse_id, database, serp_table)
|
46 |
+
fig1, clusters1 = getClustersWithGraph(database, serp_table, ts1.value)
|
47 |
+
|
48 |
+
# Second timestamp
|
49 |
+
getSearchResult(keywords, language, country, api_key, cse_id, database, serp_table)
|
50 |
+
fig2, clusters2 = getClustersWithGraph(database, serp_table, ts2.value)
|
51 |
+
|
52 |
+
movement = compare_clusters(clusters1, clusters2)
|
53 |
+
return fig1, fig2, movement
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
# Cell 3: Display Graphs
|
56 |
@app.cell
|
57 |
def cell_display_graphs(fig1, fig2):
|
58 |
if fig1 is not None and fig2 is not None:
|
|
|
60 |
display(fig2)
|
61 |
return None
|
62 |
|
63 |
+
# Cell 4: Display Movement Table
|
64 |
@app.cell
|
65 |
def cell_display_movement(movement):
|
66 |
if movement is not None:
|
67 |
+
movement = movement.copy()
|
68 |
movement.sort_values(by="searchTerms", inplace=True)
|
69 |
movement.reset_index(drop=True, inplace=True)
|
70 |
return movement
|