You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB
Python

from flask import Flask, redirect, url_for, request, render_template
# Create the application.
APP = Flask(__name__)
# Define different routes of the application.
@APP.route('/', methods=['GET', 'POST'])
def index():
title = "Hello Flask"
# If something is submitted through the form...
# ref: https://pythonbasics.org/flask-http-methods/
if request.method == 'POST':
# Read the values from the form element.
name = request.form['name']
text = request.form['text']
# Write the submitted data to a file, which is
# in this case a "csv" (comma seperated values) file
# that you can also open with spreadsheet software.
# Note that the file is opened in the mode "a+"
# which means that the Flask application will append values
# (and not overwrite the csv file).
with open('database.csv', 'a+') as db:
db.write(f"{ name},{ text }\n")
msg = "Thanks!"
# PRG pattern(Post Redirect Get pattern), to avoid repetition of contents
# https://en.wikipedia.org/wiki/Post/Redirect/Get
return redirect(url_for('index'))
# If the index page is loaded...
else:
msg = "Please submite the form below."
return render_template('index.html', title=title, msg=msg)
if __name__ == '__main__':
APP.debug = True
APP.run(port = 3000)