From 9e2ba11ab46e360606d33dd2ccc9c287585ab88f Mon Sep 17 00:00:00 2001 From: kerem Date: Thu, 27 Jun 2024 13:54:08 +0200 Subject: [PATCH] Setions Cookies and Flahs Messages --- flask_to_docker/.old/app_fifth.py | 13 + flask_to_docker/.old/app_fourth.py | 73 + flask_to_docker/.old/app_second.py | 60 +- flask_to_docker/.old/app_six.py | 75 + flask_to_docker/.old/app_third.py | 81 +- flask_to_docker/.old/intro_app.py | 54 - .../.old/templates_fifth/base.html | 15 + .../.old/templates_fifth/index.html | 15 + .../{templates => templates_fourth}/base.html | 0 .../templates_fourth}/download.html | 0 .../.old/templates_fourth/index.html | 53 + flask_to_docker/.old/templates_six/base.html | 26 + flask_to_docker/.old/templates_six/index.html | 15 + flask_to_docker/.old/templates_six/login.html | 13 + .../.old/templates_third/base.html | 11 + .../filter.html | 0 .../{templates => templates_third}/index.html | 0 .../{templates => templates_third}/other.html | 0 flask_to_docker/README.md | 210 +- flask_to_docker/app.py | 106 +- flask_to_docker/dbapp/app.py | 20 + flask_to_docker/dbapp/models.py | 13 + flask_to_docker/dbapp/routes.py | 9 + flask_to_docker/dbapp/run.py | 6 + .../02142466-a4d1-4886-874b-ef034decd958.cvs | 487 - .../12938864-b01a-4eff-98f0-86938e9624bb.cvs | 487 - .../3446b44d-1886-4c4b-985d-380319b4b20f.cvs | 487 - .../4ed91d18-ad04-4064-9351-1f6733068ccc.cvs | 487 - .../5a2fc356-f673-4be5-802e-62f6612e4ed0.cvs | 487 - .../61a68d92-2f39-4686-95da-86e702f08f8a.cvs | 487 - .../717cd001-6724-405b-a949-eb7f84a71180.cvs | 487 - .../9e18203b-6dcc-45a4-86ed-313513667852.cvs | 487 - .../ab92eadf-ae38-42f3-8c32-921097f7e6da.cvs | 487 - .../bb6bc927-6533-41ea-843b-7b43cdb62f10.cvs | 487 - flask_to_docker/file.txt | 1 - flask_to_docker/static/css/bootstrap-grid.css | 4085 ++++++ .../static/css/bootstrap-grid.css.map | 1 + .../static/css/bootstrap-grid.min.css | 6 + .../static/css/bootstrap-grid.min.css.map | 1 + .../static/css/bootstrap-grid.rtl.css | 4084 ++++++ .../static/css/bootstrap-grid.rtl.css.map | 1 + .../static/css/bootstrap-grid.rtl.min.css | 6 + .../static/css/bootstrap-grid.rtl.min.css.map | 1 + .../static/css/bootstrap-reboot.css | 597 + .../static/css/bootstrap-reboot.css.map | 1 + .../static/css/bootstrap-reboot.min.css | 6 + .../static/css/bootstrap-reboot.min.css.map | 1 + .../static/css/bootstrap-reboot.rtl.css | 594 + .../static/css/bootstrap-reboot.rtl.css.map | 1 + .../static/css/bootstrap-reboot.rtl.min.css | 6 + .../css/bootstrap-reboot.rtl.min.css.map | 1 + .../static/css/bootstrap-utilities.css | 5402 +++++++ .../static/css/bootstrap-utilities.css.map | 1 + .../static/css/bootstrap-utilities.min.css | 6 + .../css/bootstrap-utilities.min.css.map | 1 + .../static/css/bootstrap-utilities.rtl.css | 5393 +++++++ .../css/bootstrap-utilities.rtl.css.map | 1 + .../css/bootstrap-utilities.rtl.min.css | 6 + .../css/bootstrap-utilities.rtl.min.css.map | 1 + flask_to_docker/static/css/bootstrap.css | 12057 ++++++++++++++++ flask_to_docker/static/css/bootstrap.css.map | 1 + flask_to_docker/static/css/bootstrap.min.css | 6 + .../static/css/bootstrap.min.css.map | 1 + flask_to_docker/static/css/bootstrap.rtl.css | 12030 +++++++++++++++ .../static/css/bootstrap.rtl.css.map | 1 + .../static/css/bootstrap.rtl.min.css | 6 + .../static/css/bootstrap.rtl.min.css.map | 1 + flask_to_docker/static/css/style.css | 5 + flask_to_docker/static/img/logo.png | Bin 0 -> 32670 bytes flask_to_docker/templates/base.html | 17 +- flask_to_docker/templates/index.html | 56 +- flask_to_docker/templates/login.html | 13 + 72 files changed, 45029 insertions(+), 5107 deletions(-) create mode 100644 flask_to_docker/.old/app_fifth.py create mode 100644 flask_to_docker/.old/app_fourth.py create mode 100644 flask_to_docker/.old/app_six.py delete mode 100644 flask_to_docker/.old/intro_app.py create mode 100644 flask_to_docker/.old/templates_fifth/base.html create mode 100644 flask_to_docker/.old/templates_fifth/index.html rename flask_to_docker/.old/{templates => templates_fourth}/base.html (100%) rename flask_to_docker/{templates => .old/templates_fourth}/download.html (100%) create mode 100644 flask_to_docker/.old/templates_fourth/index.html create mode 100644 flask_to_docker/.old/templates_six/base.html create mode 100644 flask_to_docker/.old/templates_six/index.html create mode 100644 flask_to_docker/.old/templates_six/login.html create mode 100644 flask_to_docker/.old/templates_third/base.html rename flask_to_docker/.old/{templates => templates_third}/filter.html (100%) rename flask_to_docker/.old/{templates => templates_third}/index.html (100%) rename flask_to_docker/.old/{templates => templates_third}/other.html (100%) create mode 100644 flask_to_docker/dbapp/app.py create mode 100644 flask_to_docker/dbapp/models.py create mode 100644 flask_to_docker/dbapp/routes.py create mode 100644 flask_to_docker/dbapp/run.py delete mode 100644 flask_to_docker/downloads/02142466-a4d1-4886-874b-ef034decd958.cvs delete mode 100644 flask_to_docker/downloads/12938864-b01a-4eff-98f0-86938e9624bb.cvs delete mode 100644 flask_to_docker/downloads/3446b44d-1886-4c4b-985d-380319b4b20f.cvs delete mode 100644 flask_to_docker/downloads/4ed91d18-ad04-4064-9351-1f6733068ccc.cvs delete mode 100644 flask_to_docker/downloads/5a2fc356-f673-4be5-802e-62f6612e4ed0.cvs delete mode 100644 flask_to_docker/downloads/61a68d92-2f39-4686-95da-86e702f08f8a.cvs delete mode 100644 flask_to_docker/downloads/717cd001-6724-405b-a949-eb7f84a71180.cvs delete mode 100644 flask_to_docker/downloads/9e18203b-6dcc-45a4-86ed-313513667852.cvs delete mode 100644 flask_to_docker/downloads/ab92eadf-ae38-42f3-8c32-921097f7e6da.cvs delete mode 100644 flask_to_docker/downloads/bb6bc927-6533-41ea-843b-7b43cdb62f10.cvs delete mode 100644 flask_to_docker/file.txt create mode 100644 flask_to_docker/static/css/bootstrap-grid.css create mode 100644 flask_to_docker/static/css/bootstrap-grid.css.map create mode 100644 flask_to_docker/static/css/bootstrap-grid.min.css create mode 100644 flask_to_docker/static/css/bootstrap-grid.min.css.map create mode 100644 flask_to_docker/static/css/bootstrap-grid.rtl.css create mode 100644 flask_to_docker/static/css/bootstrap-grid.rtl.css.map create mode 100644 flask_to_docker/static/css/bootstrap-grid.rtl.min.css create mode 100644 flask_to_docker/static/css/bootstrap-grid.rtl.min.css.map create mode 100644 flask_to_docker/static/css/bootstrap-reboot.css create mode 100644 flask_to_docker/static/css/bootstrap-reboot.css.map create mode 100644 flask_to_docker/static/css/bootstrap-reboot.min.css create mode 100644 flask_to_docker/static/css/bootstrap-reboot.min.css.map create mode 100644 flask_to_docker/static/css/bootstrap-reboot.rtl.css create mode 100644 flask_to_docker/static/css/bootstrap-reboot.rtl.css.map create mode 100644 flask_to_docker/static/css/bootstrap-reboot.rtl.min.css create mode 100644 flask_to_docker/static/css/bootstrap-reboot.rtl.min.css.map create mode 100644 flask_to_docker/static/css/bootstrap-utilities.css create mode 100644 flask_to_docker/static/css/bootstrap-utilities.css.map create mode 100644 flask_to_docker/static/css/bootstrap-utilities.min.css create mode 100644 flask_to_docker/static/css/bootstrap-utilities.min.css.map create mode 100644 flask_to_docker/static/css/bootstrap-utilities.rtl.css create mode 100644 flask_to_docker/static/css/bootstrap-utilities.rtl.css.map create mode 100644 flask_to_docker/static/css/bootstrap-utilities.rtl.min.css create mode 100644 flask_to_docker/static/css/bootstrap-utilities.rtl.min.css.map create mode 100644 flask_to_docker/static/css/bootstrap.css create mode 100644 flask_to_docker/static/css/bootstrap.css.map create mode 100644 flask_to_docker/static/css/bootstrap.min.css create mode 100644 flask_to_docker/static/css/bootstrap.min.css.map create mode 100644 flask_to_docker/static/css/bootstrap.rtl.css create mode 100644 flask_to_docker/static/css/bootstrap.rtl.css.map create mode 100644 flask_to_docker/static/css/bootstrap.rtl.min.css create mode 100644 flask_to_docker/static/css/bootstrap.rtl.min.css.map create mode 100644 flask_to_docker/static/css/style.css create mode 100644 flask_to_docker/static/img/logo.png create mode 100644 flask_to_docker/templates/login.html diff --git a/flask_to_docker/.old/app_fifth.py b/flask_to_docker/.old/app_fifth.py new file mode 100644 index 00000000..3177103a --- /dev/null +++ b/flask_to_docker/.old/app_fifth.py @@ -0,0 +1,13 @@ +from flask import Flask, request, make_response, Response, render_template, redirect, url_for, send_from_directory, jsonify +import pandas as pd +import os +import uuid +app = Flask(__name__, template_folder='templates', static_folder='static', static_url_path='/') + +@app.route('/', methods=['GET', 'POST']) +def index(): + return render_template('index.html') + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000, debug=True) diff --git a/flask_to_docker/.old/app_fourth.py b/flask_to_docker/.old/app_fourth.py new file mode 100644 index 00000000..5d457963 --- /dev/null +++ b/flask_to_docker/.old/app_fourth.py @@ -0,0 +1,73 @@ +from flask import Flask, request, make_response, Response, render_template, redirect, url_for, send_from_directory, jsonify +import pandas as pd +import os +import uuid +app = Flask(__name__, template_folder='templates') + + +@app.route('/', methods=['GET', 'POST']) +def index(): + if request.method == 'GET': + return render_template('index.html') + elif request.method == 'POST': + username = request.form.get('username') + password = request.form.get('password') + + if username == 'kynsight' and password == 'pass': + return 'Success' + else: + return 'Failure' + + +@app.route('/file_upload', methods=['GET', 'POST']) +def file_upload(): + file = request.files.get('file') + if file.content_type == 'text/plain': + return file.read().decode() + elif file.content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' or file.content_type == 'application/vnd.ms-excel': + df = pd.read_excel(file) + return df.to_html() + +@app.route('/convert_csv', methods=['GET', 'POST']) +def convert_csv(): + file = request.files.get('file') + df = pd.read_excel(file) + response = Response( + df.to_csv(), + mimetype = 'text/csv', + headers={'Content-Disposition': 'attachement; filename=result.csv'} + ) + return response + +@app.route('/convert_csv_two', methods=['GET', 'POST']) +def convert_csv_two(): + file = request.files.get('file') + df = pd.read_excel(file) + + if not os.path.exists('downloads'): + os.makedirs('downloads') + + filename = f'{uuid.uuid4()}.cvs' + df.to_csv(os.path.join('downloads', filename)) + + return render_template('download.html', filename=filename) + +@app.route('/download/', methods=['GET', 'POST']) +def download(filename): + return send_from_directory('downloads', filename, download_name='result.csv') + + + +@app.route('/handle_post', methods=['GET', 'POST']) +def handle_post(): + greeting = request.json.get('greeting') + name = request.json.get('name') + + with open('file.txt','w') as f: + f.write(f'{greeting}, {name}') + + return jsonify({'message': 'Successfully written!'}) + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000, debug=True) diff --git a/flask_to_docker/.old/app_second.py b/flask_to_docker/.old/app_second.py index 922fc6ed..6d040370 100644 --- a/flask_to_docker/.old/app_second.py +++ b/flask_to_docker/.old/app_second.py @@ -1,34 +1,54 @@ -from flask import Flask, request, make_response, Response, render_template, redirect, url_for +from flask import Flask, request, make_response, Response -app = Flask(__name__, template_folder='templates') +app = Flask(__name__) @app.route('/') def index(): - myval = 'Kynsight' - myres = 30 - mylist = [10,20,30,40,50] - return render_template('index.html', value=myval, result=myres, list=mylist) + return "

