notepad / app.py
shashidharak99's picture
Create app.py
b45c921 verified
raw
history blame contribute delete
891 Bytes
from flask import Flask, render_template, request
import os
app = Flask(__name__)
FILE_NAME = "data.txt"
# create file if not exists
if not os.path.exists(FILE_NAME):
open(FILE_NAME, "w").close()
@app.route("/")
def home():
return render_template("index.html")
@app.route("/operate", methods=["POST"])
def operate():
text = request.form.get("text")
operation = request.form.get("operation")
if operation == "write":
with open(FILE_NAME, "w") as f:
f.write(text + "\n")
elif operation == "append":
with open(FILE_NAME, "a") as f:
f.write(text + "\n")
elif operation == "read":
with open(FILE_NAME, "r") as f:
text = f.read()
else:
text = "Invalid operation"
return render_template("index.html", result=text)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)