1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-08-17 00:20:42 +03:00

Simplify test webapp to not use kalamar.

This commit is contained in:
Simon Sapin 2011-08-05 11:49:05 +02:00
parent 97d78d1205
commit 623f585722
9 changed files with 46 additions and 121 deletions

2
.gitignore vendored
View File

@ -1,5 +1,3 @@
*.pyc
*.egg-info
.coverage
weasy/tests/test_web/website/static/*result*

View File

@ -128,9 +128,6 @@ class PNGDocument(Document):
fd.write(self.output.getvalue())
fd.close()
def get_png_data(self):
return self.output.getvalue().encode('base64')
class PDFDocument(Document):
def __init__(self, dom):
@ -155,4 +152,3 @@ class PDFDocument(Document):
fd = open(filename, 'wr')
fd.write(self.output.getvalue())
fd.close()

2
weasy/tests/test_web/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
input*
output*

View File

@ -1,8 +1,47 @@
#!/usr/bin/env python
from website import create_app
import os.path
from flask import Flask, request, render_template
from weasy.document import PNGDocument, PDFDocument
from weasy.draw import draw_page_to_png
#import StringIO
app = Flask(__name__)
INPUT = os.path.join(app.root_path, 'input.html')
PNG_OUTPUT = os.path.join(app.root_path, 'output.png')
PDF_OUTPUT = os.path.join(app.root_path, 'output.pdf')
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method != 'POST':
with open(INPUT) as fd:
content = fd.read()
return render_template('index.html.jinja2', content=content, image=None)
content = request.form.get("content", "").strip()
# Save the input HTML
with open(INPUT, 'w') as fd:
fd.write(content)
pdf_document = PDFDocument.from_string(content)
pdf_document.output = PDF_OUTPUT
pdf_document.do_layout()
pdf_document.draw()
png_document = PNGDocument.from_string(content)
png_document.output = PNG_OUTPUT
png_document.do_layout()
png_document.draw_page(0)
with open(PNG_OUTPUT, 'rb') as fd:
image = fd.read()
return render_template('index.html.jinja2', content=content, image=image)
if __name__ == '__main__':
app = create_app()
app.run(port=12290)
app.run(port=12290, debug=True)

View File

@ -21,7 +21,7 @@ img {
</form>
</div>
{% if image %}
<img src="data:image/png;base64,{{image}}" alt="Draw" />
<img src="data:image/png;base64,{{ image.encode('base64') }}" alt="Draw" />
{% endif %}
</body>
</html>

View File

@ -1,41 +0,0 @@
# -*- coding: utf-8 -*-
"""
Flask application creation module.
"""
import os
import kalamar.site
from flask import g, Flask
from apps import frontend
from kalamar.access_point.filesystem import FileSystem, FileSystemProperty
def make_kalamar(config):
""" Init kalamar and access points """
kalamar_site = kalamar.site.Site()
prefix = os.path.dirname(os.path.abspath(__file__))
files = FileSystem(os.path.join(prefix, 'static'),
'(.*?)',
[("name", FileSystemProperty(unicode))],
content_property = "data")
kalamar_site.register("files", files)
return kalamar_site
def create_app():
app = Flask(__name__)
app.debug = True
app.register_module(frontend.app)
kalamar_site = make_kalamar(app.config)
app.jinja_env.trim_blocks = True
app.jinja_env.autoescape = True
@app.before_request
def constants():
""" Global context constant injector."""
g.kalamar = kalamar_site
return app

View File

@ -1,46 +0,0 @@
# -*- coding: utf-8 -*-
import os
from flask import Module, g, request, render_template
from weasy.document import PNGDocument, PDFDocument
from weasy.draw import draw_page_to_png
import StringIO
app = Module(__name__)
@app.route('/')
def index():
content = g.kalamar.open('files',{"name":"input.html"})["data"].read()
return render_template('index.html.jinja2', content=content, image=None)
@app.route('/', methods=("POST",))
def post():
content = request.values.get("content", "").strip("\r\n").strip(" ")
# Save the input HTML
item = g.kalamar.open('files',{"name":"input.html"})
item['data'].write(content)
item.save()
pdf_document = PDFDocument.from_string(content)
pdf_document.do_layout()
pdf_document.draw()
png_document = PNGDocument.from_string(content)
png_document.do_layout()
png_document.draw_page(0)
# save the png result
item = g.kalamar.open('files',{"name":"png_result.png"})
item['data'].write(png_document.output.getvalue())
item.save()
# save the pdf result
item = g.kalamar.open('files',{"name":"pdf_result.pdf"})
item['data'].write(pdf_document.output.getvalue())
item.save()
image = png_document.get_png_data()
return render_template('index.html.jinja2', content=content, image=image)

View File

@ -1,23 +0,0 @@
<style>
html {
background-color:gray;
font-family: DejaVu Sans Mono;
font-size:15px;
}
p {
width:480px;
margin:40px;
padding: 10px;
border-width:20px;
vertical-align:middle;
border-width:1px;
border-style:solid;
}
h1 {
text-decoration : underline;
}
</style>
<h1>Avancement de weasyprint</h1>
<p>test test <img src="http://img41.xooimage.com/files/5/f/5/477876-2877733.jpg" border="1" alt=""/></p>