Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,6 +23,8 @@ CSV_DIR = Path("csv")
|
|
| 23 |
UPLOAD_DIR.mkdir(exist_ok=True)
|
| 24 |
CSV_DIR.mkdir(exist_ok=True)
|
| 25 |
|
|
|
|
|
|
|
| 26 |
# ------------------------------------------------------------------ Flask app
|
| 27 |
app = Flask(__name__)
|
| 28 |
app.secret_key = uuid.uuid4().hex # random on every restart
|
|
@@ -112,10 +114,10 @@ def download(uid):
|
|
| 112 |
|
| 113 |
@app.route("/show/<uid>")
|
| 114 |
def show_sentences(uid):
|
| 115 |
-
"""Display affected sentences with red highlights."""
|
| 116 |
-
sentences
|
|
|
|
| 117 |
for raw in (CSV_DIR / f"{uid}_sentences.txt").read_text(encoding="utf-8").splitlines():
|
| 118 |
-
# recover line_nr + sentence
|
| 119 |
line_nr, sentence = raw[:6].strip(), raw[8:]
|
| 120 |
bad = {ch for ch in sentence if not _is_ok(ch) if ch.isalpha()}
|
| 121 |
highlighted = re.sub(
|
|
@@ -125,15 +127,39 @@ def show_sentences(uid):
|
|
| 125 |
else m.group(0)),
|
| 126 |
sentence,
|
| 127 |
)
|
| 128 |
-
|
| 129 |
"line_nr": line_nr,
|
| 130 |
"highlights": highlighted,
|
| 131 |
"sentence": sentence,
|
| 132 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
return render_template_string(
|
| 134 |
TEMPLATE_SHOW,
|
| 135 |
sentences=sentences,
|
| 136 |
back_url=url_for("index"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
)
|
| 138 |
|
| 139 |
# ------------------------------------------------------------------ templates
|
|
@@ -158,7 +184,7 @@ TEMPLATE_INDEX = """
|
|
| 158 |
</form>
|
| 159 |
|
| 160 |
{% with msgs = get_flashed_messages() %}
|
| 161 |
-
{% if msgs %}<div class="alert alert-warning">{{ msgs[0] }}</div>{% endif %}
|
| 162 |
{% endwith %}
|
| 163 |
</div>
|
| 164 |
</body>
|
|
@@ -219,7 +245,9 @@ TEMPLATE_SHOW = """
|
|
| 219 |
<h1 class="mb-3">Tatoeba Kabyle Corpus Standardisation Checker</h1>
|
| 220 |
<a class="btn btn-outline-secondary mb-3" href="{{ back_url }}">← Back</a>
|
| 221 |
|
| 222 |
-
<
|
|
|
|
|
|
|
| 223 |
{% for s in sentences %}
|
| 224 |
<li class="mb-2">
|
| 225 |
<pre style="display:inline; margin:0; padding:0; border:none; background:none;">{{ s.highlights|safe }}</pre>
|
|
@@ -230,6 +258,21 @@ TEMPLATE_SHOW = """
|
|
| 230 |
</li>
|
| 231 |
{% endfor %}
|
| 232 |
</ol>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
</div>
|
| 234 |
</body>
|
| 235 |
</html>
|
|
|
|
| 23 |
UPLOAD_DIR.mkdir(exist_ok=True)
|
| 24 |
CSV_DIR.mkdir(exist_ok=True)
|
| 25 |
|
| 26 |
+
PER_PAGE = 50 # sentences per page for pagination
|
| 27 |
+
|
| 28 |
# ------------------------------------------------------------------ Flask app
|
| 29 |
app = Flask(__name__)
|
| 30 |
app.secret_key = uuid.uuid4().hex # random on every restart
|
|
|
|
| 114 |
|
| 115 |
@app.route("/show/<uid>")
|
| 116 |
def show_sentences(uid):
|
| 117 |
+
"""Display affected sentences with red highlights (paginated)."""
|
| 118 |
+
# recover all sentences from disk
|
| 119 |
+
all_sentences = []
|
| 120 |
for raw in (CSV_DIR / f"{uid}_sentences.txt").read_text(encoding="utf-8").splitlines():
|
|
|
|
| 121 |
line_nr, sentence = raw[:6].strip(), raw[8:]
|
| 122 |
bad = {ch for ch in sentence if not _is_ok(ch) if ch.isalpha()}
|
| 123 |
highlighted = re.sub(
|
|
|
|
| 127 |
else m.group(0)),
|
| 128 |
sentence,
|
| 129 |
)
|
| 130 |
+
all_sentences.append({
|
| 131 |
"line_nr": line_nr,
|
| 132 |
"highlights": highlighted,
|
| 133 |
"sentence": sentence,
|
| 134 |
})
|
| 135 |
+
|
| 136 |
+
# ----- pagination logic -----
|
| 137 |
+
page = request.args.get('page', 1, type=int)
|
| 138 |
+
if page < 1:
|
| 139 |
+
page = 1
|
| 140 |
+
|
| 141 |
+
total = len(all_sentences)
|
| 142 |
+
total_pages = (total + PER_PAGE - 1) // PER_PAGE if total else 1
|
| 143 |
+
if page > total_pages:
|
| 144 |
+
page = total_pages
|
| 145 |
+
|
| 146 |
+
start = (page - 1) * PER_PAGE
|
| 147 |
+
end = start + PER_PAGE
|
| 148 |
+
sentences = all_sentences[start:end]
|
| 149 |
+
|
| 150 |
+
prev_url = url_for('show_sentences', uid=uid, page=page-1) if page > 1 else None
|
| 151 |
+
next_url = url_for('show_sentences', uid=uid, page=page+1) if page < total_pages else None
|
| 152 |
+
# ---------------------------
|
| 153 |
+
|
| 154 |
return render_template_string(
|
| 155 |
TEMPLATE_SHOW,
|
| 156 |
sentences=sentences,
|
| 157 |
back_url=url_for("index"),
|
| 158 |
+
page=page,
|
| 159 |
+
total_pages=total_pages,
|
| 160 |
+
prev_url=prev_url,
|
| 161 |
+
next_url=next_url,
|
| 162 |
+
total=total,
|
| 163 |
)
|
| 164 |
|
| 165 |
# ------------------------------------------------------------------ templates
|
|
|
|
| 184 |
</form>
|
| 185 |
|
| 186 |
{% with msgs = get_flashed_messages() %}
|
| 187 |
+
{% if msgs %}<<div class="alert alert-warning">{{ msgs[0] }}</div>{% endif %}
|
| 188 |
{% endwith %}
|
| 189 |
</div>
|
| 190 |
</body>
|
|
|
|
| 245 |
<h1 class="mb-3">Tatoeba Kabyle Corpus Standardisation Checker</h1>
|
| 246 |
<a class="btn btn-outline-secondary mb-3" href="{{ back_url }}">← Back</a>
|
| 247 |
|
| 248 |
+
<p class="text-muted">Showing {{ sentences|length }} of {{ total }} affected sentences — Page {{ page }} of {{ total_pages }}</p>
|
| 249 |
+
|
| 250 |
+
<ol start="{{ (page - 1) * 50 + 1 }}">
|
| 251 |
{% for s in sentences %}
|
| 252 |
<li class="mb-2">
|
| 253 |
<pre style="display:inline; margin:0; padding:0; border:none; background:none;">{{ s.highlights|safe }}</pre>
|
|
|
|
| 258 |
</li>
|
| 259 |
{% endfor %}
|
| 260 |
</ol>
|
| 261 |
+
|
| 262 |
+
<!-- Pagination -->
|
| 263 |
+
<nav aria-label="Sentence pages" class="mt-4">
|
| 264 |
+
<ul class="pagination justify-content-center">
|
| 265 |
+
<li class="page-item {% if not prev_url %}disabled{% endif %}">
|
| 266 |
+
<a class="page-link" href="{{ prev_url or '#' }}">← Previous</a>
|
| 267 |
+
</li>
|
| 268 |
+
<li class="page-item disabled">
|
| 269 |
+
<span class="page-link">Page {{ page }} of {{ total_pages }}</span>
|
| 270 |
+
</li>
|
| 271 |
+
<li class="page-item {% if not next_url %}disabled{% endif %}">
|
| 272 |
+
<a class="page-link" href="{{ next_url or '#' }}">Next →</a>
|
| 273 |
+
</li>
|
| 274 |
+
</ul>
|
| 275 |
+
</nav>
|
| 276 |
</div>
|
| 277 |
</body>
|
| 278 |
</html>
|