Flask Beginnings
Tags: python, flask, beginners, webdev
Published: 2021-03-02 23:59:26


<!-- reference to the first flask image link -->
[flask-image]: https://i.imgur.com/FYIDNjD.png
[pinterest-link]: https://www.pinterest.com/ 
[pip-link]: https://pypi.org/project/pip/
[pypi-link]: https://pypi.org/
[code-image]: https://i.imgur.com/CLxU0RI.jpg
[flask-run]: https://i.imgur.com/nm39PdS.png
[flask-doc]: https://flask.palletsprojects.com/
[miguel-blog]: https://blog.miguelgrinberg.com/
[miguel-book]: https://www.amazon.com/Flask-Web-Development-Developing-Applications/dp/1491991739#:~:text=Flask%20Web%20Development%3A%20Developing%20Web,9781491991732%3A%20Amazon.com%3A%20Books

![flask-image]  

**Flask** is a micro web-framework, *micro* in the sense that it doesn't come with out-of-the-box functionalities, desired functionalities are rather added by extensions. Flask also does not rely on pre-existing third-party libraries or frameworks, Flask's main dependencies were all authored by the creator of Flask himself.

---

## Why you should learn Flask?
- It is easier to learn if you're new to web development, but not Python.
- It has a large and supportive community  
- It is faster to develop once you're grounded in your knowledge of Python. You can use it to easily complete your next side project  
- It is in demand, being one of the popular web frameworks, it is used a lot in production.  
- It can be used to build fast websites that can handle traffic, flask is [Pinterest's][pinterest-link] core technology, with _322 million active users_.  
- Finally, the documentation is a treasure, as it is very _comprehensive_ and _beginner-friendly_.  


## Requirements for learning flask
- Knowledge of basic *Python* concepts (generators, decorators, e.t.c)  
- A computer with **python,** **a web browser**, and a **text/code editor** installed.  
- A **_brain_**

## Installing Flask
If you're familiar with the Interactive Python console, you know about **_pip_**.  

[PIP][pip-link] (Preferred Installer Program) is the package installer for Python and it can install any package from [PyPi][pypi-link] - Python Package Index.  

pip comes pre-installed with Python since version 2.7+ and 3.4+, so you must have python installed, flask is also a package in PyPI.

To check if you have pip installed open your command-line interface, and run the command:

``` bash
pip --version
```
If your output looks like below, it means you have it installed

```bash
pip X.Y.Z ...\site-packages\pip (python X.Y)
```

Else if it looks in anyway like this:
```bash
'pip' is not recognized as an internal or external command,
operable program or batch file.
```
Then you should download and install pip before continuing.  

Once that is settled, flask can now be installed using pip, enter into the command-line:  

```bash
pip install flask
```

---  


## First Flask Web Application.

Once you're done with the flask installation, you're ready to write your first flask web application.

![code-image]

```python
from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "<h1> Hello world </h1>"
```

That is all the code you need to spin your first flask web app, don't worry if you don't understand anything yet, I'll explain the different components as you read on.  

The first line:

```python
from flask import Flask
```  

If you've used Python, you're familiar with import statements, here you're importing the **_Flask_** class from the flask module.

The next line we've got:  

```python
app = Flask(__name__)
```

The **_app_** object is an **_instance_** of the **_Flask class_**, it is created by passing the \_\_name__ argument to the Flask constructor, the \_\_name__ argument contains information about the python script you're currently writing your web application in.  
Flask uses the \_\_name__ argument to determine the file to run.  

> The **_app_** object above handles all requests made from the client to the server.  


The next block:

```python
@app.route("/")
def index():
    return "<h1> Hello world </h1>"
```

When the webserver receives requests, it passes them to the Flask application instance, but the flask application instance does not know what to do for each URL request, that's where the **_decorator_** and **_function_** above comes in.  

The decorator _@app.route("/")_ contains a **_route_** method that takes a string (actually **_a URL_**) as an argument, and the function defined below it is a **_view function_**, whatever is returned by the function is the **_response_**.  
When a request comes in, the _route_ method needs to know what code to run for each request, therefore it maps the _URL_ to the function defined below the decorator - _view function_, where all the processing occurs before a _response_ is rendered.  

---  


## Flask Development Web Server
Now to view your response in the web browser, you'll have to start the Flask server, flask comes with a development web server not suitable for production (for production you'd have to put it behind a WSGI (Web Server Gateway Interface) server like **_Gunicorn_**).

To start the flask server, you'd first have to make the webserver know what file to run, to do that you'll set the **_FLASK_APP_** environment variable to our _app.py_ web app file.

To do that run the command:

```bash
set FLASK_APP=app.py
```
> For Unix users replace **_set_** with **_export_**

Now you can run your app on the server:

```bash
flask run

 * Serving Flask app "app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
```

The server is started, now copy the address **_http://127.0.0.1:5000/_** to your browser.

![flask-run]  

> The _response_ returned by the _view function_ can get more complex and the need to return a response based on the context of the request would rather make you return **Templates** rather than just a string containing HTML elements (a bunch of these inside the python code would make the code unmaintainable).  

---  

## Flask resources:  
> - [The Flask Documentation][flask-doc]
> - [Micheal Grinberg blog][miguel-blog]
> - [Flask Web Development: Developing Web Applications with Python book][miguel-book]