Welcome

" -@app.route('/aaawwa') -def other(): - return render_template('other.html') +# We can also return some responses (like 404 page not found and co) +@app.route('/hello', methods=['GET']) +def hello(): + response = make_response('Hello World') + response.status_code = 202 + response.headers['content-type'] = 'application/octet-stream' + return response -@app.route('/filter') -def filter(): - sometext="Hewllo World" - return render_template('filter.html', sometext=sometext) -# Custom filter -@app.template_filter('reverse_string') -def reverse_string(s): - return s[::-1] +@app.route('/test_methodes', methods=['GET', 'POST']) +def test_methodes(): + if request.method == 'GET': + return f"You made a GET request\n" + if request.method == 'POST': + return f"You made a POST request\n" + else: + return f"You will never see this message\n" -@app.route('/redirect_endpoint') -def redirect_endpoint(): - return redirect(url_for('other')) +# This is a url Processor +@app.route('/greet/') +def greet(name): + return f"Hello {name}" + + +# This is a url Processor +@app.route('/add//') +def add(number1, number2): + return f'{number1} + {number2} = {number1+number2}' + + +# Handle parameters : +@app.route('/handle_url_params') +def handle_url_params(): + if 'greeting' in request.args.keys() and 'name' in request.args.keys(): + greeting = request.args['greeting'] + name = request.args.get('name') + return f'{greeting}, {name}' + else: + return f'some params are missing' + if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True) diff --git a/flask_to_docker/.old/app_six.py b/flask_to_docker/.old/app_six.py new file mode 100644 index 00000000..382511fa --- /dev/null +++ b/flask_to_docker/.old/app_six.py @@ -0,0 +1,75 @@ +from flask import Flask, request, make_response, Response, render_template, redirect, url_for, send_from_directory, jsonify, session, flash +import pandas as pd +import os +import uuid + + +app = Flask(__name__, template_folder='templates', static_folder='static', static_url_path='/') +app.secret_key = 'SOME KEY' + + +@app.route('/', methods=['GET', 'POST']) +def index(): + return render_template('index.html') + + +@app.route('/set_data') +def set_data(): + session['name'] = 'Session1' + session['other'] = 'HelloWorld' + return render_template('index.html', message='Session data set.') + + +@app.route('/get_data') +def get_data(): + if 'name' in session.keys() and 'other' in session.keys(): + name = session['name'] + other = session['other'] + return render_template('index.html', message=f'Name: {name} Content: {other}.') + else: + return render_template('index.html', message='Session has no data set.') + + +@app.route('/clear_session') +def clear_session(): + session.clear() + return render_template('index.html', message='Session has been cleared') + + +@app.route('/set_cookie') +def set_cookie(): + response = make_response(render_template('index.html', message='Cookie Set')) + response.set_cookie('cookie_name', 'cookie_value') + return response + + +@app.route('/get_cookie') +def get_cookie(): + cookie_value = request.cookies['cookie_name'] + return render_template('index.html', message=f'Cookie value: {cookie_value}') + + +@app.route('/remove_cookie') +def remove_cookie(): + response = make_response(render_template('index.html', message='Cookie removed')) + response.set_cookie('cookie_name', expires=0) + return response + + +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'GET': + return render_template('login.html') + elif request.method == 'POST': + username = request.form.get('username') + password = request.form.get('password') + if username == 'kynsight' and password == 'pass': + flash('Successfull Login!') + return render_template('index.html', message="") + else: + flash('Login Failed!') + return render_template('index.html', message="") + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000, debug=True) diff --git a/flask_to_docker/.old/app_third.py b/flask_to_docker/.old/app_third.py index 5d457963..922fc6ed 100644 --- a/flask_to_docker/.old/app_third.py +++ b/flask_to_docker/.old/app_third.py @@ -1,73 +1,34 @@ -from flask import Flask, request, make_response, Response, render_template, redirect, url_for, send_from_directory, jsonify -import pandas as pd -import os -import uuid +from flask import Flask, request, make_response, Response, render_template, redirect, url_for + app = Flask(__name__, template_folder='templates') -@app.route('/', methods=['GET', 'POST']) +@app.route('/') def index(): - if request.method == 'GET': - return render_template('index.html') - elif request.method == 'POST': - username = request.form.get('username') - password = request.form.get('password') - - if username == 'kynsight' and password == 'pass': - return 'Success' - else: - return 'Failure' - - -@app.route('/file_upload', methods=['GET', 'POST']) -def file_upload(): - file = request.files.get('file') - if file.content_type == 'text/plain': - return file.read().decode() - elif file.content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' or file.content_type == 'application/vnd.ms-excel': - df = pd.read_excel(file) - return df.to_html() - -@app.route('/convert_csv', methods=['GET', 'POST']) -def convert_csv(): - file = request.files.get('file') - df = pd.read_excel(file) - response = Response( - df.to_csv(), - mimetype = 'text/csv', - headers={'Content-Disposition': 'attachement; filename=result.csv'} - ) - return response - -@app.route('/convert_csv_two', methods=['GET', 'POST']) -def convert_csv_two(): - file = request.files.get('file') - df = pd.read_excel(file) - - if not os.path.exists('downloads'): - os.makedirs('downloads') - - filename = f'{uuid.uuid4()}.cvs' - df.to_csv(os.path.join('downloads', filename)) - - return render_template('download.html', filename=filename) + myval = 'Kynsight' + myres = 30 + mylist = [10,20,30,40,50] + return render_template('index.html', value=myval, result=myres, list=mylist) -@app.route('/download/', methods=['GET', 'POST']) -def download(filename): - return send_from_directory('downloads', filename, download_name='result.csv') +@app.route('/aaawwa') +def other(): + return render_template('other.html') +@app.route('/filter') +def filter(): + sometext="Hewllo World" + return render_template('filter.html', sometext=sometext) -@app.route('/handle_post', methods=['GET', 'POST']) -def handle_post(): - greeting = request.json.get('greeting') - name = request.json.get('name') - - with open('file.txt','w') as f: - f.write(f'{greeting}, {name}') +# Custom filter +@app.template_filter('reverse_string') +def reverse_string(s): + return s[::-1] - return jsonify({'message': 'Successfully written!'}) +@app.route('/redirect_endpoint') +def redirect_endpoint(): + return redirect(url_for('other')) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True) diff --git a/flask_to_docker/.old/intro_app.py b/flask_to_docker/.old/intro_app.py deleted file mode 100644 index 6d040370..00000000 --- a/flask_to_docker/.old/intro_app.py +++ /dev/null @@ -1,54 +0,0 @@ -from flask import Flask, request, make_response, Response - -app = Flask(__name__) - - -@app.route('/') -def index(): - return "

