Flask Cheat Sheet - And FREE samples
Tags: webdev, flask, python, appseed
Published: 2020-12-15 18:39:48

---
title: Flask Cheat Sheet - And FREE samples
published: true
description: Just another Flask Cheat Sheet (10 rules) and a shortlist with FREE samples to play: Datta Able, Pixel, Argon Dashboard
tags: webdev, flask, python, appseed
canonical_url: https://blog.appseed.us/flask-cheat-sheet-and-free-templates-63zo/
cover_image: https://raw.githubusercontent.com/app-generator/flask-pixel-bootstrap-uikit/master/media/flask-pixel-bootstrap-uikit-screen.png
---
Hello Coders,

This article is just another [Flask Cheat Sheet](https://blog.appseed.us/flask-cheat-sheet-and-free-templates-63zo/) that might help beginners to speed up their learning curve and the rest of our audience with some nice production-ready samples. For newcomers, **[Flask](https://appseed.us/admin-dashboards/flask)** is a lightweight web application framework written in Python. Sometimes classified as a microframework, Flask provides a lightweight codebase that can be easily extended to become an API, a simple web app, or a complex eCommerce platform.

---

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

---

- (Free) [Flask Pixel UI Kit](https://appseed.us/apps/flask-apps/flask-pixel-bootstrap-uikit) - product page
- (Free) [Flask Datta Able](https://appseed.us/admin-dashboards/flask-dashboard-dattaable) - product page
- (Free) [Flask Volt Free](https://appseed.us/admin-dashboards/flask-dashboard-volt) - product page
- (Free) [Flask Argon](https://appseed.us/admin-dashboards/flask-boilerplate-dashboard-argon) - product page

---

**1# - [What is Flask](https://docs.appseed.us/what-is/flask/)**

Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. Classified as a microframework,  Flask aims to keep the core of its framework small but highly extensible.

---

**2# - Install Flask**

```bash
$ pip install Flask
```

---

**3# - Creating an isolated environment**

```bash
$ python -m venv flask_env
```

**Activate virtual environment**

```bash
$ source flask_env/bin/activate
```

Deactivate virtual environment

```bash
$ deactivate
```

---

**4# - A super simple Flask app**

```python
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return f'Hello from Flask!'
```

---

**5# - Start the app**

```bash
$ env FLASK_APP=hello.py flask run
 * Serving Flask app "hello"
 * Running on http://127.0.0.1:5000/
```

---

**6# - Object-Based Configuration**

Create a new config file in the project root:

`PROJECT_ROOT/app/config.py`

Add content using OOP and class inheritance

```python

class BaseCfg(object):
    SECRET_KEY = 'SuperS3cretKEY_1222'
    DEBUG = False
    TESTING = False

class DevelopmentCfg(BaseCfg):
	DEBUG = True
	TESTING = True

class TestingCfg(BaseCfg):
	DEBUG = False
	TESTING = True

class ProductionCfg(BaseCfg):
    SECRET_KEY = 'HvFDDcfsnd__9nbCdgsada' 
	DEBUG = False
	TESTING = False
```

Let's use the configuration:

Update `PROJECT_ROOT/app/__init__.py` to use it:

```python
from flask import Flask
from app import config

app = Flask(__name__)
app.config.from_object(config.DevelopmentCfg) # <--- Magic line
```

---

**7# - Render page using Jinja**

The Flask controller

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

@app.route("/test")
def template_test():
    return render_template('template.html', my_string="Jinja Works!!")

if __name__ == '__main__':
    app.run(debug=True)
```

The HTML template

```html
<html>
  <head>
    <title>Flask Template Example</title>
  </head>
  <body>
    <div class="container">
      <p>Result: {{my_string}}</p>
    </div>
  </body>
</html>
```

---

By accessing `http://localhost:5000/test` in the browser, we should see:

*Status: Jinja Works*

---

**8# - Using Flask shell**

```bash
$ flask shell
```

---

**9# - Define a model with SQLAlchemy using SQLite**

Create `PROJECT_ROOT/app/models.py` with the following content:

```python
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class User(db.Model):

    id       = db.Column(db.Integer,     primary_key=True)
    user     = db.Column(db.String(64),  unique = True)
    email    = db.Column(db.String(120), unique = True)
    password = db.Column(db.String(500))

    def __init__(self, user, email, password):
        self.user       = user
        self.password   = password
        self.email      = email
```

Update `PROJECT_ROOT/app/config.py` with the Database connection string:

```python
class BaseCfg(object):
    SECRET_KEY = 'SuperS3cretKEY_1222'
    DEBUG = False
    TESTING = False

    # This will create a file in <app> FOLDER
    SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
    SQLALCHEMY_TRACK_MODIFICATIONS = False
```

Update `PROJECT_ROOT/app/__init__.py` to use the model:

```python
from flask_sqlalchemy import SQLAlchemy
...

db = SQLAlchemy  (app) # flask-sqlalchemy
```

The `User` tables can be created using flask shell:

```bash
$ flask shell
>>> from app import db
>>> db.create_all()
```

The second method is to create the tables automatically at the first request using a hook provided by Flask.

Update `PROJECT_ROOT/app/__init__.py` as below:

```python
# Setup database
@app.before_first_request
def initialize_database():
    db.create_all()
```

---

**10# - List users via Flask shell**

```bash
$ flask shell
>>> from app.models import User
>>> User.query.all()
# HERE we should see all users 
```

--- 

> Let's see this theory applied to these open-source Flask samples:

---

## [Flask Pixel UI Kit](https://appseed.us/apps/flask-apps/flask-pixel-bootstrap-uikit)

Open-Source Web App coded in **[Flask](http://appseed.us/admin-dashboards/flask)** Framework - features:

- UI Kit: Pixel UI kit by Themesberg
- UI-Ready, Jinja2 templating
- SQLite database, Flask-SQLAlchemy ORM
- Session-Based auth flow (login, register)

---

- [Pixel Lite Flask.](https://flask-pixel-bootstrap-uikit.appseed.us/) - product page
- [Pixel Lite Flask](https://appseed.us/apps/flask-apps/flask-pixel-bootstrap-uikit) - LIVE Demo

---

![Flask Pixel UI Kit - Open-source Flask starter, provided by AppSeed.](https://raw.githubusercontent.com/app-generator/flask-pixel-bootstrap-uikit/master/media/flask-pixel-bootstrap-uikit-screen-2.png)

---

## [Flask Datta Able](https://appseed.us/admin-dashboards/flask-dashboard-dattaable)

**[Flask Dashboard](http://appseed.us/admin-dashboards/flask)** generated by the AppSeed platform on top of Datta Able (free version), a modern Bootstrap 4 dashboard template. The Flask codebase is provided with authentication, database, ORM, and deployment scripts.

- [Flask Datta Able](https://appseed.us/admin-dashboards/flask-dashboard-dattaable) - the product page
- [Flask Datta Able](https://flask-dashboard-dattaable.appseed.us/) - Demo 

---

![Flask Datta Able.](https://raw.githubusercontent.com/app-generator/flask-dashboard-dattaable/master/media/flask-dashboard-dattaable-screen.png) 

---

## [Flask Volt Free](https://appseed.us/admin-dashboards/flask-dashboard-volt)

Open-Source **[Flask Dashboard](http://appseed.us/admin-dashboards/flask)** coded with basic modules, database, ORM and deployment scripts on top of Volt Dashboard (free version), a modern Bootstrap dashboard design.

Volt is a free and open source Bootstrap 5 Admin Dashboard featuring over 100 components, 11 example pages and 3 customized plugins. Volt does not require jQuery as a dependency meaning that every library and script's are jQuery free.

- [Flask Dashboard Volt](https://appseed.us/admin-dashboards/flask-dashboard-volt) - product page
- [Flask Dashboard Volt - Demo](https://flask-dashboard-volt.appseed.us/) - LIVE deployment

---

![Flask Volt Free.](https://raw.githubusercontent.com/app-generator/flask-dashboard-volt/master/media/flask-dashboard-volt-screen.png)

---

## [Flask Dashboard Argon](https://appseed.us/admin-dashboards/flask-boilerplate-dashboard-argon)

Argon Dashboard is built with over 100 individual components, giving you the freedom of choosing and combining. All components can take variations in color, that you can easily modify using SASS files.

You will save a lot of time going from prototyping to full-functional code, because all elements are implemented. This Dashboard is coming with pre-built examples, so the development process is seamless, switching from our pages to the real website is very easy to be done.

- [Flask Dashboard Argon](https://appseed.us/admin-dashboards/flask-boilerplate-dashboard-argon) - Product page
- [Flask Dashboard Argon Demo](https://flask-dashboard-argon.appseed.us/) - LIVE app

---

![Flask Argon Dashboard.](https://raw.githubusercontent.com/app-generator/static/master/products/flask-dashboard-argon-screen.png)

--- 

> Thanks for reading! For more resources please access:

- [Flask](https://palletsprojects.com/p/flask/) - the official website
- More [Flask Dashboards](https://appseed.us/admin-dashboards/flask) provided by AppSeed

---

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