Flask Boilerplate - Static Export and Live FTP deploy
Tags: flask, python, devtools
Published: 2019-09-06 08:13:35

---
title: Flask Boilerplate - Static Export and Live FTP deploy
published: true
description: Convert a Flask application to a static website with a few lines of code
cover_image: https://raw.githubusercontent.com/app-generator/static/master/products/flask-paper-kit-pages-intro.gif
tags: flask, python, devtools
---

Hello Coder, 

In this short article, I will present a simple way to export a [Flask](https://palletsprojects.com/p/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](https://appseed.us)**.

---

## [What is Flask](https://docs.appseed.us/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](https://github.com/app-generator/flask-paper-kit)

To clone and build the app we need to type a few lines of code (for full instructions, please refer the project [README](https://github.com/app-generator/flask-paper-kit/blob/master/README.md) file):

```bash
$ # 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:

![Flask Boilerplate - Gif animated intro](https://raw.githubusercontent.com/app-generator/static/master/products/flask-paper-kit-intro.gif)

---

## The Static Export vis [Frozen-Flask](http://packages.python.org/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](https://github.com/app-generator/flask-paper-kit/blob/master/static.py) 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](https://github.com/app-generator/flask-paper-kit/blob/master/deploy.js) executed under Node.js to upload the files via FTP. 

```javascript
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: 

![Flask Boilerplate - FTP Live deploy.](https://raw.githubusercontent.com/app-generator/static/master/products/flask-boilerplate-live-deploy-intro.gif)

---

### Thank You!

- [App Sources](https://github.com/app-generator/flask-paper-kit) - MIT License
- [Live Demo](https://flask-paper-kit.appseed.us/)
- [Flask Boilerplate](https://appseed.us/boilerplate-code/flask-boilerplate) - supported by AppSeed
- More [Boilerplate Code](https://appseed.us/boilerplate-code/) - provided by AppSeed


![Funny](https://media.giphy.com/media/JEYYpBGPLq98I/giphy.gif)