Welcome

" - - -# We can also return some responses (like 404 page not found and co) -@app.route('/hello', methods=['GET']) -def hello(): - response = make_response('Hello World') - response.status_code = 202 - response.headers['content-type'] = 'application/octet-stream' - return response - - -@app.route('/test_methodes', methods=['GET', 'POST']) -def test_methodes(): - if request.method == 'GET': - return f"You made a GET request\n" - if request.method == 'POST': - return f"You made a POST request\n" - else: - return f"You will never see this message\n" - - -# This is a url Processor -@app.route('/greet/') -def greet(name): - return f"Hello {name}" - - -# This is a url Processor -@app.route('/add//') -def add(number1, number2): - return f'{number1} + {number2} = {number1+number2}' - - -# Handle parameters : -@app.route('/handle_url_params') -def handle_url_params(): - if 'greeting' in request.args.keys() and 'name' in request.args.keys(): - greeting = request.args['greeting'] - name = request.args.get('name') - return f'{greeting}, {name}' - else: - return f'some params are missing' - - -if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000, debug=True) diff --git a/flask_to_docker/.old/templates_fifth/base.html b/flask_to_docker/.old/templates_fifth/base.html new file mode 100644 index 00000000..cd17647d --- /dev/null +++ b/flask_to_docker/.old/templates_fifth/base.html @@ -0,0 +1,15 @@ + + + + {% block title %}Flask APP {% endblock %} + + + + + + +

