Flask - Code a simple web app
This tutorial aims to help beginners to start coding simple apps using Flask, the popular Python framework. Before reading or coding anything suggested in this tutorial, it might be a good idea to access the official Flask resources (docs, quickstart):
- Flask - the official website
- Flask Documentation
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 is written in Python and 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.
Flask Links
- Flask - the official website
- Flask Documentation
- What is Flask - a comprehensive resource about Flask
Environment
To use the stater, Python3 should be installed properly in the workstation. If you are not sure if Python is properly installed, please open a terminal and type python --version
. The full-list with dependencies and tools required to build the app:
- Python3 - the programming language used to code the app
- GIT - used to clone the source code from the Github repository
- Basic development tools (g++ compiler, python development libraries ..etc) used by Python to compile the app dependencies in your environment.
Check Python version (using the terminal)
$ # Check Python version
$ python --version
Python 3.7.2 # <--- All good
Check GIT command tool (using the terminal)
$ # Check git
$ git --version
$ git version 2.10.1.windows.1 # <--- All good
For more information on how to set up your environment please access the resources listed below. In case we've missed something, contact us on Discord.
- How to set up Python
- Setup CentOS for development
- Setup Ubuntu for development
- Setup Windows for development
Flask - a minimal app
The most simple app, coded in Flask, looks like this:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello from Flask!'
We save this source code in hello.py
. The name is not important, but we will reference the file name in the next section of this tutorial.
What this code means
- Flask module is imported
- the app (WSGI compliant) is constructed by Flask
- a (default) route is defined that returns a friendly message to the user:
Hello from Flask
When a request is received by our app, usually sent by the browser, the Flask core tries to match the path and execute the right handler.
Execute our minimal app
$ export FLASK_APP=hello.py
$ flask run
* Running on http://127.0.0.1:5000/
Please notice that export before we start the app using Flask development server. Flask itself should be informed (via the environment) what file should load and execute. In our sample is hello.py
.
At this moment, we can visit the app in the browser, on port 5000
, the default port used by Flask to serve apps. To start the app on a different port, we must call Flask using --port
argument.
$ export FLASK_APP=hello.py
$ flask run --port=9999
* Running on http://127.0.0.1:9999/
This time, the app is started on port 9999
.
Where to go from here
By accessing the AppSeed platform anyone, even without an account can access and use many Flask starters pre-built with a basic set of modules like authentication, database and deployment scripts.
Flask - Jinja2 Theme
A super minimal Flask application provided as a template project for rendering UI themes converted to jinja. The app has no database, authentication or any other module that can confuse beginners and might be a good choice to start coding something simple in Flask.
Flask App Boilerplate
A template project, coded on top of a nice UI kit, enhanced with an SQLite database, ORM, authentication - app links:
Flask Dashboard Boilerplate
A template project, coded on top of an admin dashboard UI kit, enhanced with an SQLite database, ORM, authentication - app links:
For support and more resources please access the AppSeed platform or join our community on Discord.