Developing a Website With Flask
Tags: python, webdev, beginners
Published: 2021-01-13 07:36:48

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

```python
from flask import Flask 
 
app = Flask(__name__)
```

* Each page is handled by a function, whose route is registered by a decorator

```python
@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'

```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
<html>

<head>
  <title>My website</title>
</head>

<body>
  {{ current_dt.strftime("%H:%M") }}
</body>

</html>
```

You run your development server

```python
app.run()
```

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cikasezqii6l4e4cke6g.png)

## Flask Resources

* [Flask Official Documentation](https://flask.palletsprojects.com/en/1.1.x/): 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](https://blog.miguelgrinberg.com): 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](https://www.digitalocean.com/community/tutorials/build-a-crud-web-app-with-python-and-flask-part-one): 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](https://www.fullstackpython.com): 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.