Issue
I am creating a simple python web app using the Flask framework to list the alteration of a letter or a series of letters in a word (paragrams). The console (IntelliJ code editor) prints out the paragrams successfully, but I am unable to print them/list them on an HTML page in an unordered HTML list - Please see screenshot of HTML page below
I tried (''.join) and (','.join(str(e) for e in paragram_list)) method but they didn't work for me as shown in my code below. Please see the last return statement (return "There are " + str(num_of_paragrams) + " Paragrams for " + "''" + str(input_word) + "'\n'" +
" ... Here They Are I WANT THESE IN STRING FORMAT OR A LIST: " + str(paragram_list[paragram]) +
" ----> THIS SHOWS ONLY ONE alteration ----> " + str(''.join(values[paragram])))
The console of my code editor prints the line below and I would like the same to be printed on HTML
Here is a list of the paragrams of the word abc abc acb bac bca cab cba
Code below:
from itertools import permutations, count
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route("/")
def index():
html_form = """
<html><body>
<h1> Enter a word to find its paragrams.</h1>
<h2> This is the alteration of a letter or a series of letters in a word. </h2>
<br>
<br>
<form action="" method="GET">
Text: <input type="text" name="Paragrams">
<input type="submit" value="Find Paragrams">
</form>
</body></html>"""
input_word = request.args.get("Paragrams", "")
if input_word:
wordsparagrams = str(find_paragram(input_word))
# print(wordsparagrams)
else:
wordsparagrams = ""
return html_form + wordsparagrams
def find_paragram(input_word):
# input_word = input("Enter a word to find its paragrams\n")
values = list(permutations(input_word, len(input_word)))
print("Here is a list of the paragrams of the word " + input_word)
count_of = 0
paragram_list = []
for paragram in range(len(values)):
print(''.join(values[paragram]))
# return ''.join(values[paragram])
count_of += 1
paragram_list.append(values)
make_it_a_string = ','.join(str(e) for e in paragram_list)
print("list items \n")
print(paragram_list)
print("make it a string: ", make_it_a_string)
num_of_paragrams = count_of
print("Paragrams of:", input_word, " = ", num_of_paragrams)
return "There are " + str(num_of_paragrams) + " Paragrams for " + "'\'" + str(input_word) + "'\n'" + \
" ... Here They Are I WANT THESE IN STRING FORMAT OR A LIST: " + str(paragram_list[paragram]) +\
" ----> THIS SHOWS ONLY ONE alteration ----> " + str(''.join(values[paragram]))
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8080, debug=True)
HTML page looks:
Solution
Code structure:
templates
└───index.html
main.py
(we need to create templates folder because that's were flask searches for templates by default)
main.py:
import itertools
from flask import Flask, request, render_template
app = Flask(__name__)
def find_paragrams(word: str) -> list:
'''Returns list of word paragrams'''
# Get permutations as list of tuples of letters
permutations = list(itertools.permutations(word, len(word)))
# Join tuples inside of list
permutations = [''.join(permutation_tuple)
for permutation_tuple in permutations]
# Get rid of duplicates
permutations = list(set(permutations))
return permutations
@app.route("/", methods=['GET', 'POST'])
def index():
# Default value in case if an empty sting was passed into form
params = None
# POST request on form submit
if request.method == 'POST':
# Get form data
word = request.form['paragrams']
paragrams_list = find_paragrams(word) if word else None
if paragrams_list:
paragrams_count = len(paragrams_list)
paragrams_str = ', '.join(paragrams_list)
# Packing parametes in dict just to be clean
params = {
'paragrams_count': paragrams_count,
'word': word,
'paragrams_str': paragrams_str,
'paragrams_list': paragrams_list
}
# Render html template with jinja rendering parameters
return render_template(
'index.html',
params=params,
)
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8080, debug=True)
index.html:
<html>
<body>
<h1> Enter a word to find its paragrams.</h1>
<h2> This is the alteration of a letter or a series of letters in a word. </h2>
<br>
<br>
<form action="" method="POST">
Word: <input type="text" name="paragrams">
<input type="submit" value="Find Paragrams">
</form>
<!-- Render inner html if params argument was not None -->
{% if params %}
<!-- Accessing params dict's values with params.key -->
<p>There are {{ params.paragrams_count }} Paragrams for "{{ params.word }}"</p>
<p>In string format:</p>
{{ params.paragrams_str}}
<p>In list format:</p>
{{ params.paragrams_list }}
{% else %}
<!-- html to render if params argument was None -->
<p>Empty field, enter word first!</p>
{% endif %}
</body>
</html>
(feel free to contact me if anything stays unclear)
Answered By - Barni Pack
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.