Hello Coder,
In this short article, I will present a simple way to export a Flask application as into a static website and later, deploy the HTML files on the live system via FTP. The Flask application referenced in this article, along with the scripts is published on Github (MIT license).
Thank you! Content provided by AppSeed - App Generator.
What is Flask
A short note for newcomers: Flask is a lightweight web framework written in Python suitable for (almost) any kind of web app: one-page, API servers, or complex eCommerce apps and portals. The official info (copied from their website):
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. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks.
Build the Flask App
To clone and build the app we need to type a few lines of code (for full instructions, please refer the project README file):
$ # clone the repo
$ git clone https://github.com/app-generator/flask-paper-kit.git
$ cd flask-paper-kit
$ # install the dependencies
$ pip install -r requirements.txt
$ # create the database (using Flask shell)
$ flask shell
$ >>> from app import db
$ >>> db.create_all()
$ flask run
$ # App is running on http://localhost:5000
If all goes well, the app should be visible in your browser:
The Static Export vis Frozen-Flask
Frozen-Flask basically freezes a Flask application into a set of static files, so you can host it with ease. The static export hooks into app routes and extracts the HTML rendered by the Flask engine. Pretty cool, I must say.
The export script contains only a few lines of code:
import os
from app import app
from flask_frozen import Freezer
#----------------------------------------
# launch
#----------------------------------------
if __name__ == "__main__":
freezer = Freezer(app)
freezer.freeze()
As we can see, the Flask-Frozen build a wrapper around the victim, executes the registered routes and save the rendered HTML output into app/public
directory.
FTP Live deploy
To avoid the manual copy, I'm using a simple script executed under Node.js to upload the files via FTP.
var FtpDeploy = require('ftp-deploy');
var ftpDeploy = new FtpDeploy();
var config = {
user: "FTP_USER", # edit ME
password: "FTP_PASS", # edit ME
host: "FTP_HOST", # edit ME
port: 21,
localRoot: __dirname + '/app/build', # upload ME
remoteRoot: '/',
include: ['*', '**/*'],
....
}
Before executing the script don't forget to run a yarn command in the root of the project (to install locally the Node.js module ftp-deploy) and edit the FTP credentials.
To execute the script just type node deploy
and you should see this magic in your terminal:
Thank You!
- App Sources - MIT License
- Live Demo
- Flask Boilerplate - supported by AppSeed
- More Boilerplate Code - provided by AppSeed