/
app.py
70 lines (58 loc) · 2.26 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
from flask import Flask, make_response, render_template, request
import random
import cairosvg
app = Flask(__name__)
colorlist = ['"#17BF00"', '"#FF9E19"', '"#F33131"', '"#C21F1F"']
@app.route("/test.html")
def test():
return render_template('test.html')
@app.route("/")
def hello():
return "Hello World!"
@app.route('/images/test.png')
def serve_content(svgFile=None):
output = ''
try:
with open(os.path.join(app.root_path, 'roadmap.svg'), 'r') as file_obj:
for oneline in file_obj:
stringindex = oneline.find('<g')
if stringindex == -1:
output += oneline
continue
stringindex = oneline.find('id')
if stringindex == -1:
output += oneline
continue
tempstring = oneline[stringindex:]
randomint = random.randint(0, len(colorlist)-1)
onelist = oneline.split(' ')
outputstr = onelist[0] + ' ' + onelist[1] + ' ' + 'fill=' + colorlist[randomint] + '>\n'
output += outputstr
with open(os.path.join(app.root_path, 'images/test.png'), 'wb') as file_output:
cairosvg.svg2png(output, write_to=file_output)
with open(os.path.join(app.root_path, 'images/test.png')) as f:
response = make_response(f.read())
response.headers['Content-Type'] = 'image/png'
return response
except:
abort(make_response("dumped", 400))
@app.route('/templated.svg')
def templated_svg():
"Example using a template in the templates directory."
width = request.args.get('width', '800')
height = request.args.get('height', '600')
svg = render_template('roadmap.svg', width=width, height=height)
response = make_response(svg)
response.content_type = 'image/svg+xml'
return response
@app.route('/database.svg')
def database_svg():
"Example using a string stored somewhere."
# Read in ./images/letters.svg.
svg = open(os.path.join(app.root_path, 'images', 'roadmap.svg')).read()
response = make_response(svg)
#response.content_type = 'image/svg+xml'
return response
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)