This is from the base ;)

+ {% block content %} {% endblock %} + + +{% endblock %} diff --git a/flask_to_docker/.old/templates/base.html b/flask_to_docker/.old/templates_fourth/base.html similarity index 100% rename from flask_to_docker/.old/templates/base.html rename to flask_to_docker/.old/templates_fourth/base.html diff --git a/flask_to_docker/templates/download.html b/flask_to_docker/.old/templates_fourth/download.html similarity index 100% rename from flask_to_docker/templates/download.html rename to flask_to_docker/.old/templates_fourth/download.html diff --git a/flask_to_docker/.old/templates_fourth/index.html b/flask_to_docker/.old/templates_fourth/index.html new file mode 100644 index 00000000..578a531e --- /dev/null +++ b/flask_to_docker/.old/templates_fourth/index.html @@ -0,0 +1,53 @@ +{% extends "base.html" %} + +{% block title %}Index Page{% endblock %} + +{% block content %} +

Hellooo

+
+
+
+ +
+ +

File Upload

+
+
+ +
+ +

Convert To CSV

+
+
+ +
+ +

Convert To CSV Two

+
+
+ +
+ +

JS JSON request

+ + + +{% endblock %} diff --git a/flask_to_docker/.old/templates_six/base.html b/flask_to_docker/.old/templates_six/base.html new file mode 100644 index 00000000..b748ddfc --- /dev/null +++ b/flask_to_docker/.old/templates_six/base.html @@ -0,0 +1,26 @@ + + + + {% block title %}Flask APP {% endblock %} + + + + + + +

