Spaces:
Sleeping
Sleeping
| 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() | |
| def home(): | |
| return render_template("index.html") | |
| 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) |