EnglishPal/app/wordCMD.py

42 lines
1.1 KiB
Python
Raw Normal View History

2022-06-18 16:17:08 +08:00
from flask import Flask, request, Blueprint, render_template
from UseSqlite import InsertQuery, RecordQuery
2022-06-18 22:16:18 +08:00
TKTK = 'token' # set token
2022-06-18 16:17:08 +08:00
show_bp = Blueprint(
'site',
__name__,
)
2022-06-18 22:16:18 +08:00
def make_html_paragraph(s): # build the word's table
2022-06-18 16:17:08 +08:00
if s.strip() == '':
return ''
lst = s.split(',')
word = lst[1].strip()
result = '<tr><td>' + word + '</td></tr>'
return result
2022-06-18 22:16:18 +08:00
@show_bp.route('/show/<name>/') # set route for show page <name> means the var name to search
2022-06-18 16:17:08 +08:00
def show(name):
token = request.args.get("token")
2022-06-18 22:16:18 +08:00
# when token is wrong
2022-06-18 16:17:08 +08:00
if token != TKTK:
return "token is wrong, please try again"
rq = RecordQuery('./static/wordfreqapp.db')
2022-06-18 22:16:18 +08:00
# search the user's words in db
2022-06-18 16:17:08 +08:00
rq.instructions("SELECT * FROM words where user = \'" + name + "\'")
rq.do()
2022-06-18 22:16:18 +08:00
# show the results
record = '<h1>' + f"Here are {name}'s words:" + '</h1>'
2022-06-18 16:17:08 +08:00
record += '<table border= \"2\" >'
record += '<tr>'
record += '<th>WORDS</th>'
record += '</tr>'
for r in rq.format_results().split('\n\n'):
record += '%s' % (make_html_paragraph(r))
record += '</table>'
2022-06-18 22:16:18 +08:00
return record + '\n'