Diagrams on web

pull/201/head
tessier 5 years ago
parent 97d73acc0b
commit 64a1250cad

@ -0,0 +1,6 @@
version: "3"
services:
web:
build: ./web
ports: ["5000:5000"]
volumes: ['./web:/app']

@ -0,0 +1,19 @@
FROM alpine:3.12
RUN apk update && apk add --no-cache \
gcc libc-dev g++ graphviz python3 py-pip python3-dev ttf-opensans curl fontconfig
COPY . /app
WORKDIR app
RUN pip3 install -r requirements.txt
# add fonts to support CJK languages
RUN curl -O https://noto-website.storage.googleapis.com/pkgs/NotoSansCJKjp-hinted.zip \
&& mkdir -p /usr/share/fonts/NotoSansCJKjp \
&& unzip NotoSansCJKjp-hinted.zip -d /usr/share/fonts/NotoSansCJKjp/ \
&& rm NotoSansCJKjp-hinted.zip \
&& fc-cache -fv
ENTRYPOINT ["python3"]
CMD ["app.py"]

@ -0,0 +1,56 @@
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def welcome():
"""Top welcome page."""
template = 'welcome.html'
values = {
"aa": "bjc"
}
return render_template(template, **values)
@app.route('/builder', methods=['POST', 'GET'])
def builder():
"""Builder page with python input."""
import os
import subprocess
from flask import request
template = 'builder.html'
values = {}
diag_folder = "static/diagrams/"
code = request.form.get('code')
if code:
# clean the directory
_, _, filenames = next(os.walk(diag_folder))
for one_file in filenames:
os.remove('%s%s' % (diag_folder, one_file))
# write the code in a file and execute
with open("%stemp_code.py" % diag_folder, "w") as f:
f.write(code)
result = subprocess.run(["python3", "temp_code.py"], cwd=diag_folder, capture_output=True)
# TODO get the result if there's error to display on the template
print('stdout: ', result.stdout)
print('stderr: ', result.stderr)
# delete the temp file
os.remove("%stemp_code.py" % diag_folder)
# get the pic to display
_, _, filenames = next(os.walk(diag_folder))
pic_name = filenames[0]
values.update({
"code": code,
"pic_name": pic_name,
})
return render_template(template, **values)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

@ -0,0 +1,2 @@
diagrams
Flask

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 KiB

@ -0,0 +1,6 @@
<form action="" method="POST">
<textarea name="code" rows="8" cols="80">{{code}}</textarea>
<input type="submit" value="Build">
</form>
<img src="/static/diagrams/{{pic_name}}" alt="">

@ -0,0 +1,3 @@
<img src="static/diagrams.png" alt="Diagrams">
You can use the builder <a href="/builder">Here</a>
Loading…
Cancel
Save