|
|
#!/usr/bin/env/ python
|
|
|
|
|
|
from flask import Flask, render_template, request, Response
|
|
|
# from weasyprint import HTML
|
|
|
from .pagedjs import make_pdf
|
|
|
from .settings import DEBUG, BASEURL
|
|
|
|
|
|
# Spacy tries to import CUDA, do not break when it fails
|
|
|
try:
|
|
|
from .paseo import crear_camino
|
|
|
except ModuleNotFoundError:
|
|
|
pass
|
|
|
|
|
|
import os.path
|
|
|
|
|
|
basepath = os.path.dirname(__file__)
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
# Book HTML is loaded through filesystem, in a tmp dir, make path absolute.
|
|
|
PAGEDJS_STATIC_DIR = os.path.join(basepath, 'static')
|
|
|
|
|
|
@app.route('{}/'.format(BASEURL))
|
|
|
def index():
|
|
|
return render_template('index.html')
|
|
|
|
|
|
@app.route('{}/book'.format(BASEURL), methods=['POST'])
|
|
|
def book():
|
|
|
fragment = max(0, min(1, int(request.form['fragment'])))
|
|
|
|
|
|
first_word = 'un'
|
|
|
|
|
|
if fragment == 0:
|
|
|
novel = os.path.join(basepath, '../data/emilia_prueba.txt')
|
|
|
author = 'Emilia Pardo Bazán' # Non breaking spaces
|
|
|
title = 'La Madre Naturaleza' # Non breaking spaces
|
|
|
else:
|
|
|
novel = os.path.join(basepath, '../data/prueba.txt')
|
|
|
author = 'Benito Pérez Gáldos' # Non breaking spaces
|
|
|
title = 'Miau'
|
|
|
|
|
|
path = crear_camino(novel, first_word)
|
|
|
|
|
|
context = {
|
|
|
'title': title,
|
|
|
'author': author,
|
|
|
'path': path,
|
|
|
'STATIC_DIR': '/static' if DEBUG else PAGEDJS_STATIC_DIR,
|
|
|
'DEBUG': DEBUG
|
|
|
}
|
|
|
|
|
|
html = render_template('book.html', **context)
|
|
|
|
|
|
if (DEBUG):
|
|
|
return html
|
|
|
else:
|
|
|
pdf = make_pdf(html)
|
|
|
|
|
|
r = Response(pdf, mimetype='application/pdf')
|
|
|
|
|
|
r.headers.extend({
|
|
|
'Content-Disposition': 'attachment; filename="Paseo por arboles de madrid.pdf"'
|
|
|
})
|
|
|
|
|
|
return r
|