Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,23 +3,26 @@ import pandas as pd
|
|
3 |
import io
|
4 |
import matplotlib.pyplot as plt
|
5 |
from script import getSearchResult, getClustersWithGraph, compare_clusters
|
|
|
6 |
|
7 |
app = marimo.App()
|
8 |
|
|
|
9 |
@app.cell
|
10 |
def _(mo):
|
11 |
csv_upload = mo.ui.file(label="Upload Keyword CSV (1 column)")
|
12 |
-
|
13 |
api_key = os.environ.get("GOOGLE_API_KEY", "")
|
14 |
-
cse_id
|
15 |
country = mo.ui.text(label="Country Code (e.g. UK)", value="UK")
|
16 |
-
language
|
17 |
-
database
|
18 |
-
serp_table
|
19 |
-
cluster_table
|
20 |
-
run_button
|
21 |
return csv_upload, api_key, cse_id, country, language, database, serp_table, cluster_table, run_button
|
22 |
|
|
|
23 |
@app.cell
|
24 |
def _(mo):
|
25 |
timestamp_options = [
|
@@ -31,11 +34,11 @@ def _(mo):
|
|
31 |
("March 2025 core update (2025-04-03 00:00:00.000000)", "2025-04-03 00:00:00.000000"),
|
32 |
("June 2025 core update (2025-07-24 00:00:00.000000)", "2025-07-24 00:00:00.000000"),
|
33 |
]
|
34 |
-
|
35 |
timestamp_1 = mo.ui.dropdown(timestamp_options, label="Choose Timestamp 1")
|
36 |
timestamp_2 = mo.ui.dropdown(timestamp_options, label="Choose Timestamp 2")
|
37 |
return timestamp_1, timestamp_2
|
38 |
|
|
|
39 |
@app.cell
|
40 |
def _(csv_upload):
|
41 |
if csv_upload.value:
|
@@ -45,25 +48,33 @@ def _(csv_upload):
|
|
45 |
df_keywords, keywords = None, []
|
46 |
return df_keywords, keywords
|
47 |
|
|
|
48 |
@app.cell
|
49 |
def _(keywords, api_key, cse_id, country, language, database, serp_table, timestamp_1, timestamp_2, run_button):
|
50 |
if run_button.clicked and keywords:
|
51 |
-
#
|
52 |
getSearchResult(keywords, language, country, api_key, cse_id, database, serp_table)
|
53 |
-
|
54 |
fig1, clusters1 = getClustersWithGraph(database, serp_table, timestamp_1.value)
|
|
|
|
|
|
|
55 |
fig2, clusters2 = getClustersWithGraph(database, serp_table, timestamp_2.value)
|
56 |
|
57 |
movement = compare_clusters(clusters1, clusters2)
|
58 |
return fig1, fig2, movement
|
|
|
|
|
59 |
return None, None, None
|
60 |
|
|
|
61 |
@app.cell
|
62 |
def _(fig1, fig2):
|
63 |
if fig1 and fig2:
|
64 |
display(fig1)
|
65 |
display(fig2)
|
|
|
66 |
|
|
|
67 |
@app.cell
|
68 |
def _(movement):
|
69 |
if movement is not None:
|
|
|
3 |
import io
|
4 |
import matplotlib.pyplot as plt
|
5 |
from script import getSearchResult, getClustersWithGraph, compare_clusters
|
6 |
+
import os
|
7 |
|
8 |
app = marimo.App()
|
9 |
|
10 |
+
# 1) Upload + config
|
11 |
@app.cell
|
12 |
def _(mo):
|
13 |
csv_upload = mo.ui.file(label="Upload Keyword CSV (1 column)")
|
14 |
+
# pull secrets from env
|
15 |
api_key = os.environ.get("GOOGLE_API_KEY", "")
|
16 |
+
cse_id = os.environ.get("GOOGLE_CSE_ID", "")
|
17 |
country = mo.ui.text(label="Country Code (e.g. UK)", value="UK")
|
18 |
+
language= mo.ui.text(label="Language Code (e.g. EN)", value="EN")
|
19 |
+
database= mo.ui.text(label="SQLite DB Name", value="data.db")
|
20 |
+
serp_table = mo.ui.text(label="SERP Table", value="keywords_serps")
|
21 |
+
cluster_table= mo.ui.text(label="Cluster Table", value="keyword_clusters")
|
22 |
+
run_button = mo.ui.button(label="Run Clustering Comparison")
|
23 |
return csv_upload, api_key, cse_id, country, language, database, serp_table, cluster_table, run_button
|
24 |
|
25 |
+
# 2) Timestamp selectors
|
26 |
@app.cell
|
27 |
def _(mo):
|
28 |
timestamp_options = [
|
|
|
34 |
("March 2025 core update (2025-04-03 00:00:00.000000)", "2025-04-03 00:00:00.000000"),
|
35 |
("June 2025 core update (2025-07-24 00:00:00.000000)", "2025-07-24 00:00:00.000000"),
|
36 |
]
|
|
|
37 |
timestamp_1 = mo.ui.dropdown(timestamp_options, label="Choose Timestamp 1")
|
38 |
timestamp_2 = mo.ui.dropdown(timestamp_options, label="Choose Timestamp 2")
|
39 |
return timestamp_1, timestamp_2
|
40 |
|
41 |
+
# 3) Read CSV
|
42 |
@app.cell
|
43 |
def _(csv_upload):
|
44 |
if csv_upload.value:
|
|
|
48 |
df_keywords, keywords = None, []
|
49 |
return df_keywords, keywords
|
50 |
|
51 |
+
# 4) Run search + clustering + comparison
|
52 |
@app.cell
|
53 |
def _(keywords, api_key, cse_id, country, language, database, serp_table, timestamp_1, timestamp_2, run_button):
|
54 |
if run_button.clicked and keywords:
|
55 |
+
# 1st timestamp data fetch + cluster
|
56 |
getSearchResult(keywords, language, country, api_key, cse_id, database, serp_table)
|
|
|
57 |
fig1, clusters1 = getClustersWithGraph(database, serp_table, timestamp_1.value)
|
58 |
+
|
59 |
+
# 2nd timestamp data fetch + cluster
|
60 |
+
getSearchResult(keywords, language, country, api_key, cse_id, database, serp_table)
|
61 |
fig2, clusters2 = getClustersWithGraph(database, serp_table, timestamp_2.value)
|
62 |
|
63 |
movement = compare_clusters(clusters1, clusters2)
|
64 |
return fig1, fig2, movement
|
65 |
+
|
66 |
+
# Explicit fallback
|
67 |
return None, None, None
|
68 |
|
69 |
+
# 5) Display the two network graphs
|
70 |
@app.cell
|
71 |
def _(fig1, fig2):
|
72 |
if fig1 and fig2:
|
73 |
display(fig1)
|
74 |
display(fig2)
|
75 |
+
# no return needed here
|
76 |
|
77 |
+
# 6) Show the movement table
|
78 |
@app.cell
|
79 |
def _(movement):
|
80 |
if movement is not None:
|