-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
50 lines (38 loc) · 1.2 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
import sqlite3
from flask import Flask, jsonify, render_template, request, url_for
app = Flask(__name__, static_folder="static", template_folder="templates")
@app.route("/")
def index():
return app.send_static_file("index.html")
@app.route("/display/<number>")
def display(number):
conn = sqlite3.connect("text.db")
cursor = conn.cursor()
text = (
conn.execute("SELECT doc FROM documents WHERE rowid=?", (number,))
.fetchone()[0]
.decode()[1:-1]
)
return render_template("display.html", text=text)
@app.route("/create", methods=["POST"])
def create():
""" Creates new entry in database for text """
conn = sqlite3.connect("text.db")
cursor = conn.cursor()
data = request.data
# check for max input size
if len(data) > 1000000:
return jsonify({"url": "error: text too long"})
cursor.execute("INSERT INTO documents (doc) VALUES (?)", (data,))
conn.commit()
rowid = cursor.lastrowid
return jsonify(
{
"url": "{}{}".format(
request.base_url.replace("create", "")[:-1],
url_for("display", number=rowid),
)
}
)
if __name__ == "__main__":
app.run()