Learning Python3 Flask Part 1

Quick Start

Posted by Riino on June 21, 2020 Python Flask Server Web


Like other python package, simply install flask via pip or other tool. Flask is a “micro” kernel of a web server.

For detailed guidance of installation, please visit https://flask.palletsprojects.com/en/1.1.x/installation/#python-version.


Flask app can run as a isolated package, which is my personal recommended approach of implementation, in this case ,you need create a folder structure like:

  │  ├─YourFlaskApp
  │  │  ├─static
  │  │  │  ├─css
  │  │  │  ├─img
  │  │  │  └─js
  │  │  └─templates
  │  │     └─layout
  │  ├─ __init__.py
  │  └─ views.py
  │	└─YourData.csv

Why we do this

Via such structure, we are actually created a packaged app called YourFlaskApp, which means we can hold more than one such flask app package in same repo, and we can import them in runserver.py and make custom configuration via config.py, in other world, runserver.py will be our global custom starter for all flask apps, and config.py will be a global config script for all flask apps, too.

For example, we can set data in config.py, just like what we do in _config.yaml, but in pythons’ format of data:

Make configuration

Usually the static config should be created in config.py.


TITLE='Your website title'

Notice that , in flask, we should name all variables in uppercase.

When finished this, we can use this config.py ,we can use the variables here in both flask back-end and front-end directly later, but before this we need to clarify How Flask Runs:

Generally the most common way to run flask is to use :

flask run

This will actually try to find if there’s any flask app package in current path, and try to run the __init__.py inside, this script should be written like this:


from flask import Flask
import config
app = Flask(__name__)
import YourFlaskApp.views

__init__.py will create a flask app entity and make init config. We can also change the init config by changing the code here, for example, we can replace line 3 into :

app = Flask(__name__, static_url_path='src')

This will redirect the default static source folder , static, into src . Also, we can make other init config here.The views script holds all the controller method, e.g.


from YourFlaskApp import app
def index():
    return 'Hello World!'

This will get current flask app via __name__, and get config from current path. Then import the views script in the root of this flask app package, then , flask run common will run app.run() in background to start the server. Thus, we can make our own script for startup:


from  YourFlaskApp import app

And we can use

py runserver.py

To start the server, just like flask run, but we can add custom config here, like enable the debug mode.


We need to check 3 configuration before the app runs:

  1. Start config: runserver.py (if you want to use default config, like in Production server, use flask run)
  2. Init config: __init__.py
  3. Variable config: config.py.