DEV Community

loading...

Developing A Website With Flask

lordghostx profile image LordGhostX ・2 min read

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__)
Enter fullscreen mode Exit fullscreen mode
  • Each page is handled by a function, whose route is registered by a decorator
@app.route("/") 
def index():
    return "Hello World!"
Enter fullscreen mode Exit fullscreen mode

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) 
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

You run your development server

app.run()
Enter fullscreen mode Exit fullscreen mode

And that’s it. Your first Flask website that shows the current time. Everything else just expands on this basis.

Alt Text

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.

Discussion (5)

pic
Editor guide
Collapse
cmuralisree profile image
Chittoji Murali Sree Krishna

sometimes we need this in flask

if __name__ == "__main__":
    app.run(debug=True) 
# for debugging we have to include debug is true
# The condition __name__ == "__main__" ensures that the run() method is called only when main.py is run as the main program.
Enter fullscreen mode Exit fullscreen mode
Collapse
ndukwu_pius profile image
Ndukwu Pius Onyema

Where will the index.html file be stored

Collapse
curiouspaul1 profile image
Curious Paul

Create a folder called "templates" and and your html there

Collapse
ndukwu_pius profile image
Collapse
aalphaindia profile image
Pawan Pawar

Keep sharing!