User:Pedro Sá Couto/Prototyping 5th/Flask App/Structuring the App

From XPUB & Lens-Based wiki

Structuring the App

01 Activate the created Virtual Environment

$ source venv/bin/activate

02 Run the app

$ flask run --host=0.0.0.0

03 Create app directory

$ mkdir app

04 Go to the app directory

$ cd app

05 Create two new files

$ touch __init__.py views.py

06 __init__.py file

from flask import Flask

app = Flask(__name__)

from app import views

07 views.py file

from app import app

@app.route("/")def index():
    return "Hello world"

@app.route("/about")def about():
    return "All about Flask"

08 Create the entry point for the app to run

Rename app.py to run.py

    from app import app

    if __name__ == "__main__":

        app.run()

09 Change environment variables

$ cd ..
$ export FLASK_APP=run.py
$ export FALSK_ENV=development
$ flask run --host=0.0.0.0

10 Organize admin_views, login_view, api_view, etc

$ cd app
$ touch admin_views.py
$ vim admin_views.py
from app import app

@app.route("/admin/dashboard")
def admin_dashboard():
    return "Admin Dashboard"

@app.route("/admin/profile")
def admin_profile():
    return "Admin profile"

11 Import changes to init

$ vim __init__.py

Add to the bottom

from app import admin_views

12 Create requirements.txt file

$ pip3 freeze > requirements.txt

13 To install all requirements

$ pip3 install -r requirements.txt





Building your first Flask app - Python on the web - Learning Flask series Pt. 2
https://www.youtube.com/watch?v=-BC3V1CUKpU&t=44s
https://pythonise.com/series/learning-flask/flask-application-structure