Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries. It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
Introduction
Developing a website with Flask is essentially writing a regular Python code with a little bit of website-specific
code. Flask is very lightweight; it gives you the essentials, but that’s it — apart from plugins.
To get started, you don’t need to know much more than the following:
- You create a Flask instance
from flask import Flask
app = Flask(__name__)
- Each page is handled by a function, whose route is registered by a decorator
@app.route("/")
def index():
return "Hello World!"
The app.route
bit simply registers the function below to a specific path. It’s an alternative to app.add_url_rule('/', 'index', index)
. It’s just telling the app to refer to the index function whenever a user requests '/'.
- Each of these functions can render a website using the data that you got using 'regular Python'
import datetime
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
current_dt = datetime.datetime.now()
return render_template("index.html", current_dt=current_dt)
You need a templates/index.html
file which is written using Jinja2. This is regular HTML mixed with a specific syntax for logic and to use the data that 'was sent to' the template using the render_template
function.
<html>
<head>
<title>My website</title>
</head>
<body>
{{ current_dt.strftime("%H:%M") }}
</body>
</html>
You run your development server
app.run()
And that’s it. Your first Flask website that shows the current time. Everything else just expands on this basis.
Flask Resources
Flask Official Documentation: Even though there are so many resources out there on the internet, the best resource that contains everything about Flask is its official documentation, whether you are a beginner or an expert you will find yourself in its guidance.
Miguel Grinberg Blog: This guy’s blog is great especially for beginners, you learn by building some real-world web applications, from blogs to web APIs, after covering it, you can find yourself at the level of intermediate in Flask framework.
Scotch, Building Dream-team Web Application: This post helps you to learn some cool stuff about flask by Building a real-world and working web application from scratch, you will learn how to start your application, connect to the database, how to use ORM (Object Relational Mapper) - (SQLAlchemy), Structure of applications and many more.
Fullstackpython: This is another great tool for python (Flask) programmers, it contains many resources about all frameworks which are powered by python and some great guidance for beginners as well as experts.
Top comments (8)
sometimes we need this in flask
Where will the index.html file be stored
Create a folder called "templates" and and your html there
Thanks
u need to create a folder named templates where you will keep all the html docs
Keep sharing!
Concise!
Keep sharing Boss!
Thank you very much