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 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
You have to import from the flask module render_template
.
The initialisation of the app should have the location of where the html
templates are in. (Flask defaults to looking for a folder named templates
in your root directory if this is not mentioned)
app = Flask(__name__, template_folder='WebSite')
Standard jinja
codes can be written to aid the HTML creation process.
Content Delivery Networks (CDNs) can be used to create the css/javascript styles for the HTML as well. There is a bootstrap library for flask that helps with this. It is not used here.
To Include your own css
, import url_for
from flask. Then add the following line to your HTML base document.
<link rel="stylesheet" type="text/css" href="">
.css
files are present in the folder static/*css
. This does not seem to work for nested directories. Needs to be investigated.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.
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>'
It can be done with the following python script
import secrets
print(secrets.token_hex(16)) #16 byte length
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 ``.
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.