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 %}