Em4e commited on
Commit
287f82a
Β·
verified Β·
1 Parent(s): fd1e289

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +684 -0
app.py ADDED
@@ -0,0 +1,684 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import grape
5
+ import matplotlib.pyplot as plt
6
+ import seaborn as sns
7
+ import plotly.express as px
8
+ import plotly.graph_objects as go
9
+ from plotly.subplots import make_subplots
10
+ import time
11
+ import gc
12
+ from io import StringIO
13
+ import random
14
+ from collections import defaultdict
15
+
16
+ # Set page config
17
+ st.set_page_config(
18
+ page_title="Website Link Impact Analyzer",
19
+ page_icon="πŸ”—",
20
+ layout="wide",
21
+ initial_sidebar_state="expanded"
22
+ )
23
+
24
+ # Global cache for WWW graph
25
+ if 'www_graph_cache' not in st.session_state:
26
+ st.session_state.www_graph_cache = None
27
+
28
+ def load_graph_from_csv_grape(file_content, file_name):
29
+ """
30
+ Load page links from CSV file using Grape.
31
+ """
32
+ try:
33
+ # Read CSV content
34
+ df = pd.read_csv(StringIO(file_content))
35
+
36
+ # Check required columns with user-friendly names
37
+ required_cols = ['FROM', 'TO']
38
+ if not all(col in df.columns for col in required_cols):
39
+ st.error(f"""
40
+ ❌ **File Format Error**
41
+
42
+ Your CSV file needs these column names:
43
+ - **FROM** (the page that has the link)
44
+ - **TO** (the page being linked to)
45
+
46
+ Your file has: {', '.join(df.columns)}
47
+ """)
48
+ return None, None, None
49
+
50
+ # Clean data
51
+ df = df.dropna(subset=['FROM', 'TO'])
52
+ df['FROM'] = df['FROM'].astype(str)
53
+ df['TO'] = df['TO'].astype(str)
54
+
55
+ if len(df) == 0:
56
+ st.error(f"❌ No valid page links found in {file_name}")
57
+ return None, None, None
58
+
59
+ # Get unique nodes and create mapping
60
+ all_nodes = list(set(df['FROM'].tolist() + df['TO'].tolist()))
61
+ node_to_idx = {node: i for i, node in enumerate(all_nodes)}
62
+
63
+ # Create edge list with indices
64
+ edge_list = []
65
+ for _, row in df.iterrows():
66
+ source_idx = node_to_idx[row['FROM']]
67
+ target_idx = node_to_idx[row['TO']]
68
+ edge_list.append((source_idx, target_idx))
69
+
70
+ # Create Grape graph
71
+ G = grape.Graph.from_edge_list(
72
+ edge_list=edge_list,
73
+ directed=True,
74
+ node_names=[str(i) for i in range(len(all_nodes))],
75
+ name=f"graph_{file_name}"
76
+ )
77
+
78
+ return G, all_nodes, node_to_idx
79
+
80
+ except Exception as e:
81
+ st.error(f"❌ **Error reading file**: {str(e)}")
82
+ st.info("πŸ’‘ **Tip**: Make sure your file is a valid CSV with FROM and TO columns for page links")
83
+ return None, None, None
84
+
85
+ def create_www_graph_grape(n_nodes, m_edges, seed=42):
86
+ """
87
+ Create a realistic internet simulation using Grape.
88
+ """
89
+ cache_key = (n_nodes, m_edges, seed)
90
+
91
+ if (st.session_state.www_graph_cache is not None and
92
+ st.session_state.www_graph_cache[0] == cache_key):
93
+ return st.session_state.www_graph_cache[1]
94
+
95
+ # Set random seed
96
+ random.seed(seed)
97
+ np.random.seed(seed)
98
+
99
+ # Create BarabΓ‘si-Albert graph manually since Grape doesn't have this built-in
100
+ # Start with a complete graph of m_edges nodes
101
+ edges = []
102
+ for i in range(m_edges):
103
+ for j in range(i + 1, m_edges):
104
+ edges.append((i, j))
105
+ edges.append((j, i)) # Make it directed
106
+
107
+ # Add remaining nodes with preferential attachment
108
+ degrees = [2 * m_edges] * m_edges # Initial degrees
109
+
110
+ for new_node in range(m_edges, n_nodes):
111
+ # Select m_edges nodes to connect to based on preferential attachment
112
+ total_degree = sum(degrees)
113
+ targets = set()
114
+
115
+ while len(targets) < min(m_edges, new_node):
116
+ # Probability proportional to degree
117
+ rand_val = random.random() * total_degree
118
+ cumsum = 0
119
+ for i, degree in enumerate(degrees):
120
+ cumsum += degree
121
+ if cumsum >= rand_val and i not in targets:
122
+ targets.add(i)
123
+ break
124
+
125
+ # Add edges
126
+ for target in targets:
127
+ edges.append((new_node, target))
128
+ edges.append((target, new_node)) # Bidirectional
129
+
130
+ # Update degrees
131
+ degrees.append(2 * len(targets))
132
+ for target in targets:
133
+ degrees[target] += 2
134
+
135
+ # Create Grape graph
136
+ www_graph = grape.Graph.from_edge_list(
137
+ edge_list=edges,
138
+ directed=True,
139
+ node_names=[str(i) for i in range(n_nodes)],
140
+ name="www_simulation"
141
+ )
142
+
143
+ # Cache the result
144
+ st.session_state.www_graph_cache = (cache_key, www_graph)
145
+ return www_graph
146
+
147
+ def process_configuration_grape(www_graph, kalicube_graph, kalicube_nodes,
148
+ min_connections=5, max_connections=50):
149
+ """
150
+ Test how your page network performs in the real internet using Grape.
151
+ """
152
+ # Get WWW graph info
153
+ www_node_count = www_graph.get_number_of_nodes()
154
+ kalicube_node_count = len(kalicube_nodes)
155
+
156
+ # Create node mapping for kalicube nodes
157
+ kalicube_offset = www_node_count
158
+ kalicube_node_mapping = {}
159
+
160
+ for i, node in enumerate(kalicube_nodes):
161
+ new_node_id = kalicube_offset + i
162
+ kalicube_node_mapping[node] = new_node_id
163
+
164
+ # Get edges from both graphs
165
+ www_edges = www_graph.get_edge_list()
166
+ kalicube_edges = kalicube_graph.get_edge_list()
167
+
168
+ # Convert kalicube edges to use new node IDs
169
+ kalicube_mapped_edges = []
170
+ kalicube_idx_to_node = {i: node for node, i in kalicube_graph.get_node_name_to_node_id_map().items()}
171
+
172
+ for source_idx, target_idx in kalicube_edges:
173
+ source_node = kalicube_idx_to_node[source_idx]
174
+ target_node = kalicube_idx_to_node[target_idx]
175
+ new_source_id = kalicube_node_mapping[source_node]
176
+ new_target_id = kalicube_node_mapping[target_node]
177
+ kalicube_mapped_edges.append((new_source_id, new_target_id))
178
+
179
+ # Randomly connect kalicube pages to WWW
180
+ n_connections = min(min_connections, www_node_count, kalicube_node_count)
181
+
182
+ www_sample = random.sample(range(www_node_count), n_connections)
183
+ kalicube_sample = random.sample(list(kalicube_node_mapping.values()), n_connections)
184
+
185
+ connection_edges = []
186
+ for www_node, kalicube_node in zip(www_sample, kalicube_sample):
187
+ connection_edges.append((www_node, kalicube_node))
188
+
189
+ # Combine all edges
190
+ all_edges = list(www_edges) + kalicube_mapped_edges + connection_edges
191
+ total_nodes = www_node_count + kalicube_node_count
192
+
193
+ # Create merged graph
194
+ merged_graph = grape.Graph.from_edge_list(
195
+ edge_list=all_edges,
196
+ directed=True,
197
+ node_names=[str(i) for i in range(total_nodes)],
198
+ name="merged_simulation"
199
+ )
200
+
201
+ # Calculate PageRank
202
+ try:
203
+ pagerank_values = merged_graph.pagerank(
204
+ damping_factor=0.85,
205
+ maximum_iterations=100,
206
+ tolerance=1e-6
207
+ )
208
+ except Exception as e:
209
+ st.warning(f"PageRank calculation failed: {e}. Using degree centrality instead.")
210
+ # Fallback to degree centrality
211
+ degrees = merged_graph.get_node_degrees()
212
+ total_degree = sum(degrees)
213
+ pagerank_values = [deg / total_degree if total_degree > 0 else 0 for deg in degrees]
214
+
215
+ # Extract PageRank values for kalicube nodes
216
+ pagerank_dict = {}
217
+ for node, node_id in kalicube_node_mapping.items():
218
+ pagerank_dict[node] = pagerank_values[node_id] if node_id < len(pagerank_values) else 0.0
219
+
220
+ return pagerank_dict
221
+
222
+ def create_comparison_dataframe(pagerank_old_dict, pagerank_new_dict, simulation_id):
223
+ """
224
+ Compare before and after results.
225
+ """
226
+ # Find pages that appear in both tests
227
+ old_urls = set(pagerank_old_dict.keys())
228
+ new_urls = set(pagerank_new_dict.keys())
229
+ common_urls = old_urls & new_urls
230
+
231
+ if not common_urls:
232
+ return pd.DataFrame()
233
+
234
+ # Create comparison data
235
+ comparison_data = []
236
+
237
+ # Sort pages by importance for ranking
238
+ old_sorted = sorted(pagerank_old_dict.items(), key=lambda x: x[1], reverse=True)
239
+ new_sorted = sorted(pagerank_new_dict.items(), key=lambda x: x[1], reverse=True)
240
+
241
+ # Create ranking mappings
242
+ old_ranks = {url: rank + 1 for rank, (url, _) in enumerate(old_sorted)}
243
+ new_ranks = {url: rank + 1 for rank, (url, _) in enumerate(new_sorted)}
244
+
245
+ for url in common_urls:
246
+ importance_before = pagerank_old_dict[url]
247
+ importance_after = pagerank_new_dict[url]
248
+ rank_before = old_ranks[url]
249
+ rank_after = new_ranks[url]
250
+
251
+ importance_change = importance_after - importance_before
252
+ importance_change_pct = (importance_change / importance_before) * 100 if importance_before > 0 else 0
253
+ rank_change = rank_after - rank_before
254
+ rank_change_pct = (rank_change / rank_before) * 100 if rank_before > 0 else 0
255
+
256
+ comparison_data.append({
257
+ 'Page_URL': url,
258
+ 'Importance_Before': importance_before,
259
+ 'Importance_After': importance_after,
260
+ 'Rank_Before': rank_before,
261
+ 'Rank_After': rank_after,
262
+ 'Importance_Change': importance_change,
263
+ 'Importance_Change_%': importance_change_pct,
264
+ 'Rank_Change': rank_change,
265
+ 'Rank_Change_%': rank_change_pct,
266
+ 'Test_Number': simulation_id
267
+ })
268
+
269
+ return pd.DataFrame(comparison_data)
270
+
271
+ def run_single_simulation(simulation_id, kalicube_graph_old, kalicube_graph_new,
272
+ kalicube_nodes_old, kalicube_nodes_new,
273
+ www_nodes, www_edges, min_conn, max_conn):
274
+ """
275
+ Run one test comparing before and after.
276
+ """
277
+ sim_seed = 42 + simulation_id
278
+ random.seed(sim_seed)
279
+ np.random.seed(sim_seed)
280
+
281
+ # Create internet simulation
282
+ www_graph = create_www_graph_grape(www_nodes, www_edges, sim_seed)
283
+
284
+ # Test original setup
285
+ importance_old_dict = process_configuration_grape(
286
+ www_graph, kalicube_graph_old, kalicube_nodes_old, min_conn, max_conn
287
+ )
288
+
289
+ # Test new setup
290
+ importance_new_dict = process_configuration_grape(
291
+ www_graph, kalicube_graph_new, kalicube_nodes_new, min_conn, max_conn
292
+ )
293
+
294
+ # Compare results
295
+ comparison_df = create_comparison_dataframe(
296
+ importance_old_dict, importance_new_dict, simulation_id
297
+ )
298
+
299
+ if comparison_df.empty:
300
+ return None, None
301
+
302
+ # Calculate summary
303
+ total_before = comparison_df['Importance_Before'].sum()
304
+ total_after = comparison_df['Importance_After'].sum()
305
+ total_change = total_after - total_before
306
+ change_pct = (total_change / total_before) * 100 if total_before > 0 else 0
307
+
308
+ rank_changes = comparison_df['Rank_Change'].values
309
+ rank_improvements = np.sum(rank_changes < 0) # Lower rank number = better
310
+ rank_drops = np.sum(rank_changes > 0)
311
+ rank_unchanged = np.sum(rank_changes == 0)
312
+ avg_rank_change = np.mean(rank_changes)
313
+
314
+ result = {
315
+ 'Test_Number': simulation_id + 1,
316
+ 'Total_Before': total_before,
317
+ 'Total_After': total_after,
318
+ 'Total_Change': total_change,
319
+ 'Change_Percent': change_pct,
320
+ 'Pages_Improved': rank_improvements,
321
+ 'Pages_Dropped': rank_drops,
322
+ 'Pages_Unchanged': rank_unchanged,
323
+ 'Avg_Rank_Change': avg_rank_change
324
+ }
325
+
326
+ return result, comparison_df
327
+
328
+ def get_traffic_light_status(results_df, confidence_threshold=0.7):
329
+ """
330
+ Simple decision guidance based on test results.
331
+ """
332
+ total_tests = len(results_df)
333
+ positive_outcomes = (results_df['Total_Change'] > 0).sum()
334
+ negative_outcomes = (results_df['Total_Change'] < 0).sum()
335
+
336
+ positive_ratio = positive_outcomes / total_tests
337
+ negative_ratio = negative_outcomes / total_tests
338
+ mean_impact = results_df['Change_Percent'].mean()
339
+
340
+ # Simple traffic light logic
341
+ if positive_ratio >= confidence_threshold and mean_impact > 1.0:
342
+ return "🟒", "βœ… GO AHEAD - Your changes look great!", "go", "Most tests show good results. Your changes should help your page rankings."
343
+ elif positive_ratio >= confidence_threshold and mean_impact > 0:
344
+ return "🟑", "⚠️ PROCEED CAREFULLY - Small improvements expected", "caution", "Tests show some improvement, but it's modest. Consider if the effort is worth it."
345
+ elif negative_ratio >= confidence_threshold and mean_impact < -1.0:
346
+ return "πŸ”΄", "❌ STOP - Your changes may hurt your page rankings", "stop", "Most tests show negative results. Consider revising your changes before implementing."
347
+ elif negative_ratio >= confidence_threshold and mean_impact < 0:
348
+ return "🟑", "⚠️ PROCEED CAREFULLY - Some negative impact expected", "caution", "Tests show some negative impact. Monitor closely if you proceed."
349
+ else:
350
+ return "🟑", "🀷 MIXED RESULTS - Hard to predict", "caution", "Test results are mixed. Consider running more tests or getting expert advice."
351
+
352
+ def create_simple_visualizations(results_df, all_comparisons_df, confidence_threshold=0.7):
353
+ """
354
+ Create easy-to-understand visualizations.
355
+ """
356
+ # Traffic Light Assessment
357
+ traffic_emoji, traffic_status, traffic_level, explanation = get_traffic_light_status(results_df, confidence_threshold)
358
+
359
+ st.markdown("## 🚦 **Should You Make These Changes?**")
360
+
361
+ # Big, clear recommendation
362
+ if traffic_level == "go":
363
+ st.success(f"# {traffic_emoji}")
364
+ st.success(f"## {traffic_status}")
365
+ st.info(f"**Why:** {explanation}")
366
+ elif traffic_level == "stop":
367
+ st.error(f"# {traffic_emoji}")
368
+ st.error(f"## {traffic_status}")
369
+ st.warning(f"**Why:** {explanation}")
370
+ else:
371
+ st.warning(f"# {traffic_emoji}")
372
+ st.warning(f"## {traffic_status}")
373
+ st.info(f"**Why:** {explanation}")
374
+
375
+ # Simple metrics in plain English
376
+ st.markdown("### πŸ“Š **Test Results Summary**")
377
+ col1, col2, col3 = st.columns(3)
378
+
379
+ with col1:
380
+ positive_tests = (results_df['Total_Change'] > 0).sum()
381
+ total_tests = len(results_df)
382
+ st.metric("Tests Showing Improvement", f"{positive_tests} out of {total_tests}",
383
+ delta=f"{positive_tests/total_tests:.0%} positive")
384
+
385
+ with col2:
386
+ mean_change = results_df['Change_Percent'].mean()
387
+ st.metric("Average Impact on Rankings", f"{mean_change:.1f}%",
388
+ delta="Higher is better")
389
+
390
+ with col3:
391
+ improved_sites = results_df['Pages_Improved'].mean()
392
+ st.metric("Pages That Improved (avg)", f"{improved_sites:.0f}",
393
+ delta="per test")
394
+
395
+ def main():
396
+ st.title("πŸ”— Page Link Impact Analyzer (Powered by Grape)")
397
+ st.markdown("**Find out if your page link changes will help or hurt your search rankings**")
398
+
399
+ # Simple intro
400
+ st.info("""
401
+ πŸ‘‹ **Welcome!** This tool helps you test page link changes before you make them.
402
+
403
+ **What it does:** Simulates how your link changes might affect your page rankings in search engines.
404
+
405
+ **What you need:** Two CSV files - one with your current page links, one with your planned changes.
406
+
407
+ πŸ‡ **Now powered by Grape** - A high-performance graph library for faster and more efficient analysis!
408
+ """)
409
+
410
+ # Sidebar - simplified
411
+ st.sidebar.header("βš™οΈ Settings")
412
+
413
+ # File uploads with better guidance
414
+ st.sidebar.markdown("### πŸ“ **Step 1: Upload Your Files**")
415
+ st.sidebar.markdown("*Need help with file format? Check the 'File Format Help' section below.*")
416
+
417
+ old_file = st.sidebar.file_uploader("Current Page Links (CSV)", type=['csv'], key="old",
418
+ help="Upload a CSV file with your current page links")
419
+ new_file = st.sidebar.file_uploader("Planned Page Links (CSV)", type=['csv'], key="new",
420
+ help="Upload a CSV file with your planned page links")
421
+
422
+ # Simplified settings
423
+ st.sidebar.markdown("### 🎯 **Step 2: Test Settings**")
424
+
425
+ num_tests = st.sidebar.select_slider(
426
+ "How many tests to run?",
427
+ options=[5, 10, 15, 20, 25, 30],
428
+ value=10,
429
+ help="More tests = more reliable results, but takes longer"
430
+ )
431
+
432
+ internet_size = st.sidebar.select_slider(
433
+ "Internet simulation size",
434
+ options=["Small (5K sites)", "Medium (10K sites)", "Large (25K sites)", "Huge (50K sites)"],
435
+ value="Medium (10K sites)",
436
+ help="Larger = more realistic but slower"
437
+ )
438
+
439
+ # Convert internet size to numbers
440
+ size_map = {
441
+ "Small (5K sites)": 5000,
442
+ "Medium (10K sites)": 10000,
443
+ "Large (25K sites)": 25000,
444
+ "Huge (50K sites)": 50000
445
+ }
446
+ www_nodes = size_map[internet_size]
447
+
448
+ # Advanced settings (hidden by default)
449
+ with st.sidebar.expander("πŸ”§ Advanced Settings (Optional)"):
450
+ confidence_level = st.slider("Confidence level for recommendations", 60, 90, 70, 5,
451
+ help="Higher = stricter requirements for green/red lights")
452
+ show_details = st.checkbox("Show detailed results", False)
453
+ auto_run = st.checkbox("Auto-run when files uploaded", False)
454
+
455
+ confidence_threshold = confidence_level / 100
456
+
457
+ # Main content
458
+ if old_file is not None and new_file is not None:
459
+ # Load files
460
+ old_content = old_file.getvalue().decode('utf-8')
461
+ new_content = new_file.getvalue().decode('utf-8')
462
+
463
+ # Show file status
464
+ col1, col2 = st.columns(2)
465
+ with col1:
466
+ st.success(f"βœ… **Current Page Links**: {old_file.name}")
467
+ with col2:
468
+ st.success(f"βœ… **Planned Page Links**: {new_file.name}")
469
+
470
+ # Load and validate files
471
+ with st.spinner("Reading your files..."):
472
+ kalicube_graph_old, kalicube_nodes_old, kalicube_url_mapping_old = \
473
+ load_graph_from_csv_grape(old_content, old_file.name)
474
+
475
+ kalicube_graph_new, kalicube_nodes_new, kalicube_url_mapping_new = \
476
+ load_graph_from_csv_grape(new_content, new_file.name)
477
+
478
+ if kalicube_graph_old is not None and kalicube_graph_new is not None:
479
+ # Show what we found
480
+ st.markdown("### πŸ“ˆ **What We Found in Your Files**")
481
+ info_col1, info_col2 = st.columns(2)
482
+
483
+ with info_col1:
484
+ st.info(f"""
485
+ **Current Setup:**
486
+ - {len(kalicube_nodes_old)} pages
487
+ - {kalicube_graph_old.get_number_of_edges()} links between them
488
+ """)
489
+
490
+ with info_col2:
491
+ st.info(f"""
492
+ **Planned Setup:**
493
+ - {len(kalicube_nodes_new)} pages
494
+ - {kalicube_graph_new.get_number_of_edges()} links between them
495
+ """)
496
+
497
+ # Big, obvious run button
498
+ st.markdown("### πŸš€ **Step 3: Run the Test**")
499
+
500
+ run_button = st.button("πŸ”¬ Test My Changes", type="primary", use_container_width=True)
501
+
502
+ if run_button or auto_run:
503
+ # Progress with encouraging messages
504
+ progress_bar = st.progress(0)
505
+ status_text = st.empty()
506
+
507
+ encouraging_messages = [
508
+ "πŸ”¬ Setting up internet simulation...",
509
+ "🌐 Connecting your pages to the web...",
510
+ "πŸ“Š Calculating page importance scores...",
511
+ "🎯 Running tests with different scenarios...",
512
+ "πŸ“ˆ Almost done! Analyzing results..."
513
+ ]
514
+
515
+ all_results = []
516
+ all_comparisons = []
517
+
518
+ start_time = time.time()
519
+
520
+ # Run tests with encouragement
521
+ for i in range(num_tests):
522
+ msg_idx = min(i // max(1, num_tests // len(encouraging_messages)), len(encouraging_messages) - 1)
523
+ status_text.text(f"{encouraging_messages[msg_idx]} (Test {i+1}/{num_tests})")
524
+ progress_bar.progress((i + 1) / num_tests)
525
+
526
+ result, comparison_df = run_single_simulation(
527
+ i, kalicube_graph_old, kalicube_graph_new,
528
+ kalicube_nodes_old, kalicube_nodes_new,
529
+ www_nodes, 2, 5, 25 # simplified parameters
530
+ )
531
+
532
+ if result is not None:
533
+ all_results.append(result)
534
+ all_comparisons.append(comparison_df)
535
+
536
+ end_time = time.time()
537
+
538
+ # Clear progress
539
+ progress_bar.empty()
540
+ status_text.empty()
541
+
542
+ if all_results:
543
+ results_df = pd.DataFrame(all_results)
544
+ all_comparisons_df = pd.concat(all_comparisons, ignore_index=True) if all_comparisons else pd.DataFrame()
545
+
546
+ # Show results
547
+ st.success(f"πŸŽ‰ **Test Complete!** Ran {len(all_results)} tests in {end_time - start_time:.0f} seconds")
548
+
549
+ # Create simple visualizations
550
+ create_simple_visualizations(results_df, all_comparisons_df, confidence_threshold)
551
+
552
+ # Download section
553
+ st.markdown("### πŸ’Ύ **Save Your Results**")
554
+ col1, col2 = st.columns(2)
555
+
556
+ with col1:
557
+ csv_summary = results_df.to_csv(index=False)
558
+ st.download_button(
559
+ label="πŸ“Š Download Summary Report",
560
+ data=csv_summary,
561
+ file_name=f"website_impact_summary_{int(time.time())}.csv",
562
+ mime="text/csv"
563
+ )
564
+
565
+ with col2:
566
+ if not all_comparisons_df.empty:
567
+ csv_detailed = all_comparisons_df.to_csv(index=False)
568
+ st.download_button(
569
+ label="πŸ“‹ Download Detailed Results",
570
+ data=csv_detailed,
571
+ file_name=f"website_impact_detailed_{int(time.time())}.csv",
572
+ mime="text/csv"
573
+ )
574
+
575
+ # Show detailed results if requested
576
+ if show_details and not all_comparisons_df.empty:
577
+ st.markdown("### πŸ” **Detailed Results** (For the curious)")
578
+
579
+ # Simple filter
580
+ st.markdown("**Filter results:**")
581
+ filter_col1, filter_col2 = st.columns(2)
582
+ with filter_col1:
583
+ min_change = st.number_input("Show changes above (%)",
584
+ value=float(all_comparisons_df['Importance_Change_%'].min()),
585
+ step=0.1)
586
+
587
+ # Apply filter and show
588
+ filtered_df = all_comparisons_df[all_comparisons_df['Importance_Change_%'] >= min_change]
589
+
590
+ # Rename columns for clarity
591
+ display_df = filtered_df.copy()
592
+ display_df = display_df.rename(columns={
593
+ 'Page_URL': 'Page URL',
594
+ 'Importance_Change_%': 'Impact (%)',
595
+ 'Rank_Change': 'Rank Change',
596
+ 'Test_Number': 'Test #'
597
+ })
598
+
599
+ st.dataframe(
600
+ display_df[['Page URL', 'Impact (%)', 'Rank_Change', 'Test #']].sort_values('Impact (%)', ascending=False),
601
+ use_container_width=True,
602
+ height=300
603
+ )
604
+
605
+ else:
606
+ st.error("❌ No test results generated. Please check your files and try again.")
607
+
608
+ else:
609
+ # Help section when no files uploaded
610
+ st.markdown("---")
611
+
612
+ # File format help
613
+ with st.expander("πŸ“‹ **File Format Help** - How to prepare your CSV files"):
614
+ st.markdown("""
615
+ ### βœ… **Correct Format**
616
+ Your CSV files need exactly these column names:
617
+ - **FROM** = the page that has the link
618
+ - **TO** = the page being linked to
619
+
620
+ ### πŸ“ **Example:**
621
+ ```
622
+ FROM,TO
623
+ mysite.com/about,mysite.com/contact
624
+ mysite.com/blog/post1,partner.com/resource
625
+ partner.com/page,mysite.com/services
626
+ ```
627
+
628
+ ### πŸ’‘ **Tips:**
629
+ - Use any spreadsheet program (Excel, Google Sheets) to create these
630
+ - Save as CSV format
631
+ - Include full URLs or page paths
632
+ - Make sure page URLs are consistent (mysite.com/page vs mysite.com/page/ are different!)
633
+ - Each row represents one link from one page to another
634
+ """)
635
+
636
+ with st.expander("πŸ€” **What This Tool Actually Does** - Explained Simply"):
637
+ st.markdown("""
638
+ ### 🌐 **The Big Picture**
639
+ When you change links between your pages, it affects how search engines see your site. But it's hard to predict the exact impact because the internet is huge and constantly changing.
640
+
641
+ ### πŸ§ͺ **Our Solution: Virtual Testing**
642
+ 1. **We simulate the internet** - Create a virtual version with thousands of pages
643
+ 2. **We test your changes** - Run your current page links vs. your planned links
644
+ 3. **We repeat many times** - Each test uses slightly different internet conditions
645
+ 4. **We analyze the pattern** - Look at whether your changes usually help or hurt
646
+
647
+ ### 🚦 **The Traffic Light System**
648
+ - **🟒 Green = Go ahead** - Most tests show your changes help
649
+ - **🟑 Yellow = Be careful** - Mixed results or small impact
650
+ - **πŸ”΄ Red = Stop** - Most tests show your changes hurt
651
+
652
+ ### 🎯 **Why This Works**
653
+ Instead of guessing, you get data-driven confidence about your page link changes!
654
+
655
+ ### πŸ‡ **Powered by Grape**
656
+ This version uses Grape, a high-performance graph library that's much faster than traditional tools for analyzing large networks.
657
+ """)
658
+
659
+ with st.expander("❓ **Common Questions**"):
660
+ st.markdown("""
661
+ **Q: How accurate is this?**
662
+ A: The tool shows trends and probabilities, not exact predictions. It's like weather forecasting - very useful for planning!
663
+
664
+ **Q: How long does it take?**
665
+ A: Usually 30 seconds to 2 minutes, depending on your settings. Grape makes it faster than before!
666
+
667
+ **Q: What if I get yellow results?**
668
+ A: Yellow means proceed carefully. Consider running more tests, getting expert advice, or monitoring closely if you implement.
669
+
670
+ **Q: Can I test multiple scenarios?**
671
+ A: Yes! Just upload different "planned changes" files to compare options.
672
+
673
+ **Q: What file size limits?**
674
+ A: Works best with up to 10,000 page links. Larger files may be slow.
675
+
676
+ **Q: What's the difference between pages and websites?**
677
+ A: Pages are specific URLs (like mysite.com/about), while websites are domains (like mysite.com). This tool analyzes individual page links.
678
+
679
+ **Q: What's new with Grape?**
680
+ A: Grape is a high-performance graph library that makes calculations much faster and can handle larger datasets more efficiently than NetworkX.
681
+ """)
682
+
683
+ if __name__ == "__main__":
684
+ main()