This is from the base ;)

+ + {% with messages = get_flashed_messages() %} + {% if messages %} +
    + {% for message in messages %} +
  • {{ message }}
  • + {% endfor%} +
+ {% endif %} + {% endwith %} + + {% block content %} {% endblock %} + ` +```html +{% extends "base.html" %} + +{% block title %}Index Page{% endblock %} + +{% block content %} +

Hello Wolrd

+ +

Hello

+ + Button Text + Text + + + +{% endblock %} +``` + +--- + +# Session and Cookies [YouTube](https://www.youtube.com/watch?v=8jOgqA7nlLo&list=PL7yh-TELLS1EyAye_UMnlsTGKxg8uatkM&index=7) +*Sessions* and *cookies* are a very important part of a website. in shot +- *Cookie* are on the *client side* and can be accessed by the user ! +- *session* on the other hand are stored on the *server side* and are very good to keep sensitive information that the user should not be anble to see or modify. + +So let'a jump in to our *app.py* and let me show you how where we create, read, and delete *cookies* and *sessions* +- inludes and intitaltion +```python +from flask import session +app.secret_key = 'SOME KEY' +``` +- create a *session* and implment some *variables* called *keys* and render *index.html* with a *message* +```python +@app.route('/set_data') +def set_data(): + session['name'] = 'Session1' + session['other'] = 'HelloWorld' + return render_template('index.html', message='Session data set.') +``` +- First we check if the *key* exist with `if 'name' in session.keys()` +- Then we can put them into into varibale, and generate *index.html* with a message inculde those *variables* +```python +@app.route('/get_data') +def get_data(): + if 'name' in session.keys() and 'other' in session.keys(): + name = session['name'] + other = session['other'] + return render_template('index.html', message=f'Name: {name} Content: {other}.') + else: + return render_template('index.html', message='Session has no data set.') +``` +- here is how to *clear* a *session*. +```python +@app.route('/clear_session') +def clear_session(): + session.clear() + return render_template('index.html', message='Session has been cleared') +``` +- creating a *cookie* requires a *response* command as it is stored on the *client side* side +- First we create a response containig the *index.html* to render. +- and add a *cookie* to this *response*. +```python +@app.route('/set_cookie') +def set_cookie(): + response = make_response(render_template('index.html', message='Cookie Set')) + response.set_cookie('cookie_name', 'cookie_value') + return response +``` +- To get the *cookies* content we need a *request*, again as it is stored on de *client side*. +```python +@app.route('/get_cookie') +def get_cookie(): + cookie_value = request.cookies['cookie_name'] + return render_template('index.html', message=f'Cookie value: {cookie_value}') +``` +- To dekete the *cookies* content we a requires a *response*, again this time with `response.set_cookie('cookie_name', expires=0)` *expires=0* indicating that the cookie has expired. +```python +@app.route('/remove_cookie') +def remove_cookie(): + response = make_response(render_template('index.html', message='Cookie removed')) + response.set_cookie('cookie_name', expires=0) + return response +``` + +--- + +# Message Flashing [YouTube](https://youtu.be/8jOgqA7nlLo?si=H8vJWGqg1mJAODzs&t=938) +This can be made without using javascript, so it is cool ! *flash* module form *flask* allows us to implment flashing messages that *jinja* can intreprate. This can be very usefull to implment when we need to track the flow of opperation on a website. +Here is an example how to implement it on our *app.py*, *index.html*, *base.html* and showing it by creating a small *login.html* +- The route to be created at *app.py* dont forget to inlcude *flash* from *FLask*: +- With the *'GET'* indicates that we are oppneing the page so we generate *login.html* +- Afterwards whne we put our infomration an send a *'POST* request trough *login.html* we check if the data is correct an flash a message accordingly. Ex:`flash('Successfull Login!')`. +```python +@app.route('/login', methods=['GET', 'POST']) +def login(): + if request.method == 'GET': + return render_template('login.html') + elif request.method == 'POST': + username = request.form.get('username') + password = request.form.get('password') + if username == 'kynsight' and password == 'pass': + flash('Successfull Login!') + return render_template('index.html', message="") + else: + flash('Login Failed!') + return render_template('index.html', message="") +``` +- content of *login.html* +- with `` we cna see that we point back to `def login():`. +```html +{% extends "base.html" %} + +{% block title %}login Page{% endblock %} + +{% block content %} +

Login

+
+ + + +
+{% endblock %} +``` +- when we flash the message we want it to be seen on each page so we include it on *base.html* on the `` +- Please be careful and note that the `get_flashed_messages()` function is *not* placed in the `{% block content %} {% endblock %}` +```html + +

This is from the base ;)

+ {% with messages = get_flashed_messages() %} + {% if messages %} +
    + {% for message in messages %} +
  • {{ message }}
  • + {% endfor%} +
+ {% endif %} + {% endwith %} + + {% block content %} {% endblock %} + +

Hello Wolrd

+

{{ message }}

+ set session data
+ get session data
+ clear session data
+ set cookie
+ get cookie
+ remove cookie
+ Login
{% endblock %} diff --git a/flask_to_docker/templates/login.html b/flask_to_docker/templates/login.html new file mode 100644 index 00000000..2854d5ad --- /dev/null +++ b/flask_to_docker/templates/login.html @@ -0,0 +1,13 @@ + +{% extends "base.html" %} + +{% block title %}login Page{% endblock %} + +{% block content %} +

Login

+
+ + + +
+{% endblock %}