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.

66 lines
1.6 KiB
Python

3 years ago
#!/usr/bin/env/ python
3 years ago
from flask import Flask, render_template, request, Response
# from weasyprint import HTML
from pagedjs import make_pdf
from settings import DEBUG, BASEURL
3 years ago
# Spacy tries to import CUDA, do not break when it fails
3 years ago
try:
from paseo import crear_camino
3 years ago
except ModuleNotFoundError:
pass
import os.path
basepath = os.path.dirname(__file__)
3 years ago
app = Flask(__name__)
# Book HTML is loaded through filesystem, in a tmp dir, make path absolute.
PAGEDJS_STATIC_DIR = os.path.join(basepath, 'static')
3 years ago
@app.route('{}/'.format(BASEURL))
def index():
3 years ago
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
3 years ago
else:
novel = os.path.join(basepath, '../data/prueba.txt')
author = 'Benito Pérez Gáldos' # Non breaking spaces
3 years ago
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
}
3 years ago
html = render_template('book.html', **context)
if (DEBUG):
return html
else:
pdf = make_pdf(html)
3 years ago
r = Response(pdf, mimetype='application/pdf')
3 years ago
r.headers.extend({
'Content-Disposition': 'attachment; filename="Paseo por arboles de madrid.pdf"'
})
3 years ago
return r