0
0
Fork 0

AJAX载入文章数据

Alpha-snapshot20230507
huangdan 2023-05-08 14:33:48 +08:00
parent becef7e343
commit 5039f5710e
2 changed files with 62 additions and 26 deletions

View File

@ -50,22 +50,22 @@
{# {% endfor %}#} {# {% endfor %}#}
{% if result_of_generate_article != "had read all articles" %} {% if result_of_generate_article != "had read all articles" %}
<a id="next_btn" class="btn btn-success" href="/{{ username }}/reset" role="button"> 下一篇 Next Article </a> <button class="btn btn-success" id="load_next_article" onclick="load_next_article()"> 下一篇 Next Article </button>
{% endif %} {% endif %}
{% if session.get('visited_articles') and session.get('visited_articles')['index']>0 %} {% if session.get('visited_articles') and session.get('visited_articles')['index']>0 %}
<a id="pre_btn" class="btn btn-success" href="/{{ username }}/back" role="button"> 上一篇 Previous Article </a> <button class="btn btn-success" id="load_pre_article" onclick="load_pre_article()"> 上一篇 Previous Article </button>
{% endif %} {% endif %}
<p><b>阅读文章并回答问题</b></p> <p><b>阅读文章并回答问题</b></p>
<div id="text-content"> <div id="text-content">
{% if result_of_generate_article == 'found' %} {% if result_of_generate_article == 'found' %}
<div class="alert alert-success" role="alert">According to your word list, your level is <span class="badge bg-success">{{ today_article["user_level"] }}</span> and we have chosen an article with a difficulty level of <span class="badge bg-success">{{ today_article["text_level"] }}</span> for you.</div> <div class="alert alert-success" role="alert">According to your word list, your level is <span class="badge bg-success" id="user-level">{{ today_article["user_level"] }}</span> and we have chosen an article with a difficulty level of <span class="badge bg-success" id="text_level">{{ today_article["text_level"] }}</span> for you.</div>
<p class="text-muted">Article added on: {{ today_article["date"] }}</p><br/> <p class="text-muted" id="date">Article added on: {{ today_article["date"] }}</p><br/>
<div class="p-3 mb-2 bg-light text-dark"><br/> <div class="p-3 mb-2 bg-light text-dark"><br/>
<p class="display-5">{{ today_article["article_title"] }}</p><br/> <p class="display-5" id="article_title">{{ today_article["article_title"] }}</p><br/>
<p class="lead"><font id="article" size=2>{{ today_article["article_body"] }}</font></p><br/> <p class="lead"><font id="article" size=2>{{ today_article["article_body"] }}</font></p><br/>
<p><small class="text-muted">{{ today_article['source'] }}</small></p><br/> <p><small class="text-muted" id="source">{{ today_article['source'] }}</small></p><br/>
<p><b>{{ today_article['question'] }}</b></p><br/> <p><b id="question">{{ today_article['question'] }}</b></p><br/>
<script type="text/javascript"> <script type="text/javascript">
function toggle_visibility(id) { {# https://css-tricks.com/snippets/javascript/showhide-element/#} function toggle_visibility(id) { {# https://css-tricks.com/snippets/javascript/showhide-element/#}
const e = document.getElementById(id); const e = document.getElementById(id);
@ -151,6 +151,50 @@
<script src="{{ js }}"></script> <script src="{{ js }}"></script>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
<script>
function load_next_article(){
$.ajax({
url: '/get_next_article/{{username}}',
dataType: 'json',
success: function(data) {
// 更新页面内容
if(data){
$('#user_level').html(data['user_level']);
$('#text_level').html(data["text_level"]);
$('#date').html('Article added on: '+data["date"]);
$('#article_title').html(data["article_title"]);
$('#article').html(data["article_body"]);
$('#source').html(data['source']);
$('#question').html(data["question"]);
$('#answer').html(data["answer"]);
}
}
});
}
function load_pre_article(){
$.ajax({
url: '/get_pre_article/{{username}}',
dataType: 'json',
success: function(data) {
// 更新页面内容
if(data){
$('#user_level').html(data['user_level']);
$('#text_level').html(data["text_level"]);
$('#date').html('Article added on: '+data["date"]);
$('#article_title').html(data["article_title"]);
$('#article').html(data["article_body"]);
$('#source').html(data['source']);
$('#question').html(data["question"]);
$('#answer').html(data["answer"]);
}
}
});
console.info({{visited_articles}})
}
</script>
</body> </body>
<style> <style>
mark { mark {

View File

@ -21,14 +21,10 @@ userService = Blueprint("user_bp", __name__)
path_prefix = '/var/www/wordfreq/wordfreq/' path_prefix = '/var/www/wordfreq/wordfreq/'
path_prefix = './' # comment this line in deployment path_prefix = './' # comment this line in deployment
@userService.route("/get_next_article/<username>",methods=['GET','POST'])
@userService.route("/<username>/reset", methods=['GET', 'POST']) def get_next_article(username):
def user_reset(username): user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
''' session['old_articleID'] = session.get('articleID')
用户界面
:param username: 用户名
:return: 返回页面内容
'''
if request.method == 'GET': if request.method == 'GET':
visited_articles = session.get("visited_articles") visited_articles = session.get("visited_articles")
if visited_articles['article_ids'][-1] == "null": # 如果当前还是“null”则将“null”pop出来,无需index+=1 if visited_articles['article_ids'][-1] == "null": # 如果当前还是“null”则将“null”pop出来,无需index+=1
@ -36,26 +32,22 @@ def user_reset(username):
else: # 当前不为“null”直接 index+=1 else: # 当前不为“null”直接 index+=1
visited_articles["index"] += 1 visited_articles["index"] += 1
session["visited_articles"] = visited_articles session["visited_articles"] = visited_articles
return redirect(url_for('user_bp.userpage', username=username)) visited_articles, data, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
else: else:
return 'Under construction' return 'Under construction'
return json.dumps(data)
@userService.route("/<username>/back", methods=['GET']) @userService.route("/get_pre_article/<username>",methods=['GET'])
def user_back(username): def get_pre_article(username):
''' user_freq_record = path_prefix + 'static/frequency/' + 'frequency_%s.pickle' % (username)
用户界面
:param username: 用户名
:return: 返回页面内容
'''
if request.method == 'GET': if request.method == 'GET':
visited_articles = session.get("visited_articles") visited_articles = session.get("visited_articles")
visited_articles["index"] -= 1 # 上一篇index-=1 visited_articles["index"] -= 1 # 上一篇index-=1
if visited_articles['article_ids'][-1] == "null": # 如果当前还是“null”则将“null”pop出来 if visited_articles['article_ids'][-1] == "null": # 如果当前还是“null”则将“null”pop出来
visited_articles['article_ids'].pop() visited_articles['article_ids'].pop()
session["visited_articles"] = visited_articles session["visited_articles"] = visited_articles
return redirect(url_for('user_bp.userpage', username=username)) visited_articles, data, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles'))
return json.dumps(data)
@userService.route("/<username>/<word>/unfamiliar", methods=['GET', 'POST']) @userService.route("/<username>/<word>/unfamiliar", methods=['GET', 'POST'])
def unfamiliar(username, word): def unfamiliar(username, word):