Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (93 loc) · 3.22 KB
/
Copy pathmain.py
File metadata and controls
103 lines (93 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from flask import Flask, request, render_template_string, session, redirect, url_for
import pandas as pd
import os
import uuid
app = Flask(__name__)
app.secret_key = "supersecretkey"
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>CSV Row Viewer</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.row-box { border: 1px solid #ccc; padding: 20px; margin: 20px 0; border-radius: 8px; }
.nav-buttons { margin-top: 20px; }
button { padding: 10px 20px; margin-right: 10px; }
</style>
</head>
<body>
<h1>Upload CSV</h1>
{% if not session.get("file_id") %}
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file" accept=".csv" required>
<button type="submit">Upload</button>
</form>
{% else %}
<h2>Row {{ index + 1 }} of {{ total }}</h2>
<div class="row-box">
{% for col, val in row.items() %}
<p><b>{{ col }}</b>: {{ val }}</p>
{% endfor %}
</div>
<div class="nav-buttons">
{% if index > 0 %}
<a href="{{ url_for('navigate', direction='prev') }}"><button>Previous</button></a>
{% endif %}
{% if index < total - 1 %}
<a href="{{ url_for('navigate', direction='next') }}"><button>Next</button></a>
{% endif %}
</div>
{% endif %}
</body>
</html>
"""
def load_dataframe():
"""Helper: load dataframe from uploaded file"""
file_id = session.get("file_id")
if not file_id:
return None
file_path = os.path.join(UPLOAD_FOLDER, f"{file_id}.csv")
df = pd.read_csv(file_path)
# Filter rows containing "path planning" in any column
filtered_df = df[df.astype(str).apply(lambda x: x.str.contains('path planning', case=False)).any(axis=1)]
return filtered_df if not filtered_df.empty else df
@app.route("/", methods=["GET", "POST"])
def upload_csv():
if request.method == "POST":
file = request.files["file"]
if file and file.filename.endswith(".csv"):
# Save file with a unique ID
file_id = str(uuid.uuid4())
file_path = os.path.join(UPLOAD_FOLDER, f"{file_id}.csv")
file.save(file_path)
# Reset session
session["file_id"] = file_id
session["index"] = 0
return redirect(url_for("upload_csv"))
if "file_id" in session:
df = load_dataframe()
index = session.get("index", 0)
row = df.iloc[index].to_dict()
return render_template_string(
TEMPLATE,
row=row,
index=index,
total=len(df),
session=session
)
return render_template_string(TEMPLATE)
@app.route("/navigate/<direction>")
def navigate(direction):
if "file_id" in session:
df = load_dataframe()
index = session.get("index", 0)
if direction == "next" and index < len(df) - 1:
session["index"] = index + 1
elif direction == "prev" and index > 0:
session["index"] = index - 1
return redirect(url_for("upload_csv"))
if __name__ == "__main__":
app.run(debug=True)