Flask App Tutorial
Tags: flask, python, webdev
Published: 2021-01-08 19:59:52

---
title: Flask App Tutorial
published: true
description: A beginner-friendly tutorial for Flask
tags: flask, python, webdev
canonical_url: https://github.com/app-generator/tutorial-flask-app
cover_image: https://raw.githubusercontent.com/app-generator/static/master/common/thumb-flask-4.png
---
Hello Coders,

This article might help beginners to code a simple App using Flask, the popular Python Framework. If something relevant for a beginner is not here or you are a beginner and a section is not presented well enough, don't hesitate to ask anything in the comments section. Being almost daily on Dev, there are big chances to respond.

---

> Thank you! **Content provided by [AppSeed - App Generator](https://appseed.us)**.

---

## Topics

- Flask Introduction
- Set up (aka environment)
- Edit the code
- Start the application
- Summary

---

## Flask Introduction

:point_right: Flask is a lightweight web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. Compared to Django, Flask provides a lightweight codebase and more freedom to the developer.

Flask is written in [Python](https://www.python.org/) and it does not require particular tools or libraries.

---

- [Flask](https://palletsprojects.com/p/flask/) - the official website
- [Flask Docs](https://flask.palletsprojects.com/en/1.1.x/) - learn what Flask has to offer in detail 

---

<a href="#topics"> :point_up_2: Return to top</a>

---

## Set up

:point_right: To code a Flask application we need some tools properly set up in our workstation. Here is the recommended list:

--- 

- [Python v3](https://www.python.org/downloads/) - the language used by Flask
- A (free) editor: [VsCode](https://code.visualstudio.com/), [Sublime](https://www.sublimetext.com/), [Atom](https://atom.io/), [Notepad++](https://notepad-plus-plus.org/)

---

To finish this tutorial we will work with the console (aka terminal). In Windows, we can use `Powershell` or the corespondent `xterm` for Linux. The next step is to open a terminal and check the Python installation:

```bash
$ python --version
Python 3.8.4
```

If the above command returns an error please double-check the Python installation and get back here after to follow up the next steps.

---

**Install Flask**

Flask is basically a Python package and we can install it with ease via a tool shipped by Python: [PIP](https://pypi.org/project/pip/).

```bash
$ $ pip install Flask
```

---

<a href="#topics"> :point_up_2: Return to top</a>

---

## Edit the code

:point_right: Create a new file named **hello.py** in the current directory and add this code:

---

```python
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return f'My first Flask APP!'
```

This simple code snippet ( also saved [here](https://github.com/app-generator/tutorial-flask-app/blob/main/hello.py) ) does the following:

- Import `Flask`, the library that we've installed in the previous step
- `app` object is constructed by `Flask`. At this point, we can use all features provided by **Flask**
- define a default `route` that will return a simple *Hello* message when someone accesses our application using the browser

---

<a href="#topics"> :point_up_2: Return to top</a>

---

## Start the application

:point_right: Flask being such a nice framework, comes with a simple server to help us check the code without being forced to install 3rd party tools. To use it and see our minimal app running in the browser, a variable must be exported in the `environment` that informs `Flask` the name of the file:

```bash
$ export FLASK_APP=run.py
```

Once we have this variable we can call `Flask`:

```bash
$ flask run
```

By default `Flask` will start the application on address 'localhost:5000'. If we visit the application in the browser, we should see the message:

**My first Flask APP!**

---

<a href="#topics"> :point_up_2: Return to top</a>

---

## Summary

:point_right: During this tutorial, we've learned a few basic things:

- What is Flask and how to install it
- We've coded a super simple app
- We've seen the application running in the browser

---

**Where to go from here?**

- Access the [Flask Tutorial](https://github.com/app-generator/tutorial-flask) and learn more about Flask
- [Learn to code](https://github.com/app-generator/learn-to-code) using other technologies. 

---

<a href="#topics"> :point_up_2: Return to top</a>

---

> Btw, my (nick) name is **Sm0ke** and I'm pretty active also on [Twitter](https://twitter.com/Sm0keDev).