以下是我原来的两(2)个功能:
def user_bible(query):
output = requests.get("http://getbible.net/json?passage={0}".format(query))
json_dict_output = json.loads(output.text.strip("();"))
before_for_loop_parse = json_dict_output[u'book'][0][u'chapter'] #[u'2'][u'verse']
keys = before_for_loop_parse.keys()
keys.sort(compare_number_strings)
stored_list = []
for k in keys:
stored_list.append(before_for_loop_parse[k][u'verse'])
return parse_rough_draft(json_dict_output)
和我的第二个小功能:
def compare_number_strings(string1, string2):
return cmp(int(string1), int(string2))
然后我的导师帮我编辑了一个新功能,允许在功能的输出中显示章节和节文数字
def parse_rough_draft(json_dict_output):
books = json_dict_output[u'book']
result_dict={}
for i in books:
book_name_variable = i[u'book_name']
current_book_value = result_dict.setdefault(book_name_variable, {})
chapter_variable = i[u'chapter_nr']
chapter_value = current_book_value.setdefault(chapter_variable, {})
all_chapter_verses_variable = i[u'chapter']
for m in all_chapter_verses_variable.keys():
verse = all_chapter_verses_variable[m]
chapter_value[m] = verse[u'verse']
return result_dict
但是,我的新函数parse rough draft():
会输出一个未排序的列表。我正在尝试对此列表进行排序,然后比较列表的整数,以便输出将以正确的数字顺序显示来自“Jn 2:4-19”的经文。在这个函数parse rough draft():
中,我在for循环上面定义了一个字典,因此for循环中的所有内容都包含在我的result_dict
字典中?
我补充说:
keys = result_dict.keys()
keys.sort(compare_number_strings)
return keys
但是现在我的jinja2模板页面上的输出只打印输出'Book:John' 来自以下Bible.html模板
</form>
{% with messages = get_flashed_messages() %}
{% with books = messages[0] %}
{% if books %}
<div class="well">
<div class="row" style="margin: 50px;margin-right: 100px; font-size:150%;">
<div class="col-md-12">
<!--<h5>Scripture:</h5> -->
{% for book in books %}
<div class="book">
Book: {{book}}
{% for chapter in books[book]%}
<div class="chapter">
Chapter: {{chapter}}
{% with sorted_keys = books[book][chapter].keys() %}
{% for verse in sorted_keys %}
<div class="verse">
Verse: {{verse}}
<div class="verse-text">{{books[book][chapter][verse]}}</div>
</div>
{% endfor %}
{% endwith %}
</div>
{% endfor %}
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}
{% endwith %}
{% endwith %}
</div>
这个功能可以在这里看到: http://shielded-brushlands-1568.herokuapp.com/Bible/
和github repo在这里: https://github.com/phillipsk/webapp/blob/master/templates/bible.html