From 27546d04a0c7a39b1bfc3387e842bf7e2c0d2883 Mon Sep 17 00:00:00 2001 From: iacore Date: Sun, 14 Jun 2026 12:25:55 +0800 Subject: [PATCH] terms-search: add a /sources.html --- terms-search/.codegraph/.gitignore | 16 ++++++++ terms-search/.dockerignore | 2 +- terms-search/Dockerfile | 2 +- terms-search/server.py | 60 ++++++++++------------------- terms-search/templates/index.html | 30 +++++++++++++++ terms-search/templates/sources.html | 19 +++++++++ 6 files changed, 88 insertions(+), 41 deletions(-) create mode 100644 terms-search/.codegraph/.gitignore create mode 100644 terms-search/templates/index.html create mode 100644 terms-search/templates/sources.html diff --git a/terms-search/.codegraph/.gitignore b/terms-search/.codegraph/.gitignore new file mode 100644 index 0000000..9de0f16 --- /dev/null +++ b/terms-search/.codegraph/.gitignore @@ -0,0 +1,16 @@ +# CodeGraph data files +# These are local to each machine and should not be committed + +# Database +*.db +*.db-wal +*.db-shm + +# Cache +cache/ + +# Logs +*.log + +# Hook markers +.dirty diff --git a/terms-search/.dockerignore b/terms-search/.dockerignore index a8f42b4..44e4a9c 100644 --- a/terms-search/.dockerignore +++ b/terms-search/.dockerignore @@ -1,4 +1,4 @@ -# flyctl launch added from .gitignore +.git **/__pycache__ **/__pypackages__ fly.toml diff --git a/terms-search/Dockerfile b/terms-search/Dockerfile index 81965fe..0b2b938 100644 --- a/terms-search/Dockerfile +++ b/terms-search/Dockerfile @@ -4,7 +4,7 @@ WORKDIR /app RUN pip install --no-cache-dir flask duckdb gunicorn -COPY server.py search.py termlib.duckdb ./ +COPY . . EXPOSE 8910 diff --git a/terms-search/server.py b/terms-search/server.py index 884748c..bf8b714 100644 --- a/terms-search/server.py +++ b/terms-search/server.py @@ -3,47 +3,23 @@ import sys, os sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import duckdb -from flask import Flask, request, jsonify, render_template_string +from flask import Flask, request, jsonify, render_template import search as s app = Flask(__name__) -HTML = r''' - -静心学堂中英文翻译对照表 -

静心学堂(非官方)中英文翻译对照表

-
- - - - - -
- -{% if searched %} -{% if error %}

Error: {{ error }}

-{% else %} -

{{ count }} results

- - - {% for r in results %} - - - - - - - {% endfor %} -
ZHENLOCSRC
{{ r.zh }}{{ r.en }}{{ r.loc or '' }}{{ r.source }}
-{% endif %} -{% endif %} -''' - def do_search(q, loc, src, limit): with duckdb.connect(s.DB, read_only=True) as con: rows = s.search(con, q, loc, src, limit) return [{'zh': r[0], 'en': r[1], 'loc': r[2] or None, 'source': r[3]} for r in rows] +def do_sources(): + with duckdb.connect(s.DB, read_only=True) as con: + rows = con.execute( + 'SELECT source, COUNT(*) AS cnt FROM unified_terms_flat GROUP BY source ORDER BY cnt DESC' + ).fetchall() + return [{'source': r[0], 'count': r[1]} for r in rows] + @app.route('/search') def search(): try: @@ -59,14 +35,20 @@ def search(): @app.route('/sources') def sources(): try: - with duckdb.connect(s.DB, read_only=True) as con: - rows = con.execute( - 'SELECT source, COUNT(*) AS cnt FROM unified_terms_flat GROUP BY source ORDER BY cnt DESC' - ).fetchall() - return jsonify([{'source': r[0], 'count': r[1]} for r in rows]) + sources = do_sources() + return jsonify(sources) except Exception as e: return jsonify({'error': str(e)}), 500 +@app.route('/sources.html') +def sources_html(): + try: + sources = do_sources() + total = sum(s['count'] for s in sources) + return render_template('sources.html', sources=sources, total=total) + except Exception as e: + return render_template('sources.html', sources=[], total=0, error=str(e)) + @app.route('/') def ui(): q = request.args.get('q', '') @@ -84,8 +66,8 @@ def ui(): count = len(results) except Exception as e: error = str(e) - return render_template_string(HTML, q=q, loc=loc, src=src, limit=limit, - searched=searched, results=results, count=count, error=error) + return render_template('index.html', q=q, loc=loc, src=src, limit=limit, + searched=searched, results=results, count=count, error=error) if __name__ == '__main__': app.run(port=8910) diff --git a/terms-search/templates/index.html b/terms-search/templates/index.html new file mode 100644 index 0000000..4fa0237 --- /dev/null +++ b/terms-search/templates/index.html @@ -0,0 +1,30 @@ + + +静心学堂中英文翻译对照表 +

静心学堂(非官方)中英文翻译对照表

+

search | sources

+
+ + + + + +
+ +{% if searched %} +{% if error %}

Error: {{ error }}

+{% else %} +

{{ count }} results

+ + + {% for r in results %} + + + + + + + {% endfor %} +
ZHENLOCSRC
{{ r.zh }}{{ r.en }}{{ r.loc or '' }}{{ r.source }}
+{% endif %} +{% endif %} diff --git a/terms-search/templates/sources.html b/terms-search/templates/sources.html new file mode 100644 index 0000000..270d8e3 --- /dev/null +++ b/terms-search/templates/sources.html @@ -0,0 +1,19 @@ + + +Sources — 静心学堂翻译对照表 +

Sources

+

search | sources

+ +{% if error %}

Error: {{ error }}

+{% else %} + + + {% for s in sources %} + + + + + {% endfor %} + +
SourceRows
{{ s.source }}{{ s.count }}
Total{{ total }}
+{% endif %}