Introduction to Web development using Flask
Tags: python, webdev
Published: 2020-06-20 14:07:34

You can learn web development with the <a href="https://python.org">Python</a> programming language (Flask).

Flask is a python module that enables web development to be done in Python. A simple web application could be implemented with code.

If you are new to Flask, I recommend <a href="https://gumroad.com/l/IMzBy">this book</a>

## Flask Hello World

The Flask module can be installed with <a href="https://pythonbasics.org/how-to-use-pip-and-pypi/">pip</a>. To use flask just import it in your Python app.

    from flask import Flask

The program below creates the "Hello world" app in Python Flask

```python
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/', methods=['GET'])
    def hello_world():
    	return "Hello world\n"
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000)
```

You can see the following in the terminal, when running the above program, 

```bash
    (venv) -bash-4.2$ python3 hello_world.py
     * Serving Flask app "hello_world" (lazy loading)
     * 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://0.0.0.0:5000/ (Press CTRL+C to quit)
```

The **"hello world** function is added to **'/'** URL. 

```python
    @app.route('/', methods=['GET'])
    def hello_world():
```

The flask app starts with a run() call.

```python
app.run(host='0.0.0.0', port=5000)
```

Use the **app.run(debug = True)** command to enable the debug option, the debugs option will be deactivated by default. 

You can output anything like

<img src="https://d33wubrfki0l68.cloudfront.net/39e9486b028ee4bc7327d6a2c69e27790f95640f/f69aa/wp-content/uploads/2019/10/flask.png" alt="python flask">

You should use Python <a href="https://python-commandments.org/python-2-vs-3/">version 3</a> (or newer).

## Routing

Routing is used to move directly from the URL to the correct Python <a href="https://pythonspot.com/functions/">function</a>.

It is convenient to navigate the web sites without visiting the home page. The decorator **app.route()** is used for URL linking. 

```python
    @app.route('/python', methods=['GET']) # decorator
    def hello_python(): # bind to the method
    	return "Hello Python\n"
```

Open the url localhost:5005/python, it shows the output of hello_python().

## Redirect and errors

The redirect() method returns a reply object when called and
redirects it to another location with the HTTP status code. 

It has this syntax:

     Flask.redirect(location, statuscode, response)

These are the regular HTTP status codes

* HTTP_300_MULTIPLE_CHOICES
* HTTP_301_MOVED_PERMANENTLY
* HTTP_302_FOUND
* HTTP_303_SEE_OTHER
* HTTP_304_NOT_MODIFIED
* HTTP_305_USE_PROXY
* HTTP_306_RESERVED
* HTTP_307_TEMPORARY_REDIRECT

The method **abort()** ends the session with an HTTP error.

    Flask.abort(404)

You can use Flask to create all kinds of web apps, websites, to plot <a href="https://github.com/plotly/plotlyjs-flask-example">[1]</a><a href="https://gitpress.io/u/1282/flask_matplotlib">[2]</a> and more.

Related links

* <a href="https://gumroad.com/l/IMzBy">Python Flask course</a>
* <a href="https://pythonprogramminglanguage.com/">Learn Python</a>