Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
FILE_NAME = "data.txt"
|
| 7 |
+
|
| 8 |
+
# create file if not exists
|
| 9 |
+
if not os.path.exists(FILE_NAME):
|
| 10 |
+
open(FILE_NAME, "w").close()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@app.route("/")
|
| 14 |
+
def home():
|
| 15 |
+
return render_template("index.html")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@app.route("/operate", methods=["POST"])
|
| 19 |
+
def operate():
|
| 20 |
+
|
| 21 |
+
text = request.form.get("text")
|
| 22 |
+
operation = request.form.get("operation")
|
| 23 |
+
|
| 24 |
+
if operation == "write":
|
| 25 |
+
with open(FILE_NAME, "w") as f:
|
| 26 |
+
f.write(text + "\n")
|
| 27 |
+
|
| 28 |
+
elif operation == "append":
|
| 29 |
+
with open(FILE_NAME, "a") as f:
|
| 30 |
+
f.write(text + "\n")
|
| 31 |
+
|
| 32 |
+
elif operation == "read":
|
| 33 |
+
with open(FILE_NAME, "r") as f:
|
| 34 |
+
text = f.read()
|
| 35 |
+
|
| 36 |
+
else:
|
| 37 |
+
text = "Invalid operation"
|
| 38 |
+
|
| 39 |
+
return render_template("index.html", result=text)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
app.run(host="0.0.0.0", port=7860)
|