Flask
Very Basic Usage of Flask

Overview

Flask is a lightweight python web framework, that allows you to build dynamic websites. In other words, websites whose content isn’t the same for everyone. Think social networking websites, or E-commerce websites. Even though you and someone else can access the same home page, it will be different for different users.

This framework consists of libraries and function decorators that can allow you to setup a server on your system. This server then renders webpages (that you write in HTML CSS, for starters) for you.

The HTML file that you write can have jinja syntax in it, which is what gives the HTML pages its dynamic nature.

This blog is haphazardly written, intended to just give me some reminders of the various commands and procedures on using Flask.

I am still new to using it, so when I learn more, I will update this blog and add some polish to it!

If you want to learn about how to build Flask apps, I strongly suggest watching these set of Youtube tutorials by Corey Schafer. They are the best tutorials on how to use this, hands down!

Running it

Running a server

export FLASK_APP = flask_site.py

will export the name of the main flask site rendering python file o the environment variable FLASK_APP which will enable you to run a server with

flask run

Rendering Webpages.

FORMS

Instead of making the form directly in the HTML document, Flask can aid with the creation of it with the help of the flask_wtf package. Create a forms.py file and add the information of the form that you want to create in it. Each form is a class, having various attributes. Look at the forms.py here for more information about it.

Form authentication.

In the main flask_site.py, add a secret key for the app with

app = Flask(__name__, template_folder='WebSite')
app.config['SECRET_KEY']='<SecretKeyHere>'

Generation of a secret key

It can be done with the following python script

import secrets
print(secrets.token_hex(16)) #16 byte length

Form rendering

Import the created class into flask_site.py, as done in the file in the repository. The next step is to add this class to the HTML form. Pass an instance of the created form class to whichever HTML doc has to render the form, in its app route.

Whenever a form is submitted (provided that is is validated as well), depending on what the action= field is set to, that method of the route will be called.

For example, if action='' in the HTML <form action=''>, then the same route that rendered the HTML will be run again.

If it was instead <form action='some_route'>, then the method underapp.route("some_route") in the routes.py would be called.

In the HTML tag, you can access the form fields only if it is passed in with the render_template function as a parameter.

For example, return render_template("result.html",value=asin). In the HTML file result.html, the value asin can be used with the jinja syntax ``.

Much More.

This is just to get me restarted on Flask. There is a whole lot more that can be done, and not covered here. I will update it when I get the time.