This page explains how to deploy a Flask application on Heroku, the popular deployment platform.
@@ -795,8 +795,7 @@ Heroku is a cloud-based, fully-managed platform as a service (Paas) for building
Heroku apps include a Procfile that specifies the commands that are executed by the app on startup.
As specified in the official docs, the Procfile is always a simple text file that is named Procfile without a file extension.
-
web: gunicorn run:app --log-file=-
-
+
web: gunicorn run:app --log-file=-
For our sample, gunicorn is called with run:app argument.
@@ -808,36 +801,35 @@ Python 3.7.2 # <--- All good
Change the current directory to source code directory
-
$ # Make sure you are running the commands INSIDE source code directory
-$
-$ # Create and activate a Virtualenv (Unix based systems)
-$ virtualenv env
-$ source env/bin/activate
-$
-$ # Create and activate a Virtualenv (Windows based systems)
-$ # virtualenv env
-$ # .\env\Scripts\activate
-$
-$ # Install requirements
-$ pip3 install-r requirements.txt
-$
-$ # Set the FLASK_APP environment variable
-$ (Unix/Mac)export FLASK_APP=run.py
-$ (Windows)set FLASK_APP=run.py
-$ (Powershell)$env:FLASK_APP =".\run.py"
-$
-$ # Set up the DEBUG environment
-$ # (Unix/Mac) export FLASK_ENV=development
-$ # (Windows) set FLASK_ENV=development
-$ # (Powershell) $env:FLASK_ENV = "development"
-$
-$ # Run the application
-$ # --host=0.0.0.0 - expose the app on all network interfaces (default 127.0.0.1)
-$ # --port=5000 - specify the app port (default 5000)
-$ flask run --host=0.0.0.0 --port=5000
-$
-$ # Access the app in browser: http://127.0.0.1:5000/
-
+
$ # Make sure you are running the commands INSIDE source code directory
+$
+$ # Create and activate a Virtualenv (Unix based systems)
+$ virtualenv env
+$ source env/bin/activate
+$
+$ # Create and activate a Virtualenv (Windows based systems)
+$ # virtualenv env
+$ # .\env\Scripts\activate
+$
+$ # Install requirements
+$ pip3 install -r requirements.txt
+$
+$ # Set the FLASK_APP environment variable
+$ (Unix/Mac) export FLASK_APP=run.py
+$ (Windows) set FLASK_APP=run.py
+$ (Powershell) $env:FLASK_APP = ".\run.py"
+$
+$ # Set up the DEBUG environment
+$ # (Unix/Mac) export FLASK_ENV=development
+$ # (Windows) set FLASK_ENV=development
+$ # (Powershell) $env:FLASK_ENV = "development"
+$
+$ # Run the application
+$ # --host=0.0.0.0 - expose the app on all network interfaces (default 127.0.0.1)
+$ # --port=5000 - specify the app port (default 5000)
+$ flask run --host=0.0.0.0 --port=5000
+$
+$ # Access the app in browser: http://127.0.0.1:5000/
At this point, we can visit the app in the browser http://127.0.0.1:5000/.
By default, the app will redirect guest users to the login page. To access the private pages:
@@ -854,24 +846,25 @@ By default, the app will redirect guest users to the login page. To access the p
Starter uses a simple codebase (no Blueprints) with a structure presented bellow:
-
< PROJECT ROOT >
+
<PROJECTROOT>
|
- |-- app/ # Implements app logic
- | |-- base/ # Base Blueprint - handles the authentication
- | |-- home/ # Home Blueprint - serve UI Kit pages
+ |-- app/ # Implements app logic
+ | |-- base/ # Base Blueprint - handles the authentication
+ | |-- home/ # Home Blueprint - serve UI Kit pages
| |
- | __init__.py # Initialize the app
+ | __init__.py # Initialize the app
|
- |-- requirements.txt # Development modules - SQLite storage
- |-- requirements-mysql.txt # Production modules - Mysql DMBS
- |-- requirements-pqsql.txt # Production modules - PostgreSql DMBS
+ |-- requirements.txt # Development modules - SQLite storage
+ |-- requirements-mysql.txt # Production modules - Mysql DMBS
+ |-- requirements-pqsql.txt # Production modules - PostgreSql DMBS
|
- |-- .env # Inject Configuration via Environment
- |-- config.py # Set up the app
- |-- run.py # Start the app - WSGI gateway
+ |-- .env # Inject Configuration via Environment
+ |-- config.py # Set up the app
+ |-- run.py # Start the app - WSGI gateway
|
- |-- ************************************************************************
-
@@ -897,22 +890,20 @@ By default, the app will redirect guest users to the login page. To access the p
.env (saved in the root of the project)
-
# File: `.env`
+
# File: `.env`
-DEBUG=True # Enable/Disable the development environment
+DEBUG=True # Enable/Disable the development environment
-SECRET_KEY=S3cr3t_Key # The Key used by Flask to encrypt session information
+SECRET_KEY=S3cr3t_Key # The Key used by Flask to encrypt session information
-# Database production settings (If DEBUG=False)
+# Database production settings (If DEBUG=False)
-DB_ENGINE=postgresql # DBMS
-DB_NAME=appseed-flask # Database Name
-DB_HOST=localhost # Database Host
-DB_PORT=5432 # Database Port
-DB_USERNAME=appseed # DB Username
-DB_PASS=pass # DB Password
-
-
+DB_ENGINE=postgresql # DBMS
+DB_NAME=appseed-flask # Database Name
+DB_HOST=localhost # Database Host
+DB_PORT=5432 # Database Port
+DB_USERNAME=appseed # DB Username
+DB_PASS=pass # DB Password
@@ -920,16 +911,15 @@ By default, the app will redirect guest users to the login page. To access the p
@@ -1079,16 +1066,14 @@ By default, the app will redirect guest users to the login page. To access the p
This configuration becomes active if .env file has the DEBUG file set to True
-
# Development/Debug configuration
-
-# Set up the App SECRET_KEY
-SECRET_KEY=config('SECRET_KEY',default='S#perS3crEt_007')
+
# Development/Debug configuration
-# This will create a file in <app> FOLDER
-SQLALCHEMY_DATABASE_URI='sqlite:///'+os.path.join(basedir,'db.sqlite3')
-SQLALCHEMY_TRACK_MODIFICATIONS=False
+# Set up the App SECRET_KEY
+SECRET_KEY = config('SECRET_KEY', default='S#perS3crEt_007')
-
+# This will create a file in <app> FOLDER
+SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')
+SQLALCHEMY_TRACK_MODIFICATIONS = False
During the first request, the SQLite database and tables are automatically created in the root in the project.
@@ -1104,23 +1089,21 @@ By default, the app will redirect guest users to the login page. To access the p
This configuration becomes active if .env file has the DEBUG file set to False
-
# Production configuration
-
-SESSION_COOKIE_HTTPONLY=True
-REMEMBER_COOKIE_HTTPONLY=True
-REMEMBER_COOKIE_DURATION=3600
+
+@login_manager.request_loader
+def request_loader(request):
+ username = request.form.get('username')
+ user = User.query.filter_by(username=username).first()
+ return user if user else None
@@ -1306,13 +1286,11 @@ The authentication status is checked by @login_r
<divclass="collapse navbar-collapse"id="navigation"><ulclass="navbar-nav ml-auto"><!-- The Usage of <current_user> object -->
@@ -1342,8 +1320,7 @@ The authentication status is checked by @login_r
</ul>
- </div>
-
CentOS is a Linux distribution that provides a free, community-supported computing platform functionally compatible with its upstream source, Red Hat Enterprise Linux (RHEL). In January 2014, CentOS announced the official joining with Red Hat while staying independent from RHEL under a new CentOS governing board.
@@ -689,10 +688,8 @@
The Development Tools package group provides the GNU Compiler Collection (GCC), GNU Debugger (GDB), and other related development tools.
-
$ # install Development Tools bundle
-$ sudo yum group install"Development Tools"
-
-
+
$ # install Development Tools bundle
+$ sudo yum group install "Development Tools"
@@ -701,8 +698,7 @@
Git is the most popular version control system on Linux. It is easy to use, amazingly fast, it’s very efficient with large projects, and it has an incredible branching system for non-linear development.
-
$ sudo yum install git
-
+
$ sudo yum install git
@@ -710,16 +706,13 @@
By default, CentOS is not installing python3, but we can easily set up the installation.
-
$ sudo yum install python3
-$
-
+
$ sudo yum install python3
And Python3 libraries for development
-
sudo yum install python3-devel
-
+
sudo yum install python3-devel
@@ -727,8 +720,7 @@
Apache is available within CentOS’s default software repositories, which means you can install it with the yum package manager.
-
$ sudo yum install httpd
-
+
$ sudo yum install httpd
@@ -736,17 +728,16 @@
-
$ # this cmd will start the server
-$ sudo systemctl start httpd
-$
-$ # test server status
-$ sudo systemctl status httpd
-$
-$ # access the default page with `lynx`
-$ sudo yum install lynx
-$
-$ lynx http://localhost
-
+
$ # this cmd will start the server
+$ sudo systemctl start httpd
+$
+$ # test server status
+$ sudo systemctl status httpd
+$
+$ # access the default page with `lynx`
+$ sudo yum install lynx
+$
+$ lynx http://localhost
@@ -754,11 +745,10 @@
Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside of a browser.
-
$ sudo yum install nodejs
-$
-$ # check the version
-$ node --version
-
+
$ sudo yum install nodejs
+$
+$ # check the version
+$ node --version
@@ -766,11 +756,10 @@
The yarn is an advanced package management software for Node.js applications. It is a fast, secure, and reliable alternative that any other Nodejs package manager.
-
$ sudo npm install yarn -g
-$
-$ # check the version
-$ yarn -v
-
+
$ sudo npm install yarn -g
+$
+$ # check the version
+$ yarn -v
@@ -779,8 +768,7 @@
RHEL 8 does not officially support Docker; in this section, we will show how to install the new set of container tools as well as the old lady, docker package. The docker package is replaced by the Container Tools module, which consists of tools such as Podman, Buildah, Skope, and several others.
-
$ dnf module install-y container-tools
-
+
$ dnf module install -y container-tools
@@ -789,10 +777,9 @@
Now install docker from the official repositories by running the following commands. Here, the yum-utils package provides the yum-config-manager utility.
Ubuntu is a complete Linux operating system, freely available with both community and professional support. Ubuntu is suitable for both desktop and server use. The current Ubuntu release supports many architectures: Intel x86 (IBM-compatible PC), AMD64 (x86-64), ARMv7, ARMv8.
Ubuntu includes thousands of pieces of software, starting with the Linux kernel version 4.15 and GNOME 3.28, and covering every standard desktop application from word processing and spreadsheet applications to internet access applications, web server software, email software, programming languages and tools and of course several games. For more information please access the official website: Ubuntu.com
@@ -687,10 +686,8 @@
The Build Essential package group provides the GNU Compiler Collection (GCC), GNU Debugger (GDB), and other related development tools.
Git is the most popular version control system on Linux. It is easy to use, amazingly fast, it’s very efficient with large projects, and it has an incredible branching system for non-linear development.
-
$ sudo apt install install git
-
+
$ sudo apt install install git
@@ -708,16 +704,14 @@
Ubuntu comes with both Python 2.7 and Python 3.5 by default. You can install Python 3.6 along with them via a third-party PPA by doing the following steps:
Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside of a browser.
-
$ sudo apt-get install install nodejs
-$
-$ # check the version
-$ node --version
-
+
$ sudo apt-get install install nodejs
+$
+$ # check the version
+$ node --version
@@ -737,11 +730,11 @@
The yarn is an advanced package management software for Node.js applications. It is a fast, secure, and reliable alternative that any other Nodejs package manager.
-
$ sudo npm install yarn -g
-$
-$ # check the version
-$ yarn -v
-
+
$ sudo npm install yarn -g
+$
+$ # check the version
+$ yarn -v
Microsoft Windows, commonly referred to as Windows, is a group of several proprietary graphical operating system families, all of which are developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. Active Microsoft Windows families include Windows NT and Windows IoT; these may encompass subfamilies, e.g. Windows Server or Windows Embedded Compact (Windows CE). Defunct Microsoft Windows families include Windows 9x, Windows Mobile and Windows Phone.
@@ -714,9 +713,8 @@
If the installation goes well, test the Python execution from the terminal: