from flask import Flask
from flask import render_template
from flask import request
from flask import flash
from flask import redirect
from flask import url_for
import os
app = Flask(__name__, template_folder='templates')
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
@app.route("/")
def hello():
return render_template('index.html')
@app.route("/index")
def index():
return render_template('index.html')
@app.route('/savedata', methods=['GET', 'POST'])
def savedata():
if request.method == 'POST':
filename = request.form.get('filename')
filecontent = request.form.get('filecontent')
filepath = 'static/temp/' +filename+ '.txt'
f = open(filepath, "a")
f.write(filecontent)
f.close()
message = 'File is Created'
return render_template('Display-Data.html', message=message,filepath=filepath)
if request.method == 'GET':
return render_template('get-is-not-supported.html')
@app.route('/listfiles')
def listfiles():
entries = os.listdir('static/temp/')
return render_template('List-Files.html', entries=entries)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.4/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body><div class="container">
<h2>Create a File</h2>
<form action="/savedata" method="POST">
<div class="form-group">
<label for="usr">File Name:</label>
<input type="text" class="form-control" id="filename" name="filename">
</div>
<div class="form-group">
<label for="comment">Contents :</label>
<textarea class="form-control" rows="5" id="filecontent" name="filecontent"></textarea>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div></body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Personal Details</title>
</head>
<body>
<div>{{ message }}. To download file <a href='{{ filepath }}'>Click here</a></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Test App</title>
</head>
<body>
<ul>
{% for item in entries %}
<li><a href='static/temp/{{ item }}'>{{ item }}</a> </li>
{% endfor %}
</ul>
</body>
</html>
Get method support is not available