You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
4.9 KiB
Python
165 lines
4.9 KiB
Python
2 years ago
|
from flask import Flask, render_template, Response, abort, url_for, request
|
||
|
# from weasyprint import HTML
|
||
|
import os
|
||
|
|
||
|
from pagedjs import make_pdf_from_url
|
||
|
from settings import DEBUG, HTML_TMP_DIR, SITEURL
|
||
|
import tempfile
|
||
|
from werkzeug.security import safe_join
|
||
|
|
||
|
from dreaming_trees import call_trees, open_dreams, part_of_speech, convert_token_to_text, replace_pos, highlight_word, in_between
|
||
|
|
||
|
# create list of dictionaries with Forest, ha, Tree_1, Tree_2, Tree_3, CO2 in tonnes, ratio
|
||
|
file = "data_trees.csv"
|
||
|
data = call_trees(file)
|
||
|
|
||
|
# open file with dreams
|
||
|
textfile = "dreams_selection_till_p47.txt"
|
||
|
dreaming = open_dreams(textfile)
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
|
||
|
"""
|
||
|
GENERAL:
|
||
|
Everything you print here, shows up in the terminal.
|
||
|
If you want to have it show up in the html pages add the texts there in html.
|
||
|
import your script mentioning the different functions
|
||
|
|
||
|
add this for commands in script with functions
|
||
|
if __name__ == '__main__':
|
||
|
-> if you call script by its own name, it will execute the commands,
|
||
|
otherwise it will only activate the functions to be called elsewhere
|
||
|
|
||
|
|
||
|
"""
|
||
|
|
||
|
"""
|
||
|
Shows the index page of the application
|
||
|
"""
|
||
|
@app.route('/')
|
||
|
def index():
|
||
|
return render_template('index.html')
|
||
|
|
||
|
|
||
|
|
||
|
"""
|
||
|
Shows the first form or view in Flask
|
||
|
the app.route is the webaddress
|
||
|
you need to create the template in templates, and specify what variables you want to use in that template
|
||
|
"""
|
||
|
@app.route('/step1')
|
||
|
def step_1():
|
||
|
locations = []
|
||
|
for element in data:
|
||
|
locations.append(element['Forest'])
|
||
|
return render_template('step1.html', locations=locations)
|
||
|
|
||
|
|
||
|
"""
|
||
|
Shows the second form
|
||
|
You need to get the chosen variable from the first template, by using request
|
||
|
And again, specify the variables you will be using in the template
|
||
|
"""
|
||
|
@app.route('/step2', methods=['GET', 'POST'])
|
||
|
def step_2():
|
||
|
location = request.form['location']
|
||
|
trees = [data[int(location)]['Tree_0'], data[int(location)]['Tree_1'], data[int(location)]['Tree_2']]
|
||
|
return render_template('step2.html', location=location, trees=trees)
|
||
|
|
||
|
"""
|
||
|
This is where the book generation happens.
|
||
|
Get the different variables the reader has chosen by using the request function.
|
||
|
Declare all variable you want to use in the book template.
|
||
|
Specify these variables in html = render.template('book.html'...)
|
||
|
"""
|
||
|
@app.route('/generate', methods=['GET', 'POST'])
|
||
|
def generate ():
|
||
|
location = request.form['location']
|
||
|
place = data[int(location)]['Forest']
|
||
|
tree = request.form['tree']
|
||
|
name_tree = "Tree_"+tree
|
||
|
dream_tree = data[int(location)][name_tree]
|
||
|
selected_description = name_tree + "_description"
|
||
|
description_tree = data[int(location)][selected_description]
|
||
|
|
||
|
doc, tokens, verbs, nouns, adjectives, articles = part_of_speech(dreaming)
|
||
|
tokenized_text = convert_token_to_text(doc)
|
||
|
|
||
|
if float(data[int(location)]["CO2"]) < 1:
|
||
|
# if ration is <1
|
||
|
# replace articles and nouns by spaces of same length
|
||
|
article_text = replace_pos(tokenized_text, articles)
|
||
|
final_textlist = replace_pos(article_text, nouns)
|
||
|
final_text = " ".join(final_textlist)
|
||
|
|
||
|
elif float(data[int(location)]["CO2"]) > 110000:
|
||
|
verb_text = highlight_word(tokenized_text, verbs)
|
||
|
final_textlist = highlight_word(verb_text, nouns)
|
||
|
final_text = " ".join(final_textlist)
|
||
|
|
||
|
else:
|
||
|
final_textlist = in_between(tokenized_text, nouns)
|
||
|
final_text = " ".join(final_textlist)
|
||
|
print(final_textlist)
|
||
|
|
||
|
"""
|
||
|
Insert your own Python code in this function.
|
||
|
"""
|
||
|
|
||
|
# Rendering of the template. Forward generated data to the template
|
||
|
# using named arguments, for example, if a variable chapters was generated
|
||
|
# you can add it to the function like so:
|
||
|
# html = render_template('book.html', DEBUG=DEBUG, chapters=chapters)
|
||
|
html = render_template('book.html', DEBUG=DEBUG, tree=tree, dream_tree=dream_tree, location=location, \
|
||
|
description_tree=description_tree, final_text=final_text, place=place)
|
||
|
if (DEBUG):
|
||
|
return html
|
||
|
else:
|
||
|
pdf = make_pdf(html)
|
||
|
|
||
|
r = Response(pdf, mimetype='application/pdf')
|
||
|
|
||
|
r.headers.extend({
|
||
|
'Content-Disposition': 'attachment; filename="Title of the.pdf"'
|
||
|
})
|
||
|
|
||
|
return r
|
||
|
|
||
|
"""
|
||
|
Proxy(?) for pagedjs, it stores the provided html in a separate file.
|
||
|
Now pagedjs can connect to the application through HTTP.
|
||
|
|
||
|
This way static files, like fonts, images and css can be served in a regular way.
|
||
|
|
||
|
"""
|
||
|
def make_pdf (html):
|
||
|
if not os.path.exists(HTML_TMP_DIR):
|
||
|
os.mkdir(HTML_TMP_DIR)
|
||
|
|
||
|
with tempfile.NamedTemporaryFile(mode='w', prefix='book_', dir=HTML_TMP_DIR, delete=False) as tmpfile:
|
||
|
tmpfile.write(html)
|
||
|
tmpfile.flush()
|
||
|
|
||
|
bookname = os.path.basename(tmpfile.name)
|
||
|
url = SITEURL + url_for('show_book', bookname=bookname)
|
||
|
tmpfile.close()
|
||
|
|
||
|
# Generate the pdf with pagedjs
|
||
|
return make_pdf_from_url(url)
|
||
|
|
||
|
|
||
|
"""
|
||
|
View for pagedjs. It loads the generated HTML from the tmp dir and returns it.
|
||
|
"""
|
||
|
@app.route('/book/<string:bookname>')
|
||
|
def show_book (bookname):
|
||
|
bookpath = safe_join(HTML_TMP_DIR, bookname)
|
||
|
|
||
|
if os.path.exists(bookpath):
|
||
|
with open(bookpath, 'r') as h:
|
||
|
html = h.read()
|
||
|
|
||
|
return html
|
||
|
|
||
|
abort(404)
|