From ee03f31efb4ed0020cd64948c9ca97df5691c2e8 Mon Sep 17 00:00:00 2001 From: mb Date: Tue, 1 Nov 2022 11:54:12 +0100 Subject: [PATCH] pushing the flask example --- README.md | 22 ++++++++++++++++++++++ app.py | 39 +++++++++++++++++++++++++++++++++++++++ static/main.css | 3 +++ templates/base.html | 12 ++++++++++++ templates/index.html | 15 +++++++++++++++ 5 files changed, 91 insertions(+) create mode 100644 README.md create mode 100644 app.py create mode 100644 static/main.css create mode 100644 templates/base.html create mode 100644 templates/index.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..1f99913 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# Flask example + +This is a Flask example that you can use to make web applications. + +## Install + +You need to install Flask on your computer (or on the server you are working): + +`$ pip3 install flask` + +## Run the web application + +You can run the example application in different ways, here two examples: + +* `$ python3 app.py` +* `$ flask --debug run` + +## Documentation + +* +* + diff --git a/app.py b/app.py new file mode 100644 index 0000000..61b9fed --- /dev/null +++ b/app.py @@ -0,0 +1,39 @@ +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!" + return render_template('index.html', title=title, msg=msg) + + # 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 = 5000) \ No newline at end of file diff --git a/static/main.css b/static/main.css new file mode 100644 index 0000000..2dcbd73 --- /dev/null +++ b/static/main.css @@ -0,0 +1,3 @@ +body{ + background: lavender; +} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..b3b0cc5 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,12 @@ + + + + + {{ title }} + + + + {% block content %} + {% endblock %} + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..5c88c48 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block content %} +
+

{{ title }}

+

{{ msg }}

+
+ + +

+

+

+
+
+{% endblock %} \ No newline at end of file